# 2.2 Sequence homology searching ¶

In this chapter we'll talk about using pairwise alignment to search databases of biological sequences with the goal of identifying sequence homology. We previously defined homology between a pair of sequences to mean that those sequences are derived from a common ancestral sequence. Homology searching is an essential part of making inferences about where a biological sequence came from, and/or what it does. In most cases, if you have an unannotated biological sequence, such as the following protein sequence, it's very hard (really, impossible) to know what it is without more information.

> mystery-sequence1
PQITLWQRPLVTIRIGGQLKEALLDTGADDTVLEEMNLPGKWKPKMIGGIGGFIKVRQYDQIPVEIAHKAIGTVLVGPTPVNIIGRNLLTQIGATLNF

What a researcher will often do is search this sequence, their query, against some reference database of annotated sequences to learn what function the sequence performs (if the reference database contains functional annotation of sequences) and/or what organisms are likely to encode this sequence in their genome (if the reference database contains taxonomic annotation of sequences).

Whose genome is the above sequence encoded in? What is its function? Take a minute now to answer these questions using the Protein BLAST homology search tool on the NCBI website.

In the context of database searching, a query sequence and a reference sequence that we hypothesize to be homologous can be identical to one another, or they can differ as a result of mutation events. When sequences differ, we're often then interested in how much they differ, or their pairwise similarity, which can help us identify the most closely related of several homologs in the reference database. There is an important distinction in the terms homology and similarity: homology is a discrete variable, and similarity is a continuous variable. A pair of biological sequences either are or are not derived from a common ancestor, but they can be more or less similar to each other. Saying that two sequences are 80% homologous doesn't make sense. What people generally mean when they say this is that two sequences are 80% similar, and as a result they are hypothesizing homology between the sequences.

## 2.2.1 Defining the problem¶

As mentioned above, if we want to perform a homology search we'll have one or more query sequences, and for each we want to know which sequence(s) in a reference database it is most similar to.

Sequence homology searching can be implemented in a few ways. In this chapter, we'll use the local alignment function that we worked with in the Pairwise Alignment chapter, local_pairwise_align_ssw, run it many times to search one query sequence against many reference sequences, and investigate the highest scoring alignment(s) to identify the best database match. Remember that you can always get help with a function by passing it as an argument to help:

In [1]:
from skbio.alignment import local_pairwise_align_ssw
help(local_pairwise_align_ssw)

Help on function local_pairwise_align_ssw in module skbio.alignment._pairwise:

local_pairwise_align_ssw(sequence1, sequence2, **kwargs)
Align query and target sequences with Striped Smith-Waterman.

State: Experimental as of 0.4.0.

Parameters
----------
sequence1 : DNA, RNA, or Protein
The first unaligned sequence
sequence2 : DNA, RNA, or Protein
The second unaligned sequence

Returns
-------
tuple
TabularMSA object containing the aligned sequences, alignment score
(float), and start/end positions of each input sequence (iterable
of two-item tuples). Note that start/end positions are indexes into the
unaligned sequences.

Notes
-----
This is a wrapper for the SSW package [1]_.

For a complete list of optional keyword-arguments that can be provided,
see skbio.alignment.StripedSmithWaterman.

The following kwargs will not have any effect: suppress_sequences,
zero_index, and protein

If an alignment does not meet a provided filter, None will be returned.

References
----------
.. [1] Zhao, Mengyao, Wan-Ping Lee, Erik P. Garrison, & Gabor T.
Marth. "SSW Library: An SIMD Smith-Waterman C/C++ Library for
Applications". PLOS ONE (2013). Web. 11 July 2014.
http://www.plosone.org/article/info:doi/10.1371/journal.pone.0082138

--------
skbio.alignment.StripedSmithWaterman



When our reference database starts getting hundreds of millions of bases long (as would be the case if we were searching against 97% OTUs from the Greengenes small-subunit ribosomal RNA (SSU rRNA) reference database), billions of bases long (as would be the case if we were searching against the human genome) or trillions of bases long (as would be the case if we were searching against the NCBI non-redundant nucleotide database), runtime becomes an important consideration. For that reason, learning about heuristic algorithms is an essential part of learning about sequence homology searching. Heuristic algorithms apply some rules (i.e., heuristics) to approximate the correct solution to a problem in a fraction of the runtime that would be required if we wanted to be guaranteed to find the correct solution. Heuristic algorithms are very common in bioinformatics, and we'll use them in several other places in this book.

While we'll be aligning nucleotide sequences in this chapter, the same concepts apply to protein homology searching.

The first thing we'll do as we learn about sequence homology searching is load some annotated sequences. The sequences that we're going to work with are derived from the Greengenes database, and we're accessing them using the QIIME default reference project. Greengenes is a database of 16S rRNA gene sequences, a component of the archaeal and bacterial ribosome (the molecular machine that drives translation of mRNA to proteins). This gene is of a lot of interest to biologists because it's one of about 200 genes that are encoded in the genomes of all known cellular organisms. We'll come back to this gene a few times in the book, notably in Studying Biological Diversity. The sequences in Greengenes are taxonomically annotated, meaning that we'll have a collection of gene sequences and the taxonomic identity of the organism whose genome the sequence is found in. If we search an unannotated 16S rRNA query sequence against this database, we can make inferences about what organism our query sequence is from.

First, let's load Greengenes into a list of skbio.DNA sequence objects, and associate the taxonomy of each sequence as sequence metadata.

In [2]:
%pylab inline

from IPython.core import page
page.page = print

Populating the interactive namespace from numpy and matplotlib

In [3]:
import qiime_default_reference as qdr
import skbio

reference_taxonomy = {}
for e in open(qdr.get_reference_taxonomy()):
seq_id, seq_tax = e.strip().split('\t')
reference_taxonomy[seq_id] = seq_tax

# Load the reference sequences, and associate the taxonomic annotation with
reference_db = []
for e in skbio.io.read(qdr.get_reference_sequences(), format='fasta', constructor=skbio.DNA):
reference_db.append(e)

print("%s sequences were loaded from the reference database." % len(reference_db))

99322 sequences were loaded from the reference database.


Next, we'll just inspect a couple of the sequences we loaded. Notice how the specificity of our taxonomic annotations (i.e., how many taxonomic levels are annotated and unknown) differs for different sequences.

In [4]:
reference_db[0]

Out[4]:
DNA
-----------------------------------------------------------------------
'description': ''
'id': '1111883'
'taxonomy': 'k__Bacteria; p__Gemmatimonadetes; c__Gemm-1; o__; f__;
g__; s__'
Stats:
length: 1428
has gaps: False
has degenerates: False
has definites: True
GC-content: 61.90%
-----------------------------------------------------------------------
0    GCTGGCGGCG TGCCTAACAC ATGTAAGTCG AACGGGACTG GGGGCAACTC CAGTTCAGTG
60   GCAGACGGGT GCGTAACACG TGAGCAACTT GTCCGACGGC GGGGGATAGC CGGCCCAACG
...
1320 GCCGCGGTGA ATACGTTCCC GGGCCTTGTA CACACCGCCC GTCACGCCAT GGAAGCCGGA
1380 GGGACCCGAA ACCGGTGGGC CAACCGCAAG GGGGCAGCCG TCTAAGGT
In [5]:
reference_db[-1]

Out[5]:
DNA
----------------------------------------------------------------------
'description': ''
'id': '4483258'
'taxonomy': 'k__Archaea; p__Crenarchaeota; c__Thermoprotei;
o__Thermoproteales; f__Thermoproteaceae; g__; s__'
Stats:
length: 2123
has gaps: False
has degenerates: False
has definites: True
GC-content: 58.36%
----------------------------------------------------------------------
0    CTGGTTGATC CTGCCGGACC CGACCGCTAT CGGGGTGGGG CTTAGCCATG CGAGTCAAGC
60   GCCCCAGGGA CCCGCTGGGG TGCGGCGCAC GGCTCAGTAA CACGTGGCCA ACCTACCCTC
...
2040 ATAATCTCCT TATTGTCTGA TCCTTATGCA TTTTCCTTTG GCCCATCCCG TGAATACGCG
2100 CGGTGAATAC GTCCCTGCCC CTT

For the sake of runtime, we're going to work through this chapter using a random sample of sequences from this database. Here we'll use Python's random module to select sequences at random.

In [6]:
import random

reference_db = random.sample(reference_db, k=5000)
print("%s sequences are present in the subsampled database." % len(reference_db))

5000 sequences are present in the subsampled database.


We'll also extract some sequences from Greengenes to use as query sequences in our database searches. This time we won't annotate them (to simulate not knowing what organisms they're from). We'll also trim these sequences so they're shorter than the full length references. This will simulate obtaining a partial gene sequence, as is most common with the current sequencing technologies (as of this writing), but will also help to make the examples run faster.

Note that some of our query sequences may also be in our subsampled reference database and some won't. This is realistic: sometimes we're working with sequences that are exact matches to known sequences, and sometimes we're working with sequences that don't match any known sequences (or at least any in the reference database that we're working with).

In [7]:
queries = []
for e in skbio.io.read(qdr.get_reference_sequences(), format='fasta', constructor=skbio.DNA):
e = e[100:300]
queries.append(e)
queries = random.sample(queries, k=500)


Let's inspect a couple of the query sequences that we'll work with.

In [8]:
queries[0]

Out[8]:
DNA
---------------------------------------------------------------------
'description': ''
'id': '573052'
Stats:
length: 200
has gaps: False
has degenerates: False
has definites: True
GC-content: 41.50%
---------------------------------------------------------------------
0   CGGGTGAGTA ACGCGTATGC AACCTACCTT TTACTGGGGA ATAGTCAAGA GAAATTTTGA
60  ATAATACCCC ATACGATCTT TAAATATCAT TATTTTTAGA AGAAAACTTC GGTGGTAAAA
120 GATGGGCATG CGTCCTATTA GTTTGTTGGT AAGGTAATGG CTTACCAAGA CTTCGATAGG
180 TAGGGGCCCT GAGAGGGGGA
In [9]:
queries[-1]

Out[9]:
DNA
---------------------------------------------------------------------
'description': ''
'id': '180681'
Stats:
length: 200
has gaps: False
has degenerates: False
has definites: True
GC-content: 50.50%
---------------------------------------------------------------------
0   CGGGTGAGTA ACGCGTGAGC AACCTGCCTC TCAGAGGGGA ATAACGTTTT GAAAAGAACG
60  CTAATACCGC ATAACATATC GGAACCGCAT GATTCTGATA TCAAAGGAGC AATCCGCTGA
120 GAGATGGGCT CGCGTCCGAT TAGTTAGTTG GTGAGGTAAC GGCTCACCAA GACTACGATC
180 GGTAGCCGGA CTGAGAGGTT

## 2.2.3 Defining the problem¶

The problem that we are going to address here is as follows. We now have a query sequence ($q_i$) which is not taxonomically annotated (meaning we don't know the taxonomy of the organism whose genome it is found in), and a reference database ($R$) of taxonomically annotated sequences ($r_1, r_2, r_3, ... r_n$). We want to infer a taxonomic annotation for $q_i$. We'll do this by identifying the most closely evolutionary related sequences in $R$ and associating their taxonomy with $q_i$. Because we actually do know the taxonomy of $q_i$ (to the extent that we trust the annotations in $R$), we can evaluate how well this approach works.

There are a few realistic features of the situation that we've set up here that I want you to be aware of.

1. All of the query and reference sequences are homologous. In this case, they are all sequences of the 16S rRNA gene from archaea and bacteria. This may or may not be the case in real-world applications. Sometimes you'll work with gene-specific databases such as Greengenes, and sometimes you'll work with non-specific databases such as the NCBI nucleotide database (nr). Regardless, the search process is similar.
2. The evolutionary distance between each query sequence and its most closely related sequences in $R$ will vary widely. Sometimes $q$ will be an exact match to a reference sequence $r_i$, and sometimes we may have as little as $50\%$ similarity.

As we work through the next sections, imagine that we're exploring scaling this system up, so that instead of search just one or a few query sequences against the reference database, we ultimately want to apply this to search millions of sequences against the database. This would be the real-world problem we faced if we had collected 16S rRNA sequences from the environment (which would of course be unannotated) using high-throughput DNA sequencing.

## 2.2.4 A complete homology search function¶

Let's define a homology search function that aligns each provided query sequences $q_i$ with each of our reference database sequences ($r_1, r_2, r_3, ... r_n$). This function will take as input one or more query sequences, and the reference database. We'll call the top scoring alignments for each $q_i$ the best hits, and we'll specifically request some number (n) of best hits for each $q_i$. The output of this function will be a summary of the n best hits for each query sequence, including some technical information about the alignment and the taxonomy associated with the corresponding reference sequence. We'll then review the taxonomy annotations for our best hits, and from those make an inference about the taxonomy annotation for $q$.

Spend a minute looking at this function to understand what it's doing.

In [10]:
from iab.algorithms import local_alignment_search

%psource local_alignment_search

def local_alignment_search(queries, reference_db, n=5,
aligner=local_pairwise_align_ssw):
results = []
indices = []
for q in queries:
# first we'll compute all of the alignments and their associated scores
hits = []
for r in reference_db:
aln, score, _ = aligner(q, r)
# then we reverse-sort them by score, and return the n highest
# scoring alignments (this needs to be updated so we only
# ever keep track of the n highest scoring alignments)
best_hits = sorted(hits, key=lambda e: e[1], reverse=True)[:n]
if len(best_hits) == 0:
# if there are no hits, log that information
results.append(("n/a", np.nan, np.nan, np.nan))
else:
# otherwise compile and track some information about the n
# best hits
for r_id, score, aln, r_tax in best_hits:
percent_similarity = (100 * (1. - aln[0].distance(aln[1])))
aln_length = aln.shape[1]
results.append((r_tax, percent_similarity,
aln_length, score))
index = pd.MultiIndex.from_tuples(indices, names=['query', 'reference'])
columns = ['reference taxonomy', 'percent similarity',
'alignment length', 'score']
results = pd.DataFrame(results, index=index, columns=columns)
return results



Now let's perform some database searches. You can run the remaining code cells in this section a few times to experiment with searching different query sequences against the same reference database.

This next cell, which is the one that actually performs the database searches, will take a little bit of time to run (maybe up to a minute or two). There is some code in this cell that will track the runtime. As it's running, think about how many query sequences we're searching against how many reference sequences, and refer back to the number of sequences in the full reference database. Does this strategy seem scalable to millions or sequences, which as mentioned above might be our ultimate goal? When you know the per-sequence runtime of this search, estimate how long it would take to do this in seconds for one million sequences. Convert the time in seconds to a unit that will be more meaningful to you.

In [11]:
import time

start_time = time.time()
current_queries = random.sample(queries, k=4)
results = local_alignment_search(current_queries, reference_db)
stop_time = time.time()
print("Runtime: %1.4f sec per query" % ((stop_time - start_time) / len(current_queries)))
results

Runtime: 4.4506 sec per query

Out[11]:
reference taxonomy percent similarity alignment length score
query reference
556288 674234 k__Bacteria; p__Bacteroidetes; c__Bacteroidia;... 86.206897 203 280
1136308 k__Bacteria; p__Bacteroidetes; c__Bacteroidia;... 81.862745 204 263
2510437 k__Bacteria; p__Bacteroidetes; c__Bacteroidia;... 85.858586 198 256
577041 k__Bacteria; p__Bacteroidetes; c__Bacteroidia;... 83.582090 201 251
771082 k__Bacteria; p__Bacteroidetes; c__Bacteroidia;... 82.587065 201 241
1005952 4306048 k__Bacteria; p__Firmicutes; c__Bacilli; o__Lac... 96.500000 200 365
887706 k__Bacteria; p__Firmicutes; c__Bacilli; o__Lac... 96.000000 200 360
2410938 k__Bacteria; p__Firmicutes; c__Bacilli; o__Lac... 95.500000 200 355
1051299 k__Bacteria; p__Firmicutes; c__Bacilli; o__Lac... 95.500000 200 355
1060104 k__Bacteria; p__Firmicutes; c__Bacilli; o__Lac... 94.500000 200 345
143882 553466 k__Bacteria; p__Proteobacteria; c__Deltaproteo... 83.251232 203 274
4314418 k__Bacteria; p__Proteobacteria; c__Deltaproteo... 85.858586 198 268
4388050 k__Bacteria; p__Proteobacteria; c__Alphaproteo... 86.802030 197 259
750755 k__Bacteria; p__Proteobacteria; c__Alphaproteo... 85.786802 197 249
264188 k__Bacteria; p__Proteobacteria; c__Deltaproteo... 86.705202 173 247
4340579 540748 k__Bacteria; p__Firmicutes; c__Clostridia; o__... 85.294118 204 282
13832 k__Bacteria; p__Firmicutes; c__Clostridia; o__... 84.079602 201 246
172753 k__Bacteria; p__Firmicutes; c__Clostridia; o__... 80.000000 205 243
210896 k__Bacteria; p__Firmicutes; c__Clostridia; o__... 74.641148 209 220
649398 k__Bacteria; p__Firmicutes; c__Clostridia; o__... 74.641148 209 220

Now, let's try to answer our initial question: what is the most likely taxonomic annotation for each of our query sequences? Spend a few minutes reviewing this information, and write down what you think the most likely taxonomic annotation is for each of the query sequences. Here are some hints to help you out:

• The k, p, c, o, f, g, and s refer to kingdom, phylum, class, order, family, genus, and species, respectively. If you see an annotation for a reference sequence that looks like g__, that means that the genus is unknown for that sequence.
• Just as the reference taxonomy annotations don't always go down to the species level, your taxonomic annotations don't have to either. Not assigning at a given level implies that you're uncertain about what the annotation should be at that level, and it's usually better just to indicate that you're uncertain rather than make a bad guess. If you're uncertain of what the species is, assign the query s__ and try to decide what the most likely genus is. If you're uncertain of the genus, assign g__, and try to decide what the most likely family is...
• As you look at each of the reference taxonomy annotations below, refer back to the table above to look at the percent similarity between each query and reference, and maybe the length of the alignments and their scores. These values give you an idea of how confident you should be in each of your taxonomic annotations.
In [12]:
for q in current_queries:
print('Closest taxonomies for query %s (in order):' % q_id)
for e in results['reference taxonomy'][q_id]:
print(' ', e)
print()

Closest taxonomies for query 556288 (in order):
k__Bacteria; p__Bacteroidetes; c__Bacteroidia; o__Bacteroidales; f__; g__; s__
k__Bacteria; p__Bacteroidetes; c__Bacteroidia; o__Bacteroidales; f__; g__; s__
k__Bacteria; p__Bacteroidetes; c__Bacteroidia; o__Bacteroidales; f__; g__; s__
k__Bacteria; p__Bacteroidetes; c__Bacteroidia; o__Bacteroidales; f__Porphyromonadaceae; g__; s__
k__Bacteria; p__Bacteroidetes; c__Bacteroidia; o__Bacteroidales; f__BA008; g__; s__

Closest taxonomies for query 1005952 (in order):
k__Bacteria; p__Firmicutes; c__Bacilli; o__Lactobacillales; f__Streptococcaceae; g__Streptococcus; s__
k__Bacteria; p__Firmicutes; c__Bacilli; o__Lactobacillales; f__Streptococcaceae; g__Streptococcus; s__
k__Bacteria; p__Firmicutes; c__Bacilli; o__Lactobacillales; f__Streptococcaceae; g__Streptococcus; s__
k__Bacteria; p__Firmicutes; c__Bacilli; o__Lactobacillales; f__Streptococcaceae; g__Streptococcus; s__
k__Bacteria; p__Firmicutes; c__Bacilli; o__Lactobacillales; f__Streptococcaceae; g__Streptococcus; s__

Closest taxonomies for query 143882 (in order):
k__Bacteria; p__Proteobacteria; c__Deltaproteobacteria; o__Myxococcales; f__OM27; g__; s__
k__Bacteria; p__Proteobacteria; c__Deltaproteobacteria; o__Myxococcales; f__OM27; g__; s__
k__Bacteria; p__Proteobacteria; c__Alphaproteobacteria; o__Rhodospirillales; f__Rhodospirillaceae; g__Magnetospirillum; s__
k__Bacteria; p__Proteobacteria; c__Alphaproteobacteria; o__Rhodospirillales; f__Rhodospirillaceae; g__Magnetospirillum; s__
k__Bacteria; p__Proteobacteria; c__Deltaproteobacteria; o__Myxococcales; f__; g__; s__

Closest taxonomies for query 4340579 (in order):
k__Bacteria; p__Firmicutes; c__Clostridia; o__Clostridiales; f__Veillonellaceae; g__Pelosinus; s__
k__Bacteria; p__Firmicutes; c__Clostridia; o__Clostridiales; f__Veillonellaceae; g__Dendrosporobacter; s__quercicolus
k__Bacteria; p__Firmicutes; c__Clostridia; o__Clostridiales; f__Veillonellaceae; g__Pelosinus; s__
k__Bacteria; p__Firmicutes; c__Clostridia; o__Clostridiales; f__Veillonellaceae; g__; s__
k__Bacteria; p__Firmicutes; c__Clostridia; o__Clostridiales; f__Veillonellaceae; g__; s__



Because we have taxonomic annotations for all of the Greengenes sequences (though as you probably have noticed by now, they differ in their specificity), we can next look at taxonomy associated with each of our queries in Greengenes. How do your annotations compare to those from Greengenes, which we'll print out in the next cell?

In [13]:
for q in current_queries:
print('Known taxonomy for query %s:\n %s' % (q_id, reference_taxonomy[q_id]))
print()

Known taxonomy for query 556288:
k__Bacteria; p__Bacteroidetes; c__Bacteroidia; o__Bacteroidales; f__; g__; s__

Known taxonomy for query 1005952:
k__Bacteria; p__Firmicutes; c__Bacilli; o__Lactobacillales; f__Streptococcaceae; g__Streptococcus; s__

Known taxonomy for query 143882:
k__Bacteria; p__Proteobacteria; c__Deltaproteobacteria; o__Myxococcales; f__OM27; g__; s__

Known taxonomy for query 4340579:
k__Bacteria; p__Firmicutes; c__Clostridia; o__Clostridiales; f__Veillonellaceae; g__Sporomusa; s__



## 2.2.5 Reducing the runtime for database searches¶

In the examples above, it's taking on the order of 5-15 seconds to search a single sequence against our subset of Greengenes. This makes sense when you think about the computations that are being performed. For every sequence in our reference database (5000, if you haven't modified the database subsampling step) it is computing the $F$ and $T$ matrices described in the Pairwise Alignment chapter, and then tracing back the matrix to compute the aligned sequences. Given all of that, the fact that computation only takes 5-15 seconds is pretty incredible. However, that doesn't change the fact that this doesn't scale to real-world applications because we'd have to wait way too long for results. Performing all pairwise alignments is prohibitively expensive for database searching.

As we discussed in the previous chapter, the run time of pairwise alignment scales quadratically with sequence length. Database searching, at least in the example we're exploring in this chapter, is a bit of a different problem however. Our sequence lengths aren't changing, but rather it takes a long time because we're performing a computationally expensive step, pairwise alignment, many times. Our database is fixed in that the number of sequences in it doesn't change and the sequences themselves don't change. Our query sequences are all exactly the same length in this example (remember that we set that above, when we sliced a single region from reference database sequences to create our query sequences). Let's explore how the runtime of this database search scales under these constraints.

In [14]:
import pandas as pd
import itertools

def tabulate_local_alignment_search_runtime(queries, reference_db, n_query_sequences,
n_reference_sequences, search_function):
data = []
# we'll iterate over the pairs of number of query sequences
# and number of reference sequences, and compute the runtime
# of the database search three times for each pair (so we
# have some idea of the variance in the runtimes). this is
# achieved here with a nested for loop (i.e., a for loop
# within a for loop).
for nq, nr in itertools.product(n_query_sequences, n_reference_sequences):
for i in range(3):
# select nq query sequences at random
current_queries = random.sample(queries, k=nq)
# select nr reference sequences at random
temp_reference_db = random.sample(reference_db, k=nr)
# run the search and store its runtime
start_time = time.time()
_ = search_function(current_queries, temp_reference_db)
stop_time = time.time()
median_query_sequence_len = np.median([len(q) for q in current_queries])
median_reference_sequence_len = np.median([len(r) for r in temp_reference_db])
data.append((nq, nr, median_query_sequence_len, median_reference_sequence_len,
stop_time - start_time))
runtimes = pd.DataFrame(data=np.asarray(data),
columns=["Number of query seqs", "Number of reference seqs",
"Median query seq length", "Median reference seq length",
"Runtime (s)"] )
return runtimes

# we'll temporarily work with a smaller reference database
# so this will run a lot faster. this will be of fixed size.
n_reference_sequences = [100]
# since our database is smaller, we can work with some slightly
# larger numbers of sequences.
n_query_sequences = [1, 5, 10, 15]

local_alignment_search_runtimes = tabulate_local_alignment_search_runtime(queries, reference_db,
n_query_sequences, n_reference_sequences,
local_alignment_search)
local_alignment_search_runtimes

Out[14]:
Number of query seqs Number of reference seqs Median query seq length Median reference seq length Runtime (s)
0 1.0 100.0 200.0 1426.0 0.094794
1 1.0 100.0 200.0 1443.0 0.086589
2 1.0 100.0 200.0 1430.0 0.087868
3 5.0 100.0 200.0 1442.5 0.538399
4 5.0 100.0 200.0 1410.5 0.452072
5 5.0 100.0 200.0 1435.0 0.443922
6 10.0 100.0 200.0 1433.0 0.886210
7 10.0 100.0 200.0 1414.5 0.873787
8 10.0 100.0 200.0 1421.0 0.873409
9 15.0 100.0 200.0 1424.5 1.379481
10 15.0 100.0 200.0 1438.5 1.324903
11 15.0 100.0 200.0 1404.0 1.306165

This table shows that we've tried a few variations on number of query sequences but kept the number of reference sequences constant. The is no variance in the query sequence length, and there is a relatively small amount of variance in reference sequence length (they're all of the same order of magnitude). There is also relatively little variance in runtime for fixed numbers of query and reference sequences.

This table clearly shows that there is an increase in runtime with an increasing number of query sequences, which we'd of course expect. What we care about is how runtime is increasing as a function of number of query sequences. Let's plot runtime versus the number of query sequences to help us understand that relationship.

In [15]:
import seaborn as sns
ax = sns.regplot(x="Number of query seqs", y="Runtime (s)", data=local_alignment_search_runtimes)
ax.set_xlim(0)
ax.set_ylim(0)
ax

Out[15]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f64deb290b8>

What we see here is pretty clearly a linear relationship: $runtime \approx constant \times number\ of\ query\ sequences$. This is because as we increase the number of query sequences, we're increasing the number of pairwise alignments that we need to perform. If we have 5 queries and 10 reference sequences, we compute $5 \times 10 = 50$ pairwise alignments. If we have 10 queries and 100 reference sequences, we compute $10 \times 100 = 1000$ pairwise alignments. There are a few practical ways to reduce the runtime of a process like this.

The first seems obvious, and even silly at first: perform fewer alignments. This could be achieved in a few ways. You could reduce the number of query sequences, though this might be something a researcher is resistant to: they have some collection of unknown sequences, and they want to know what they all are. You could alternatively reduce the number of reference sequences, but you might run into the same issues there: we wouldn't want to exclude reference sequences that might provide us with useful information about our query sequences. Finally, we might be able to figure out some ways to perform fewer alignments by not searching all of the query sequences against all of the reference sequences. If we could come up with some procedure to approximate which pairwise alignments were likely to be good (i.e., high scoring) and which were likely to be bad (i.e., low scoring) that is faster than performing the pairwise alignments, we could apply that procedure and only align a pair of sequences when we expect to get a high score. That could potentially allow us to reduce the number of alignments we need to perform, and therefore the runtime of the algorithm.

Another approach to reducing the runtime of this process would be to create a faster implementation of the algorithm (though at some point that won't be possible anymore), use a faster computer, or run the process in parallel on multiple processors. All of these would be ways to reduce the runtime of the search by some factor $f$, where $new\ runtime \approx \frac{runtime}{f}$.

In practice, for a production scale sequence database search application like BLAST, we'd combine these approaches. In the next section we'll explore ways to reduce the runtime of database searching for a fixed number of query sequences and a fixed number of reference sequences by reducing the number of pairwise alignments that the search function will perform.

## 2.2.6 Heuristic algorithms¶

As mentioned above, it just takes too long to search individual query sequences against a large database. This problem also isn't going away anytime soon. While computers are getting faster (or cheaper), the size of our sequences collections are getting bigger because sequencing is getting cheaper. In fact, many people think that obtaining DNA sequences is getting cheaper faster than computers are getting cheaper. As our number of query sequences increases because we are able to obtain more for the same amount of money, or the size of our reference databases increases (because we're continuously obtaining more sequence data) this will increasing become a problem. Figures 1 and 2, respectively, illustrate that these are both real-world issues. Notice that the axes in these boths are on a log scale in both cases.

In [16]:
import IPython.display

Out[16]:

Figure 1: Genome sequencing costs.

In [17]:
import IPython.display

Out[17]:

Figure 2: Size of GenBank.

One way that we can deal with this problem is by recognizing that most of the alignments that are performed in a database search are unlikely to be very good alignments. An algorithm developer could therefore improve runtime by defining a heuristic (or a rule) that is applied to determine which reference sequences are likely to result in good alignments, and only aligning the query against those. For it to be useful, making the decision to align or not (i.e., applying the heuristic) must be much faster than actually performing the pairwise alignment. The heuristic also needs to make good choices about which reference sequences to align the query against. If the algorithm chooses to not align against a specific query, that query is ruled out as a possible result of the database search. A good heuristic for sequence homology searching therefore makes it very unlikely to exclude the best alignment(s). When thinking about heuristic algorithms in general, there are some important considerations:

1. How often does the heuristic algorithm fail to get the right answer (in our case, does it make good choices about which reference sequences to align against)?
2. How much faster is the heuristic than the "complete" approach, and is that reduction in runtime enough to justify not being guaranteed to get the best answer?

We'll now look at a few heuristics in the context of these questions.

### 2.2.6.1 Random reference sequence selection¶

Our first heuristic will be a straw man that we use as a baseline. We'll select a random $p\%$ of the reference sequences to align our query against. This will clearly result in a large decrease in the number of sequence alignments that we need to perform because we'll go from performing $R_s$ (the reference database size) sequence alignments to $p \times R_s$ sequence alignments for each query sequence $q_i$.

Here's the source code for this. You can see that we're just wrapping our local_alignment_search function in a function that samples down to $p\%$ of the reference sequences.

In [18]:
from iab.algorithms import heuristic_local_alignment_search_random
%psource heuristic_local_alignment_search_random

def heuristic_local_alignment_search_random(
queries, reference_db, p, n=5, aligner=local_pairwise_align_ssw):
k = int(p * len(reference_db))
database_subset = random.sample(reference_db, k)
return local_alignment_search(queries, database_subset, n=n, aligner=aligner)



Let's select some new queries and see how the results compare to our known taxonomies.

In [19]:
current_queries = random.sample(queries, k=10)

In [20]:
results = heuristic_local_alignment_search_random(current_queries, reference_db, p=0.10)

for q in current_queries:
print('Closest taxonomies for query %s (in order):' % q_id)
for e in results['reference taxonomy'][q_id]:
print(' ', e)
print()

Closest taxonomies for query 527625 (in order):
k__Bacteria; p__Actinobacteria; c__Thermoleophilia; o__Solirubrobacterales; f__; g__; s__
k__Bacteria; p__Actinobacteria; c__Thermoleophilia; o__Solirubrobacterales; f__Conexibacteraceae; g__; s__
k__Bacteria; p__Verrucomicrobia; c__[Pedosphaerae]; o__[Pedosphaerales]; f__; g__; s__
k__Bacteria; p__Proteobacteria; c__Deltaproteobacteria; o__Desulfuromonadales; f__Pelobacteraceae; g__; s__
k__Bacteria; p__Proteobacteria; c__Deltaproteobacteria; o__Desulfarculales; f__Desulfarculaceae; g__; s__

Closest taxonomies for query 4327028 (in order):

Closest taxonomies for query 593515 (in order):
k__Bacteria; p__Proteobacteria; c__Alphaproteobacteria; o__Rhodospirillales; f__Rhodospirillaceae; g__; s__
k__Bacteria; p__Proteobacteria; c__Alphaproteobacteria; o__; f__; g__; s__
k__Bacteria; p__Proteobacteria; c__Alphaproteobacteria; o__Rhizobiales; f__; g__; s__

Closest taxonomies for query 558595 (in order):
k__Bacteria; p__Proteobacteria; c__Gammaproteobacteria; o__Thiotrichales; f__Piscirickettsiaceae; g__; s__
k__Bacteria; p__Proteobacteria; c__Gammaproteobacteria; o__Thiotrichales; f__Piscirickettsiaceae; g__; s__
k__Bacteria; p__Proteobacteria; c__Gammaproteobacteria; o__Thiotrichales; f__Piscirickettsiaceae; g__; s__
k__Bacteria; p__Proteobacteria; c__Gammaproteobacteria; o__Thiotrichales; f__Piscirickettsiaceae; g__; s__

Closest taxonomies for query 839161 (in order):
k__Bacteria; p__Proteobacteria; c__Alphaproteobacteria; o__Rhizobiales; f__Hyphomicrobiaceae; g__Rhodoplanes; s__
k__Bacteria; p__Proteobacteria; c__Alphaproteobacteria; o__Rhodobacterales; f__Rhodobacteraceae; g__; s__
k__Bacteria; p__Proteobacteria; c__Alphaproteobacteria; o__Rhizobiales; f__Hyphomicrobiaceae; g__Rhodoplanes; s__
k__Bacteria; p__Proteobacteria; c__Alphaproteobacteria; o__Rhizobiales; f__; g__; s__
k__Bacteria; p__Proteobacteria; c__Alphaproteobacteria; o__Rhodobacterales; f__Rhodobacteraceae; g__; s__

Closest taxonomies for query 4328983 (in order):
k__Bacteria; p__Firmicutes; c__Clostridia; o__Clostridiales; f__[Mogibacteriaceae]; g__; s__
k__Bacteria; p__Firmicutes; c__Clostridia; o__Clostridiales; f__[Acidaminobacteraceae]; g__Fusibacter; s__
k__Bacteria; p__Firmicutes; c__Clostridia; o__Clostridiales; f__[Mogibacteriaceae]; g__; s__
k__Bacteria; p__Firmicutes; c__Clostridia; o__Clostridiales; f__Clostridiaceae; g__Clostridium; s__
k__Bacteria; p__Firmicutes; c__Clostridia; o__Clostridiales; f__Lachnospiraceae; g__Coprococcus; s__

Closest taxonomies for query 4386257 (in order):
k__Bacteria; p__Proteobacteria; c__Betaproteobacteria; o__Burkholderiales; f__; g__; s__
k__Bacteria; p__Proteobacteria; c__Gammaproteobacteria; o__Acidithiobacillales; f__Acidithiobacillaceae; g__Acidithiobacillus; s__
k__Bacteria; p__Proteobacteria; c__Betaproteobacteria; o__SC-I-84; f__; g__; s__
k__Bacteria; p__Proteobacteria; c__Betaproteobacteria; o__Methylophilales; f__Methylophilaceae; g__; s__
k__Bacteria; p__Proteobacteria; c__Betaproteobacteria; o__Rhodocyclales; f__Rhodocyclaceae; g__Sulfuritalea; s__

Closest taxonomies for query 19077 (in order):
k__Bacteria; p__BRC1; c__PRR-11; o__; f__; g__; s__
k__Bacteria; p__WS3; c__PRR-12; o__Sediment-1; f__; g__; s__
k__Bacteria; p__Proteobacteria; c__Alphaproteobacteria; o__Rhizobiales; f__Beijerinckiaceae; g__; s__
k__Bacteria; p__Proteobacteria; c__Alphaproteobacteria; o__Rhodospirillales; f__Rhodospirillaceae; g__; s__
k__Bacteria; p__Proteobacteria; c__Alphaproteobacteria; o__Rhizobiales; f__Methylocystaceae; g__Methylosinus; s__

Closest taxonomies for query 4308793 (in order):
k__Bacteria; p__Firmicutes; c__Clostridia; o__Clostridiales; f__[Tissierellaceae]; g__; s__
k__Bacteria; p__AC1; c__; o__; f__; g__; s__
k__Bacteria; p__Firmicutes; c__Clostridia; o__Clostridiales; f__Ruminococcaceae; g__; s__
k__Bacteria; p__Firmicutes; c__Clostridia; o__Clostridiales; f__Peptostreptococcaceae; g__Tepidibacter; s__
k__Bacteria; p__Firmicutes; c__Clostridia; o__Clostridiales; f__Ruminococcaceae; g__Ruminococcus; s__

Closest taxonomies for query 143483 (in order):
k__Bacteria; p__Firmicutes; c__Clostridia; o__Clostridiales; f__; g__; s__
k__Bacteria; p__Firmicutes; c__Clostridia; o__Clostridiales; f__; g__; s__
k__Bacteria; p__Firmicutes; c__Clostridia; o__Clostridiales; f__Lachnospiraceae; g__Blautia; s__
k__Bacteria; p__Firmicutes; c__Clostridia; o__Clostridiales; f__Lachnospiraceae; g__; s__
k__Bacteria; p__Firmicutes; c__Clostridia; o__Clostridiales; f__Lachnospiraceae; g__; s__


In [21]:
for q in current_queries:
print('Known taxonomy for query %s:\n %s' % (q_id, reference_taxonomy[q_id]))

Known taxonomy for query 527625:
k__Bacteria; p__Proteobacteria; c__Deltaproteobacteria; o__MIZ46; f__; g__; s__
Known taxonomy for query 4327028:
Known taxonomy for query 593515:
k__Bacteria; p__Proteobacteria; c__Alphaproteobacteria; o__; f__; g__; s__
Known taxonomy for query 558595:
k__Bacteria; p__Proteobacteria; c__Gammaproteobacteria; o__Thiotrichales; f__Piscirickettsiaceae; g__; s__
Known taxonomy for query 839161:
k__Bacteria; p__Proteobacteria; c__Alphaproteobacteria; o__Rickettsiales; f__Pelagibacteraceae; g__; s__
Known taxonomy for query 4328983:
k__Bacteria; p__Firmicutes; c__Clostridia; o__Clostridiales; f__[Acidaminobacteraceae]; g__; s__
Known taxonomy for query 4386257:
k__Bacteria; p__Proteobacteria; c__Betaproteobacteria; o__Ellin6067; f__; g__; s__
Known taxonomy for query 19077:
k__Bacteria; p__BRC1; c__PRR-11; o__; f__; g__; s__
Known taxonomy for query 4308793:
k__Bacteria; p__Fusobacteria; c__Fusobacteriia; o__Fusobacteriales; f__Fusobacteriaceae; g__Fusobacterium; s__
Known taxonomy for query 143483:
k__Bacteria; p__Firmicutes; c__Clostridia; o__Clostridiales; f__Lachnospiraceae; g__; s__


What we need now is a way to know how often we get the "right answer", and how long this heuristic algorithm takes relative to the complete algorithm. We therefore first need to define what the "right answer" is. How about this: if the most common taxonomy assignment resulting from the database search at taxonomy_levels levels of taxonomy (i.e., how deep or specific our assignment is) matches the known taxonomy, then our algorithm has achieved the right answer. We can vary taxonomy_levels to see how the different heuristics perform at different levels.

Here's what this would look like:

In [22]:
import collections

def evaluate_search(queries, reference_db, reference_taxonomy, search_function, taxonomy_levels, n=5, aligner=local_pairwise_align_ssw):
start_time = time.time()
search_results = search_function(current_queries, reference_db, n=n, aligner=aligner)
stop_time = time.time()
runtime = stop_time - start_time
per_query_runtime = runtime/len(queries)
data = []
indices = []
for q in queries:
indices.append(q_id)
q_known_taxonomy = tuple(reference_taxonomy[q_id].split('; ')[:taxonomy_levels])
q_observed_taxonomies = collections.Counter()
for e in search_results['reference taxonomy'][q_id]:
q_observed_taxonomies[tuple(e.split('; ')[:taxonomy_levels])] += 1
q_observed_taxonomy = q_observed_taxonomies.most_common()[0][0]
data.append((q_known_taxonomy, q_observed_taxonomy))
index = pd.Index(indices, name='Query ID')
data = pd.DataFrame(data, index=index, columns=['Known taxonomy', 'Observed taxonomy'])
number_correct = np.sum(data['Known taxonomy'] == data['Observed taxonomy'])
fraction_correct = number_correct / data.shape[0]
return per_query_runtime, fraction_correct, data


First let's see how this works for our full database search algorithm. What's the runtime, and how often do we get the correct answer? We'll start with five levels of taxonomy (which corresponds to the family level). This step will take a couple of minutes to run, because it's doing the full database search.

In [23]:
taxonomy_levels = 5

In [24]:
runtime, fraction_correct, data = evaluate_search(current_queries, reference_db, reference_taxonomy,
local_alignment_search, taxonomy_levels=taxonomy_levels)
print('%1.2f seconds per query sequence' % runtime)
print('%1.2f%% correct answers' % (fraction_correct * 100.0))
print('Result details:')
for q_id in data.index:
print(q_id)
print(' ', data['Known taxonomy'][q_id])
print(' ', data['Observed taxonomy'][q_id])
print()

4.60 seconds per query sequence
Result details:
527625
('k__Bacteria', 'p__Proteobacteria', 'c__Deltaproteobacteria', 'o__MIZ46', 'f__')
('k__Bacteria', 'p__Proteobacteria', 'c__Deltaproteobacteria', 'o__MIZ46', 'f__')

4327028

593515
('k__Bacteria', 'p__Proteobacteria', 'c__Alphaproteobacteria', 'o__', 'f__')
('k__Bacteria', 'p__Proteobacteria', 'c__Alphaproteobacteria', 'o__Rhodospirillales', 'f__Rhodospirillaceae')

558595
('k__Bacteria', 'p__Proteobacteria', 'c__Gammaproteobacteria', 'o__Thiotrichales', 'f__Piscirickettsiaceae')
('k__Bacteria', 'p__Proteobacteria', 'c__Gammaproteobacteria', 'o__Thiotrichales', 'f__Piscirickettsiaceae')

839161
('k__Bacteria', 'p__Proteobacteria', 'c__Alphaproteobacteria', 'o__Rickettsiales', 'f__Pelagibacteraceae')
('k__Bacteria', 'p__Proteobacteria', 'c__Alphaproteobacteria', 'o__Rhodospirillales', 'f__Rhodospirillaceae')

4328983
('k__Bacteria', 'p__Firmicutes', 'c__Clostridia', 'o__Clostridiales', 'f__[Acidaminobacteraceae]')
('k__Bacteria', 'p__Firmicutes', 'c__Clostridia', 'o__Clostridiales', 'f__[Mogibacteriaceae]')

4386257
('k__Bacteria', 'p__Proteobacteria', 'c__Betaproteobacteria', 'o__Ellin6067', 'f__')
('k__Bacteria', 'p__Proteobacteria', 'c__Betaproteobacteria', 'o__Ellin6067', 'f__')

19077
('k__Bacteria', 'p__BRC1', 'c__PRR-11', 'o__', 'f__')
('k__Bacteria', 'p__WS3', 'c__PRR-12', 'o__Sediment-1', 'f__')

4308793
('k__Bacteria', 'p__Fusobacteria', 'c__Fusobacteriia', 'o__Fusobacteriales', 'f__Fusobacteriaceae')
('k__Bacteria', 'p__Fusobacteria', 'c__Fusobacteriia', 'o__Fusobacteriales', 'f__Fusobacteriaceae')

143483
('k__Bacteria', 'p__Firmicutes', 'c__Clostridia', 'o__Clostridiales', 'f__Lachnospiraceae')
('k__Bacteria', 'p__Firmicutes', 'c__Clostridia', 'o__Clostridiales', 'f__Lachnospiraceae')



Next let's see how this compare to our random heuristic search algorithm. Try running this a few times, as you might get different answers due to different random selections of the database.

In [25]:
import functools

heuristic_local_alignment_search_random_10 = functools.partial(heuristic_local_alignment_search_random, p=0.10)

runtime, fraction_correct, data = evaluate_search(current_queries, reference_db, reference_taxonomy,
heuristic_local_alignment_search_random_10, taxonomy_levels=taxonomy_levels)

print('%1.2f seconds per query sequence' % runtime)
print('%1.2f%% correct answers' % (fraction_correct * 100.0))
print('Result details:')
for q_id in data.index:
print(q_id)
print(' ', data['Known taxonomy'][q_id])
print(' ', data['Observed taxonomy'][q_id])
print()

0.46 seconds per query sequence
Result details:
527625
('k__Bacteria', 'p__Proteobacteria', 'c__Deltaproteobacteria', 'o__MIZ46', 'f__')
('k__Bacteria', 'p__Proteobacteria', 'c__Deltaproteobacteria', 'o__NB1-j', 'f__NB1-i')

4327028

593515
('k__Bacteria', 'p__Proteobacteria', 'c__Alphaproteobacteria', 'o__', 'f__')

558595
('k__Bacteria', 'p__Proteobacteria', 'c__Gammaproteobacteria', 'o__Thiotrichales', 'f__Piscirickettsiaceae')

839161
('k__Bacteria', 'p__Proteobacteria', 'c__Alphaproteobacteria', 'o__Rickettsiales', 'f__Pelagibacteraceae')
('k__Bacteria', 'p__Proteobacteria', 'c__Alphaproteobacteria', 'o__Rhodospirillales', 'f__Rhodospirillaceae')

4328983
('k__Bacteria', 'p__Firmicutes', 'c__Clostridia', 'o__Clostridiales', 'f__[Acidaminobacteraceae]')
('k__Bacteria', 'p__Firmicutes', 'c__Clostridia', 'o__Clostridiales', 'f__')

4386257
('k__Bacteria', 'p__Proteobacteria', 'c__Betaproteobacteria', 'o__Ellin6067', 'f__')
('k__Bacteria', 'p__Proteobacteria', 'c__Betaproteobacteria', 'o__Methylophilales', 'f__')

19077
('k__Bacteria', 'p__BRC1', 'c__PRR-11', 'o__', 'f__')

4308793
('k__Bacteria', 'p__Fusobacteria', 'c__Fusobacteriia', 'o__Fusobacteriales', 'f__Fusobacteriaceae')
('k__Bacteria', 'p__Firmicutes', 'c__Clostridia', 'o__Clostridiales', 'f__Ruminococcaceae')

143483
('k__Bacteria', 'p__Firmicutes', 'c__Clostridia', 'o__Clostridiales', 'f__Lachnospiraceae')
('k__Bacteria', 'p__Firmicutes', 'c__Clostridia', 'o__Clostridiales', 'f__Lachnospiraceae')



Again, what's the runtime, and how often do we get the correct answer? Based on comparison to the full search, what do you think: is this a good heuristic?

After performing many trials of the above searches, I get the correct genus-level assignment about half as often with the random reference database heuristic relative to the full database search. Your results might differ from that due to differences in the random selection of query and reference sequences. Try running all the cells in this section a few times.

Go back to the beginning of this section and try running this check based on fewer levels of taxonomy (i.e., decreased taxonomic specificity, such as the phylum) and on more levels of taxonomy (i.e., increased taxonomic specificity, such as the species level). How does that impact how often we get the right answer?

### 2.2.6.2 Composition-based reference sequence collection¶

While the random selection of database sequences can vastly reduce the runtime for database searching, we don't get the right answer very often. Let's try some heuristics that are a bit smarter. How about this: if the overall nucleotide composition of a query sequence is very different than the overall nucleotide composition of a reference sequence, it's unlikely that the best alignment will result from that pairwise alignment, so don't align the query to that reference sequence. Given that, how do we define "overall nucleotide composition" in a useful way?

#### 2.2.6.2.1 GC content¶

One metric of sequence composition that we can compute quickly (because remember, this has to be a lot faster than computing the alignment for it to be worth it) is GC content. Let's define a heuristic that only performs a pairwise alignment for the reference sequences that have the most similar GC content to the query sequence. The number of alignments that we'll perform will be defined as database_subset_size.

In [26]:
database_subset_size = 500

In [27]:
from iab.algorithms import heuristic_local_alignment_search_gc

%psource heuristic_local_alignment_search_gc

def heuristic_local_alignment_search_gc(
queries, reference_db, database_subset_size, n=5,
reference_db_gc_contents=None,
aligner=local_pairwise_align_ssw):
results = []
if reference_db_gc_contents is None:
reference_db_gc_contents = \
{r.metadata['id'] : r.gc_content() for r in reference_db}
for q in queries:
query_gc_content = q.gc_content()
database_subset = []
for r in reference_db:
# find the difference in GC content between the reference and
# query. we'll sort and select our reference sequences by this
# value
database_subset.append((abs(ref_gc_content - query_gc_content), r))
database_subset.sort(key=lambda x: x[0])
database_subset = [e[1] for e in database_subset[:database_subset_size]]
results.append(local_alignment_search(
[q], database_subset, n=n, aligner=aligner))
return pd.concat(results)



If we run our queries again, how often do we get the right answer? How much did we reduce runtime? Do you think this is a better or worse heuristic?

In [28]:
heuristic_local_alignment_search_gc_2 = functools.partial(heuristic_local_alignment_search_gc, database_subset_size=database_subset_size)

runtime, fraction_correct, data = evaluate_search(current_queries, reference_db, reference_taxonomy,
heuristic_local_alignment_search_gc_2, taxonomy_levels=taxonomy_levels)

print('%1.2f seconds per query sequence' % runtime)
print('%1.2f%% correct answers' % (fraction_correct * 100.0))
print('Result details:')
for q_id in data.index:
print(q_id)
print(' ', data['Known taxonomy'][q_id])
print(' ', data['Observed taxonomy'][q_id])
print()

0.51 seconds per query sequence
Result details:
527625
('k__Bacteria', 'p__Proteobacteria', 'c__Deltaproteobacteria', 'o__MIZ46', 'f__')
('k__Bacteria', 'p__Proteobacteria', 'c__Deltaproteobacteria', 'o__MIZ46', 'f__')

4327028
('k__Bacteria', 'p__Proteobacteria', 'c__Gammaproteobacteria', 'o__Oceanospirillales', 'f__SUP05')

593515
('k__Bacteria', 'p__Proteobacteria', 'c__Alphaproteobacteria', 'o__', 'f__')

558595
('k__Bacteria', 'p__Proteobacteria', 'c__Gammaproteobacteria', 'o__Thiotrichales', 'f__Piscirickettsiaceae')
('k__Bacteria', 'p__Proteobacteria', 'c__Gammaproteobacteria', 'o__Thiotrichales', 'f__Piscirickettsiaceae')

839161
('k__Bacteria', 'p__Proteobacteria', 'c__Alphaproteobacteria', 'o__Rickettsiales', 'f__Pelagibacteraceae')
('k__Bacteria', 'p__Proteobacteria', 'c__Alphaproteobacteria', 'o__Rhizobiales', 'f__Hyphomicrobiaceae')

4328983
('k__Bacteria', 'p__Firmicutes', 'c__Clostridia', 'o__Clostridiales', 'f__[Acidaminobacteraceae]')
('k__Bacteria', 'p__Firmicutes', 'c__Clostridia', 'o__Clostridiales', 'f__[Mogibacteriaceae]')

4386257
('k__Bacteria', 'p__Proteobacteria', 'c__Betaproteobacteria', 'o__Ellin6067', 'f__')
('k__Bacteria', 'p__Proteobacteria', 'c__Betaproteobacteria', 'o__Rhodocyclales', 'f__Rhodocyclaceae')

19077
('k__Bacteria', 'p__BRC1', 'c__PRR-11', 'o__', 'f__')
('k__Bacteria', 'p__WS3', 'c__PRR-12', 'o__Sediment-1', 'f__')

4308793
('k__Bacteria', 'p__Fusobacteria', 'c__Fusobacteriia', 'o__Fusobacteriales', 'f__Fusobacteriaceae')
('k__Bacteria', 'p__Fusobacteria', 'c__Fusobacteriia', 'o__Fusobacteriales', 'f__Fusobacteriaceae')

143483
('k__Bacteria', 'p__Firmicutes', 'c__Clostridia', 'o__Clostridiales', 'f__Lachnospiraceae')
('k__Bacteria', 'p__Firmicutes', 'c__Clostridia', 'o__Clostridiales', 'f__')



Try increasing and decreasing the number of sequences we'll align by increasing or decreasing database_subset_size. How does this impact the runtime and fraction of time that we get the correct answer?

#### 2.2.6.2.2 kmer content¶

Another metric of sequence composition is kmer composition. A kmer is simply a word (or list of adjacent characters) in a sequence of length k. Here are the kmer frequencies in a short DNA sequence. The overlap=True parameter here means that our kmers can overlap one another.

In [29]:
skbio.DNA('ACCGTGACCAGTTACCAGTTTGACCAA').kmer_frequencies(k=5, overlap=True)

Out[29]:
{'ACCAA': 1,
'ACCAG': 2,
'ACCGT': 1,
'AGTTA': 1,
'AGTTT': 1,
'CAGTT': 2,
'CCAGT': 2,
'CCGTG': 1,
'CGTGA': 1,
'GACCA': 2,
'GTGAC': 1,
'GTTAC': 1,
'GTTTG': 1,
'TACCA': 1,
'TGACC': 2,
'TTACC': 1,
'TTGAC': 1,
'TTTGA': 1}

In our next heuristic, we'll only align our query to the reference sequences with the largest fraction of the kmers that are observed in the query sequence are also present in the reference sequence. This makes a lot of sense to use as an alignment heuristic: we're only aligning sequences when it looks like they'll have multiple length-k stretches of nucleotides that are not interrupted by substitutions or insertion/deletion mutations.

Here's the source code:

In [30]:
from iab.algorithms import heuristic_local_alignment_search_kmers

%psource heuristic_local_alignment_search_kmers

def heuristic_local_alignment_search_kmers(
queries, reference_db, database_subset_size, k, n=5,
reference_db_kmer_frequencies=None,
aligner=local_pairwise_align_ssw):
results = []
if reference_db_kmer_frequencies is None:
reference_db_kmer_frequencies = \
{r.metadata['id'] : r.kmer_frequencies(k=k, overlap=True) for r in reference_db}
for q in queries:
query_kmer_frequency = q.kmer_frequencies(k=k, overlap=True)
database_subset = []
for r in reference_db:
s = fraction_shared_kmers(query_kmer_frequency, ref_kmer_frequency)
database_subset.append((s, r))
database_subset.sort(key=lambda x: x[0], reverse=True)
database_subset = [e[1] for e in database_subset[:database_subset_size]]
results.append(local_alignment_search(
[q], database_subset, n=n, aligner=aligner))
return pd.concat(results)


In [31]:
k = 7


Let's apply this and see how it does. How does the runtime and fraction of correct assignments compare to our GC content-based search and our full database search?

In [32]:
heuristic_local_alignment_search_kmers_50 = \
functools.partial(heuristic_local_alignment_search_kmers, k=k, database_subset_size=database_subset_size)

runtime, fraction_correct, data = evaluate_search(current_queries, reference_db, reference_taxonomy,
heuristic_local_alignment_search_kmers_50,
taxonomy_levels=taxonomy_levels)

print('%1.2f seconds per query sequence' % runtime)
print('%1.2f%% correct answers' % (fraction_correct * 100.0))
print('Result details:')
for q_id in data.index:
print(q_id)
print(' ', data['Known taxonomy'][q_id])
print(' ', data['Observed taxonomy'][q_id])
print()

7.12 seconds per query sequence
Result details:
527625
('k__Bacteria', 'p__Proteobacteria', 'c__Deltaproteobacteria', 'o__MIZ46', 'f__')
('k__Bacteria', 'p__Proteobacteria', 'c__Deltaproteobacteria', 'o__MIZ46', 'f__')

4327028

593515
('k__Bacteria', 'p__Proteobacteria', 'c__Alphaproteobacteria', 'o__', 'f__')
('k__Bacteria', 'p__Proteobacteria', 'c__Alphaproteobacteria', 'o__Rhodospirillales', 'f__Rhodospirillaceae')

558595
('k__Bacteria', 'p__Proteobacteria', 'c__Gammaproteobacteria', 'o__Thiotrichales', 'f__Piscirickettsiaceae')
('k__Bacteria', 'p__Proteobacteria', 'c__Gammaproteobacteria', 'o__Thiotrichales', 'f__Piscirickettsiaceae')

839161
('k__Bacteria', 'p__Proteobacteria', 'c__Alphaproteobacteria', 'o__Rickettsiales', 'f__Pelagibacteraceae')
('k__Bacteria', 'p__Proteobacteria', 'c__Alphaproteobacteria', 'o__Rhodospirillales', 'f__Rhodospirillaceae')

4328983
('k__Bacteria', 'p__Firmicutes', 'c__Clostridia', 'o__Clostridiales', 'f__[Acidaminobacteraceae]')
('k__Bacteria', 'p__Firmicutes', 'c__Clostridia', 'o__Clostridiales', 'f__[Mogibacteriaceae]')

4386257
('k__Bacteria', 'p__Proteobacteria', 'c__Betaproteobacteria', 'o__Ellin6067', 'f__')
('k__Bacteria', 'p__Proteobacteria', 'c__Betaproteobacteria', 'o__Ellin6067', 'f__')

19077
('k__Bacteria', 'p__BRC1', 'c__PRR-11', 'o__', 'f__')
('k__Bacteria', 'p__WS3', 'c__PRR-12', 'o__Sediment-1', 'f__')

4308793
('k__Bacteria', 'p__Fusobacteria', 'c__Fusobacteriia', 'o__Fusobacteriales', 'f__Fusobacteriaceae')
('k__Bacteria', 'p__Fusobacteria', 'c__Fusobacteriia', 'o__Fusobacteriales', 'f__Fusobacteriaceae')

143483
('k__Bacteria', 'p__Firmicutes', 'c__Clostridia', 'o__Clostridiales', 'f__Lachnospiraceae')
('k__Bacteria', 'p__Firmicutes', 'c__Clostridia', 'o__Clostridiales', 'f__Lachnospiraceae')



#### 2.2.6.2.3 Further optimizing composition-based approaches by pre-computing reference database information¶

One important feature of composition-based approaches is that, because the reference database doesn't change very often, we can pre-compute features of the reference sequences and re-use this. This can help us to vastly decrease the runtime of our heuristic searches. For example, the computation of all of the reference database kmer frequencies is a lot of work. If we can compute that outside of our database search, we can avoid doing that step for every database search, and therefore remove that computationally expensive (i.e., slow) step of the process.

Here we'll compute all of the reference database kmer frequencies. Notice that this step takes about a minute to complete. This is a minute of compute time that we can save on every database search!

In [33]:
reference_db_kmer_frequencies = {r.metadata['id']: r.kmer_frequencies(k=k, overlap=True) for r in reference_db}


We'll now pass our pre-computed kmer frequencies into our search function. How does the runtime and accuracy of this search compare to the searches above? This last database search that we've implemented here is very similar to how BLAST works.

In [34]:
heuristic_local_alignment_search_kmers_50 = \
functools.partial(heuristic_local_alignment_search_kmers, reference_db_kmer_frequencies=reference_db_kmer_frequencies,
k=k, database_subset_size=database_subset_size)

runtime, fraction_correct, data = evaluate_search(current_queries, reference_db, reference_taxonomy,
heuristic_local_alignment_search_kmers_50,
taxonomy_levels=taxonomy_levels)

print('%1.2f seconds per query sequence' % runtime)
print('%1.2f%% correct answers' % (fraction_correct * 100.0))
print('Result details:')
for q_id in data.index:
print(q_id)
print(' ', data['Known taxonomy'][q_id])
print(' ', data['Observed taxonomy'][q_id])
print()

1.02 seconds per query sequence
Result details:
527625
('k__Bacteria', 'p__Proteobacteria', 'c__Deltaproteobacteria', 'o__MIZ46', 'f__')
('k__Bacteria', 'p__Proteobacteria', 'c__Deltaproteobacteria', 'o__MIZ46', 'f__')

4327028

593515
('k__Bacteria', 'p__Proteobacteria', 'c__Alphaproteobacteria', 'o__', 'f__')
('k__Bacteria', 'p__Proteobacteria', 'c__Alphaproteobacteria', 'o__Rhodospirillales', 'f__Rhodospirillaceae')

558595
('k__Bacteria', 'p__Proteobacteria', 'c__Gammaproteobacteria', 'o__Thiotrichales', 'f__Piscirickettsiaceae')
('k__Bacteria', 'p__Proteobacteria', 'c__Gammaproteobacteria', 'o__Thiotrichales', 'f__Piscirickettsiaceae')

839161
('k__Bacteria', 'p__Proteobacteria', 'c__Alphaproteobacteria', 'o__Rickettsiales', 'f__Pelagibacteraceae')
('k__Bacteria', 'p__Proteobacteria', 'c__Alphaproteobacteria', 'o__Rhodospirillales', 'f__Rhodospirillaceae')

4328983
('k__Bacteria', 'p__Firmicutes', 'c__Clostridia', 'o__Clostridiales', 'f__[Acidaminobacteraceae]')
('k__Bacteria', 'p__Firmicutes', 'c__Clostridia', 'o__Clostridiales', 'f__[Mogibacteriaceae]')

4386257
('k__Bacteria', 'p__Proteobacteria', 'c__Betaproteobacteria', 'o__Ellin6067', 'f__')
('k__Bacteria', 'p__Proteobacteria', 'c__Betaproteobacteria', 'o__Ellin6067', 'f__')

19077
('k__Bacteria', 'p__BRC1', 'c__PRR-11', 'o__', 'f__')
('k__Bacteria', 'p__WS3', 'c__PRR-12', 'o__Sediment-1', 'f__')

4308793
('k__Bacteria', 'p__Fusobacteria', 'c__Fusobacteriia', 'o__Fusobacteriales', 'f__Fusobacteriaceae')
('k__Bacteria', 'p__Fusobacteria', 'c__Fusobacteriia', 'o__Fusobacteriales', 'f__Fusobacteriaceae')

143483
('k__Bacteria', 'p__Firmicutes', 'c__Clostridia', 'o__Clostridiales', 'f__Lachnospiraceae')
('k__Bacteria', 'p__Firmicutes', 'c__Clostridia', 'o__Clostridiales', 'f__Lachnospiraceae')



## 2.2.7 Determining the statistical significance of a pairwise alignment¶

One thing you may have noticed is that the score you get back for a pairwise alignment is hard to interpret. It's dependent on the query and reference sequence lengths (and possibly their composition, depending on your substitution matrix). So an important question is how good a given pairwise alignment is. Here we'll learn about a statistical approach for answering that.

### 2.2.7.1 Metrics of alignment quality¶

In the examples above, we compared features such as how long the alignment is (relevant for local but not global alignment), the pairwise similarity between the aligned query and reference, and the score. If you've used a system like BLAST, you'll know that there are other values that are often reported about an alignment, like the number of substitutions, or the number of insertion/deletion (or gap) positions. None of these metrics are useful on their own. Let's look at an example to see why.

Imagine we're aligning these two sequences:

GAAGCAGCAC
GAACAGAAC

If we tell our search algorithm that we're interested in the alignment with the fewest number of substitutions, the following alignment would get us zero substitutions, but there are a lot of bases that look homologous which are not aligned.

GAAGCAGCAC-----
GAA------CAGAAC

On the other hand, if we want to find the alignment with the fewest number of gaps, this one would get us that result, but we now have a lot of substitution events, and some regions that clearly look misaligned (such as the CAG sequence in the middle of both).

GAAGCAGCAC
GAACAGA-AC

The alignment score that has been reported by our pairwise aligners helps us to balance these different features, and we can adjust the scoring scheme to weight things differently (e.g., so that gaps are penalized more or less than certain substitutions). The problem is that the scores are hard to interpret, particularly when we have only one or a few of them.

### 2.2.7.2 False positives, false negatives, p-values, and alpha¶

Remember that an alignment of a pair of sequences represents a hypothesis about homology between those sequences. One way that we think about determining if an alignment is good or not is to ask: what fraction of the time would I obtain a score at least this good if my sequences are not homologous? This fraction is usually referred to as our p-value, and this is computed in many different ways. If our p-value is high (e.g., 25%), then our alignment is probably not very good since it means that many non-homologous pairs of sequences would achieve a score at least that high. If our p-value is low (say 0.001%), then our alignment is probably good since scores that high are achieved only infrequently.

Our threshold for defining what we consider to be a high versus low p-value is dependent on how often we're willing to be wrong. We would set this value, which is usually referred to as $\alpha$, to some fraction, and if our p-value is less than $\alpha$, we say that the alignment is statistically significant. If our p-value is greater than $\alpha$, we say that our alignment is not statistically significant.

There are a couple of ways that we could be wrong when we do sequence homology searching, and we need to consider these when we determine what value we want to define as $\alpha$. First, we could say a pair of sequences are homologous when they're not, which would be a false positive or a type 1 error. Or, we could say that a pair of sequences are not homologous when they are, which would be a false negative, or a type 2 error.

If incurring a false positive about 5% of the time is acceptable (i.e., you're ok with calling a pair of sequences homologous when they actually are not about one in twenty times) then you'd set your $\alpha$ to 0.05. Setting $\alpha$ to a value this high likely means that the method will err on the side of false positives, and only infrequently will it say that a pair of sequences are not homologous when they actually are (i.e., achieve a false negative). If $\alpha$ were set to be very low on the other hand (say, $1 \times 10^{-50}$), then you will err on the side of false negatives. Only infrequently will you say that a pair of non-homologous sequences are homologous, but you might call many pairs of homologous sequences non-homologous. You should think of $\alpha$ as a dial. If you turn the dial toward higher values, you'll increase your false positive rate and decrease your false negative rate. If you turn the dial toward lower values, you'll decrease your false positive rate and increase your false negative rate.

There is not a hard-and-fast rule for whether false positives or false negatives are better, which makes choosing $\alpha$ hard. It's application specific, so you need to understand the biological question your answering when making this decision, and the ramifications of false positives versus false negatives. In general, when might you prefer to have false positives? When might you prefer to have false negatives?

### 2.2.7.3 Interpreting alignment scores in context¶

In this section, we are going to learn about how to interpret alignment scores by empirically determining if a pairwise alignment that we obtain is better than we would expect if the pair of sequences were working with were definitely not homologous. For a given pair of sequences that we want to align, we're first going to align them and compute the score of the alignment. We're then going to align many pairs of sequences that are similar to the query and reference, but that we know are not homologous. We'll do this by shuffling or randomizing the order of the bases in the query sequences, and performing another pairwise alignment.

First, we'll define a function that can generate random sequences for us. This will take a scikit-bio sequence object (either skbio.DNA, skbio.RNA, or skbio.Protein) and a length, and it will randomly generate a sequence of that type and length for us.

In [35]:
import random
def random_sequence(moltype, length):
result = []
alphabet = list(moltype.nondegenerate_chars)
for e in range(length):
result.append(random.choice(alphabet))
return moltype(''.join(result))


We can now run this a few times to generate some random sequences:

In [36]:
random_sequence(skbio.DNA, 50)

Out[36]:
DNA
--------------------------------------------------------
Stats:
length: 50
has gaps: False
has degenerates: False
has definites: True
GC-content: 56.00%
--------------------------------------------------------
0 GGGAGCCTTC CAAACGCTTG AGTATAGACT ACTCCTCCGC GTGCCGCTTT
In [37]:
random_sequence(skbio.DNA, 50)

Out[37]:
DNA
--------------------------------------------------------
Stats:
length: 50
has gaps: False
has degenerates: False
has definites: True
GC-content: 34.00%
--------------------------------------------------------
0 CAAATAGCCA CGGAAATTAG CAAGATAAGG ATAGGTATTA TAAATCAGCT

Next, we need a function that will shuffle the characters in a sequence, and give us a new sequence back. We'll use this to generate a sequence that is similar (in length and composition) to our input sequence, but which we know is not homologous. We'll use Pythons random.shuffle function, which randomly re-orders the order of the elements in a sequence, but keeps the composition and length of the sequence the same.

In [38]:
from iab.algorithms import shuffle_sequence
%psource shuffle_sequence

def shuffle_sequence(sequence):
# generate a list of the position indices (numbers) in sequence
randomized_order = list(range(len(sequence)))
# randomly rearrange the order of that list
random.shuffle(randomized_order)
# return a new sequence, where the positions are shuffled
return sequence[randomized_order]



Now we can define a random sequence and shuffle it. Notice how the sequences are different (in their order), but their compositions (e.g., length and GC content) are the same. Shuffling will change the order of the bases, but it won't change the frequency at which each base is present - it's exactly analogous to shuffling a deck of cards.

In [39]:
seq = random_sequence(skbio.DNA, 50)
seq

Out[39]:
DNA
--------------------------------------------------------
Stats:
length: 50
has gaps: False
has degenerates: False
has definites: True
GC-content: 56.00%
--------------------------------------------------------
0 GAAAACAGGT AACACGGATT CGCGGACCAA GGATTCGAAC GTCCCGGCTC
In [40]:
shuffle_sequence(seq)

Out[40]:
DNA
--------------------------------------------------------
Stats:
length: 50
has gaps: False
has degenerates: False
has definites: True
GC-content: 56.00%
--------------------------------------------------------
0 ACCGCCAGAA CACAATGACA TCATCGGGAG TCCGAGCGGC AGCGAATGTT

Let's generate a random query sequence and align it against itself to see what that score would be.

In [41]:
query_seq = random_sequence(skbio.DNA, 50)
_, actual_score, _ = local_pairwise_align_ssw(query_seq, query_seq)
print("Score: %1.2f" % actual_score)

Score: 100.00


Next let's generate 99 random variants of that sequence with shuffle_sequence and compute the pairwise alignment for each of those variants against the query sequence. We'll then look at the distribution of those scores.

In [42]:
from iab.algorithms import generate_random_score_distribution
%psource generate_random_score_distribution

def generate_random_score_distribution(sequence1,
sequence2,
n=99,
aligner=local_pairwise_align_ssw):
scores = []
# iterate n times
for i in range(n):
# generate a randomized version of the first sequence
random_sequence = shuffle_sequence(sequence1)
# align that randomized sequence against the second sequence
# and save its score
_, score, _ = aligner(random_sequence, sequence2)
scores.append(score)
# return the n randomized alignment scores
return scores


In [43]:
random_scores = generate_random_score_distribution(query_seq, query_seq, 99)
print(random_scores)

[10, 16, 14, 12, 11, 10, 13, 12, 10, 10, 14, 9, 12, 11, 10, 9, 10, 11, 10, 10, 15, 12, 12, 11, 10, 14, 10, 12, 11, 13, 13, 10, 9, 10, 14, 16, 13, 11, 14, 10, 12, 10, 12, 13, 12, 14, 10, 10, 8, 12, 10, 11, 11, 8, 10, 12, 10, 14, 11, 10, 11, 11, 15, 9, 15, 11, 8, 10, 9, 10, 11, 14, 11, 18, 12, 10, 10, 9, 12, 10, 14, 14, 13, 10, 11, 13, 14, 12, 10, 12, 8, 12, 19, 9, 10, 12, 10, 14, 11]


How does the actual score of aligning the sequence to itself compare to the score of aligning it to many similar but non-homologous sequences? Let's plot these to get a better idea.

In [44]:
import seaborn as sns

def plot_score_distribution(actual_score, random_scores):
ax = sns.distplot(random_scores, kde=False, label="Random scores", color="b")
ax.plot([actual_score, actual_score], ax.get_ylim(), '--', label="Actual score")
# set the range of the x axis to be zero through 110% of the actual score
ax.set_xlim(0, actual_score + actual_score * 0.1)
ax.legend(loc=9, fontsize='large')
return ax

In [45]:
plot_score_distribution(actual_score, random_scores)

Out[45]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f64a50837f0>

What does this tell us about our alignment score and therefore about our alignment? Is it good or bad?

We finally have information that we can use to evaluate an alignment score, and therefore to evaluate the quality of an alignment. Let's use this information to quantify the quality of the alignment by computing a p-value. As we described above, this is simply the probability that we would obtain an alignment score at least this good if the sequences being aligned are not homologous. Since we have a lot of scores now from sequences that are similar but not homologous, if we just count how many are at least as high as our actual score and divide by the number of scores we compute, that is an empirical (data-driven) way of determining our p-value.

To determine if our alignment is statistically significant, we need to define $\alpha$ before computing the p-value so the p-value does not impact our choice of $\alpha$. Let's define $\alpha$ as 0.05. This choice means if we obtain a p-value less than 0.05 we will consider the alignment statistically significant and accept the hypothesis that the sequences are homologous.

Here's what all of this looks like:

In [46]:
from iab.algorithms import fraction_better_or_equivalent_alignments
%psource fraction_better_or_equivalent_alignments

def fraction_better_or_equivalent_alignments(sequence1,
sequence2,
n = 99,
aligner=local_pairwise_align_ssw):
# align sequence1 and sequence2 and store the score of the alignment
_, actual_score, _ = aligner(sequence1, sequence2)
# compute the distribution of randomized scores
random_scores = generate_random_score_distribution(sequence1,
sequence2,
n,
aligner=aligner)

# count the number of random scores that are at least as good as our
# actual score
count_better = 0
for s in random_scores:
if s >= actual_score:
count_better += 1
# return the number of times we observe a score at least as good as the
# random score divided by the number of scores we computed. we add one
# to the numerator and denominator to account for our actual_score
return (count_better + 1) / (n + 1)


In [47]:
print("Fraction of alignment scores at least as good as the alignment score: %r" %
fraction_better_or_equivalent_alignments(query_seq, query_seq, 99))

Fraction of alignment scores at least as good as the alignment score: 0.01


The fraction that we get back here is 0.01, which is lower than $\alpha$, so we would accept the hypothesis that our sequences are homologous.

A few notes on these empirically defined p-values. First, here's what the formula for computing this looks like:

p\ value = \frac{number\ of\ computed\ aligned\ scores\ greater\ than\ or\ equal\ to\ the\ actual\ alignment\ score}{number\ of\ alignment\ scores\ computed}

The numerator and the denominator both include the actual alignment score, so the lowest p-value that can be achieved is $\frac{1}{99 + 1}$, where the $1$ in the numerator corresponds to our actual alignment score (which is of course equal to itself), where the $99$ in the denominator is the number of permutations, and the $1$ in the denominator is a constant which corresponds the computation of the actual score. If we increase the number of permutations, say to 999, we could achieve greater precision (more significant digits) in our p-value.

In [48]:
print("Fraction of alignment scores at least as good as the alignment score: %r" %
fraction_better_or_equivalent_alignments(query_seq, query_seq, 999))

Fraction of alignment scores at least as good as the alignment score: 0.001


When we achieve the lowest possible value for a given test, as is the case here, we report the p-value as being less than that value, since we've yet to observe a random alignment score at least that high. For example, here we would report something like:

The alignment of our query and reference sequence was statistically significant, as determined by comparing our actual alignment score to random variants ($p < 0.001$).

Let's now try this for some harder cases, where the query and subject sequences are not identical. First, let's generate a longer subject sequence at random. Then, we'll create a random query sequence and compare it. Since we're doing this in two random steps, we know that these sequences are not homologous. Does the resulting p-value reflect that?

In [49]:
sequence1 = random_sequence(skbio.DNA, 250)
sequence1

Out[49]:
DNA
---------------------------------------------------------------------
Stats:
length: 250
has gaps: False
has degenerates: False
has definites: True
GC-content: 54.40%
---------------------------------------------------------------------
0   GTCCGGTGAA CCTTCGACGC TCGACACGGA CCGAGGCAAG CCAGACATGA AGATAACCAT
60  ACGCCCTCCG GCCCCACATG GTGAGAGACT AAATGCGGCC TCCCTATTCT TGCTGAAGCG
120 CCGCTAGGCC GCCAACGGGA CTTCGCCATC AAAGGTGTAC CACATTTAGT GTGCAGCCCA
180 CGGATCATAG CATTTATCAA ATGGCGCGTC AATTCCCTAT TTTATTCCAT TAACGAACGA
240 GGGAGCAGCG
In [50]:
sequence2 = random_sequence(skbio.DNA, 250)
sequence2

Out[50]:
DNA
---------------------------------------------------------------------
Stats:
length: 250
has gaps: False
has degenerates: False
has definites: True
GC-content: 46.00%
---------------------------------------------------------------------
0   AAAACTAGGC CTAGACTACT TCATCTTAGA CTCCTTGCCT GCCCCAAGTC AATGTTCTCC
60  GTGATTGCGG AACCCATCCA CTCCTCTTCC CAGATGTCTG ACAGATGATA TTATGCGCAC
120 AGTTATGAAG CCGTATTTTG CTCTCTGGCG TAGAAGACCA TCTATTTTGA ACGTACTCGC
180 CGCTCCGAAG CAAACATGAT AGCTACGAGT CTTCTCGACC AGATGGGTAT CGATAGATCT
240 ATCTACTTTT
In [51]:
print("Fraction of alignment scores at least as good as the alignment score: %r" %
fraction_better_or_equivalent_alignments(sequence1,sequence2))

Fraction of alignment scores at least as good as the alignment score: 0.89


We've now looked at two extremes: where sequences are obviously homologous (because they were the same), and where sequences are obviously not homologous (because they were both independently randomly generated). Next, we'll explore the region between these, where this gets interesting. We'll now create a partially randomized sequence to create a pair of sequences where the homology is more obscure. We'll do this again using the Python random module, but this time we'll introduce mutations only at some positions to create a pair of sequences that are approximately percent_id identical.

Let's define a function to do this, and then compute a sequence that is 95% identical to our sequence1.

In [52]:
def partially_randomize_sequence(percent_id, sequence):
result = []
for c in sequence:
if random.random() < percent_id:
result.append(str(c))
else:
# choose a base at random that is not the current base
# i.e., simulate a substitution event
result.append(choice([r for r in sequence.nondegenerate_chars if r != c]))
return sequence.__class__(''.join(result))

In [53]:
sequence1_95 = partially_randomize_sequence(0.95, sequence1)

In [54]:
sequence1

Out[54]:
DNA
---------------------------------------------------------------------
Stats:
length: 250
has gaps: False
has degenerates: False
has definites: True
GC-content: 54.40%
---------------------------------------------------------------------
0   GTCCGGTGAA CCTTCGACGC TCGACACGGA CCGAGGCAAG CCAGACATGA AGATAACCAT
60  ACGCCCTCCG GCCCCACATG GTGAGAGACT AAATGCGGCC TCCCTATTCT TGCTGAAGCG
120 CCGCTAGGCC GCCAACGGGA CTTCGCCATC AAAGGTGTAC CACATTTAGT GTGCAGCCCA
180 CGGATCATAG CATTTATCAA ATGGCGCGTC AATTCCCTAT TTTATTCCAT TAACGAACGA
240 GGGAGCAGCG
In [55]:
sequence1_95

Out[55]:
DNA
---------------------------------------------------------------------
Stats:
length: 250
has gaps: False
has degenerates: False
has definites: True
GC-content: 54.80%
---------------------------------------------------------------------
0   GTCCGGTGAA CCTTCGACGC TCGACACGGA CCGAGGCAGG CCAGACATGA AGATAACCAT
60  ACGACCTCCG GCCCCACGTG GTGAGAGACT AAATGCGGCC TCCCTATTCT TGCTGAAGCG
120 CCGCTAGGCC GCCAACGGGA CTTCGCCATC ACAGGTGTAC CACATTTACT GTGCAACCCA
180 CGGATCATAG CAGTTATCAA ATGGCGCGTC AATTACCTAT TTTATTCCAT TAACGAACGA
240 GGGAGCAGCG

Notice how these sequences are almost identical, but have some differences. Let's apply our approach to determine it would identify these sequences as being homologous bases on $\alpha = 0.05$.

In [56]:
print("Fraction of alignment scores at least as good as the alignment score: %r" %
fraction_better_or_equivalent_alignments(sequence1, sequence1_95))

Fraction of alignment scores at least as good as the alignment score: 0.01


You likely got a significant p-value there, telling you that the sequences are homologous.

Now let's simulate much more distantly related sequences by introducing substitutions at many more sites.

In [57]:
sequence1_25 = partially_randomize_sequence(0.25, sequence1)

In [58]:
sequence1

Out[58]:
DNA
---------------------------------------------------------------------
Stats:
length: 250
has gaps: False
has degenerates: False
has definites: True
GC-content: 54.40%
---------------------------------------------------------------------
0   GTCCGGTGAA CCTTCGACGC TCGACACGGA CCGAGGCAAG CCAGACATGA AGATAACCAT
60  ACGCCCTCCG GCCCCACATG GTGAGAGACT AAATGCGGCC TCCCTATTCT TGCTGAAGCG
120 CCGCTAGGCC GCCAACGGGA CTTCGCCATC AAAGGTGTAC CACATTTAGT GTGCAGCCCA
180 CGGATCATAG CATTTATCAA ATGGCGCGTC AATTCCCTAT TTTATTCCAT TAACGAACGA
240 GGGAGCAGCG
In [59]:
sequence1_25

Out[59]:
DNA
---------------------------------------------------------------------
Stats:
length: 250
has gaps: False
has degenerates: False
has definites: True
GC-content: 44.00%
---------------------------------------------------------------------
0   GCCAAATTAA CGGACAACGC TTGACATAAA TGGTTAGTGG ATTGGTCAGA GAATGACTGA
60  ATGCCAACCT AGCTACCCCC GGTAGTAAGT ATATACGACC TACCAATTTA TACCGAAGCT
120 TGGTTGGCGC CACAGTGGGT TCTCTCGAGG AGTGCAGTAA AACAGTTCTC TTACTGGTCC
180 CGGCTAATAA ACTACAGCTA ATTCTAAATT ACTTCCCTTG TCTGTTCCGT ACAATTCAAA
240 AAGAGATCCG
In [60]:
print("Fraction of alignment scores at least as good as the alignment score: %r" %
fraction_better_or_equivalent_alignments(sequence1, sequence1_25))

Fraction of alignment scores at least as good as the alignment score: 0.44


### 2.2.7.4 Exploring the limit of detection of sequence homology searches¶

In the example above, we know that our input sequences are "homologous" because sequence1_25 and sequence1_95 are both derived from sequence1. Our method detected that homology for sequence1_95, when we simulated very closely related sequences, but not for sequence1_25, when we simulated much more distantly related sequences. This gives us an idea of the limit of detection of this method, and is a real-world problem that biologists face: as sequences are more divergent from one another, detecting homology becomes increasingly difficult.

Lets run a simulation to gain some more insight into the limit of detection of this method. We'll run this approach for pairs of sequences where we vary the percent_id parameter, and identify when our approach stops identifying sequence pairs as being homologous. This is important to know as a bioinformatician, because it tells us around what pairwise similarity we will no longer be able to identify homology using this approach.

In [61]:
# First, let's define the range of percent identities that we'll test
percent_ids = np.arange(0.0, 1.0, 0.05)
# Then, we'll define the number of random sequences we'll test at each percent identity
num_trials = 20
# Then, we'll define the sequence length that we want to work with, and num_trials random sequences
sequence_length = 150
random_sequences = [random_sequence(skbio.DNA, sequence_length) for i in range(num_trials)]

results = []

for percent_id in percent_ids:
# at each percent_id, we'll track the p-values for each trial (random sequence)
p_values = []
for sequence in random_sequences:
# partially randomize the sequence, compute its p-value, and record that p-value
sequence_at_percent_id = partially_randomize_sequence(percent_id, sequence)
p = fraction_better_or_equivalent_alignments(sequence, sequence_at_percent_id)
p_values.append(p)
results.append((percent_id, np.median(p_values), np.mean(p_values)))
pd.DataFrame(results, columns=["Percent id between query and subject",
"Median p-value", "Mean p-value"])

Out[61]:
Percent id between query and subject Median p-value Mean p-value
0 0.00 0.530 0.4765
1 0.05 0.790 0.6765
2 0.10 0.460 0.5085
3 0.15 0.650 0.6440
4 0.20 0.410 0.4555
5 0.25 0.440 0.4120
6 0.30 0.195 0.3150
7 0.35 0.375 0.3870
8 0.40 0.010 0.0810
9 0.45 0.010 0.0210
10 0.50 0.010 0.0195
11 0.55 0.010 0.0100
12 0.60 0.010 0.0100
13 0.65 0.010 0.0100
14 0.70 0.010 0.0100
15 0.75 0.010 0.0100
16 0.80 0.010 0.0100
17 0.85 0.010 0.0100
18 0.90 0.010 0.0100
19 0.95 0.010 0.0100

What does this simulation tell us about our limit of detection for homology (i.e., how similar must a pair of sequences be for us to reliably be able to identify homology between them)? Is this higher or lower than you expected?

With respect to our simulation, I took a few shortcuts here to keep the runtime low. What are some things that could be improve to make this simulation more robust, if we weren't as concerned about runtime?