scieee AI-readable full text Open interactive document viewer

Statistical models for genome sequence mapping

Valera Zorita, Eduard

Abstract

In this work we present a mapper, an algorithm to find short DNA sequences in large reference texts. Our algorithm uses the standard seed-and-extend approach, utilized by most modern mappers, combined with a novel genome annotation called neighborhood annotation. The neighborhood annotation is a data structure that contains information of similarity between sequences of the same reference. Based on this annotation, we build a statistical model to aid the processes of seeding and mapping quality estimation. Overall, our algorithm achieves higher sensitivity and more accurate estimation of mapping reliability with simulated Illumina reads, at the same speed compared to the state-of-the art algorithms. The C source code of the algorithm implementation is available at http://github.com/ezorita/mapper.

Full text

Universitat Polit`ecnica de Catalunya Statistical models for genome sequence mapping Master of Science thesis in partial Fulfillments for the Degree of Electronic Engineering at the Universitat Polit`ecnica de Catalunya Author: Eduard Valera i Zorita Advisors: Dr. Guillaume Filion Dr. Josep Vidal Manzano September 14, 2016 Contents Resum 11 Resumen 13 Abstract 15 1 DNA and sequence alignment 17 1.1 Biological information is stored using DNA . . . . . . . . . . . . . . . . . . . . . . 17 1.1.1 The molecule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.1.2 Origins of DNA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.1.3 Genes and genomes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 1.1.4 Evolution of genomes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 1.1.5 DNA sequencing technologies . . . . . . . . . . . . . . . . . . . . . . . . . . 22 1.2 Sequence alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 1.2.1 Needleman-Wunsch algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2 Finding needles in a haystack 28 2.1 Sequence mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.2 Efficient string search structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 2.2.1 Hash tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 2.2.2 Trees........................................ 31 2.2.3 Suffix arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 2.2.4 BW Transform and FM index . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2.3 The sequence neighborhood . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 2.3.1 Neighborhood annotation algorithm . . . . . . . . . . . . . . . . . . . . . . 40 2.3.2 k-mer uniqueness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 3 Mapping algorithm 44 3.1 Seeding .......................................... 45 3.1.1 MEM seeding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 3.1.2 Threshold seeding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 3.1.3 Inexact seeding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 3 3.2 Filtering significant seeds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 3.2.1 Filter on seed length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 3.2.2 Filter on number of hits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 3.2.3 Filter on seed significance . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 3.3 Aligning.......................................... 48 3.3.1 Sequence alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 3.3.2 Split reads and breakpoint algorithm . . . . . . . . . . . . . . . . . . . . . . 49 3.4 Mapping quality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 3.4.1 Alignment score model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 3.4.2 The neighbors model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 4 Results 56 5 Conclusions and future work 61 List of Figures 1.1 DNA molecule. a) DNA nucleotides are formed by a Phosphate group, a 5-carbon sugar and a nitrogenous base. b) Sugars that compose RNA/DNA differ by an OH/H group on the second carbon. c) The nitrogenous bases are Guanine, Adenine, Cytosine, Thymine (DNA only) and Uracil (RNA only). . . . . . . . . . . . . . . . 18 1.2 Structure of the DNA double helix. The nitrogenous bases hold together through hydrogen bonds: A and T with two bonds, C and G with three. The nucleotide pairs form stacks through sugar-phosphate bonds. . . . . . . . . . . . . . . . . . . 19 1.3 Single-stranded RNA molecules can form secondary structures. a) Self-annealing of complementary nucleotides from the same RNA strand. b) Representation of the secondary structure. Source [1]. . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 1.4 Human karyotype. Human cells have 23 pairs of chromosomes. Each cell contains two copies of chromosomes 1 to 22 and two copies of chromosome X (female) or one copy of chromosome X and one copy of chromosome Y (male). Source [1]. . . . 21 1.5 Organization of genes in the human genome. a) Representation of chromosome 22. b) A ten-fold expansion of a fragment of chromosome 22 with about 40 genes indicated in red. c) An expanded portion of (b) showing four genes. d) Representation of one of the genes of chromosome 22 where the regulatory region and its 9 exons are indicated. Source [1]. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 1.6 A representation of the nucleotide sequence content of the sequenced Human genome. Source[1].......................................... 23 1.7 BLOSUM62 matrix, used to score alignments between evolutionarily divergent protein sequences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 1.8 Initialization of a Needleman-Wunsch alignment between ATGCAA and ACGCTTTAA. . 25 1.9 Needleman-Wunsch matrix for q=ATGCAA and t=ACGCTTTAA (top row) and alignment path based on backtracking through valid cell transitions (bottom row). Green: match/mismatch. Red: Gap open/extend. a) Score matrix for Levenshtein distance (Pm= 0, Ps= 1, Po= 1, Pe= 1). b) Score matrix for Pm=−1, Ps= 4, Po= 6, Pe=1. ................................. 25 1.10 Smith-Waterman matrix initialization for q=ATGCAA and t=TGTGCATGGAAAGCAGCT. 26 1.11 Smith-Waterman alignment for q=ATGCAA and t=TGTGCATGGAAAGCAGCT using min-gap score model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 5 2.1 Representation of error free subsequences modeled as a stick breaking problem. The mismatched nucleotides and the longest error-free stretch are highlighted in red. 29 2.2 Seeding probability for error rate q= 0.02 and read lenghts m= 25,50,75,100,125,150 nt. 31 2.3 Process of construction of a tree structure containing all the suffixes of the text ATGAC. The position of the suffixes are stored in the leaf nodes. . . . . . . . . . . . 32 2.4 Suffix array of GATGCGAGAGATG. The numbers on the top represent the positions of the suffixes (the suffix array). Below, the suffixes sorted in lexicographical order. . 33 2.5 The preceding character of the suffixes shown in Figure 2.4. The preceding character of the first suffix is the last character of the text ($). . . . . . . . . . . . . . . . . . 34 2.6 The correspondence between the preceding character and its order of appearance in the suffix array. Note that the oder of appearance is conserved for each nucleotide set.............................................. 35 2.7 Backward search using the preceding character information. The inteval of the matching suffix is shrinked every iteration, when the preceding nucleotide is added. 36 2.8 Backward search using only the C and Occ tables. The suffix intervals are updated with (2.8) reading the query backwards. . . . . . . . . . . . . . . . . . . . . . . . . 37 2.9 Subintervals of the suffix A. The subintervals are consecutive in the suffix array. The interval of the suffix Ais denoted by IA, the subintervals are IAA,IAC,IAG,IAT and fpis the position of the first suffix of the interval. . . . . . . . . . . . . . . . . 39 2.10 Representation of the recurive block search algorithm. The first step (top-bottom) divides the intervals in two blocks. When the bottom is reached, the algorithm starts a bottom-up search and extension. . . . . . . . . . . . . . . . . . . . . . . . 43 3.1 Seed and extend algorithm. Seeding consists in finding short exact matches between the read and the reference. The extension step performs a full alignment between the read and the reference at the position of the seeds. . . . . . . . . . . . . . . . . 49 3.2 Seed and extension over a split read. The split read is composed of two distant fragments of the reference (blue and red). At the extension step, the alignment proceeds passed the fragment border, where the sequences do not coincide anymore (represented in gray on the reference). . . . . . . . . . . . . . . . . . . . . . . . . . 50 3.3 Local neighborhood of the read TR, its best match TBand the closest neighbor of the best match TN. The Lenvenshtein distance between sequences is denoted by d(·,·). ........................................... 52 3.4 On our hypothesis for the mapping score, TRoriginated from TN. This figure summarizes the three possible scenarios when a nucleotide of TRis modified. . . . 53 4.1 Sensitivity vs error rate for all the possible mapping quality thresholds. Illumina simulated reads on Human Genome v19, with 1% (left), 2% (right) and 5% (bottom) error rate. Comparison between bowtie2 (blue), bwa-mem (red) and our mapper (black). .......................................... 59 4.2 Throughput (correctly mapped sequences per core per second) vs error rate for all the possible mapping quality thresholds. Illumina simulated reads on Human Genome v19, 1% (left) and 5% (right) error rate. Comparison between bowtie2 (blue), bwa-mem (red) and our mapper (black). . . . . . . . . . . . . . . . . . . . 60 4.3 Sensitivity vs error rate for all the possible mapping quality thresholds. Illumina simulated reads on Drosophila Melanogaster genome v3, 2% (left) and 15% (right) error rate. Comparison between bowtie2 (blue), bwa-mem (red) and our mapper (black). .......................................... 60 List of Tables 1.1 Genome size and protein-coding gene count of model organisms. . . . . . . . . . . 20 1.2 DNA sequencing technologies in current use. . . . . . . . . . . . . . . . . . . . . . 23 1.3 Examples of alignment scores. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 1.4 Alignments generated with different score models for q=AAATCA and t=AAAGAATTCA. 26 3.1 Upper bound probability of incorrect seed (l= 19). . . . . . . . . . . . . . . . . . . 48 9 16 Chapter 1 DNA and sequence alignment 1.1 Biological information is stored using DNA 1.1.1 The molecule The deoxyribonucleic acid (DNA) is the molecule that provides all living organisms the ability to store, retrieve and pass from generation to generation the genetic instructions required to make and maintain a living organism. A molecule of DNA consists of two long strands of complementary poly-nucleotide chains. The building blocks which compose a chain of DNA are called nucleotides. The nucleoties are molecules with simple structure composed of a five-carbon sugar, a nitrogencontaining base and one or more phosphate groups (Figure 1.1a). The nucleotides are covalently linked together building a backbone of sugar-phosphate bonds, in which the phosphate groups that are attached to the 5th carbon of the sugars (5’) form a covalent bond with the 3rd carbon (3’) of the next nucleotide in the strand (Figure 1.2). Following this fashion, DNA nucleotides can be enchained to form an arbitrarily long DNA strands. Note that only one of the ends of a DNA molecule will have a dangling phosphate group on its backbone (the 5’ end), giving an inherent polarity to the strand. The base group is the only subunit that differs in each of the four types of nucleotides. There are four different nitrogen-containing bases that give name to their respecive nucleotide: Adenine (A), Thymine (T), Guanine (G) and Cytosine (C) (Figure 1.1c). The bases can hold together two strands of DNA through hydrogen bonds. However, they do not pair at random: the chemical structure of the bases only permits the efficient formation of hydrogen bonds in the interior of the double helix between A and T with 2 hydrogen bonds, and G and C with 3 hydrogen bonds. The latter pairing of bases is not a strict limitation, nevertheless, in terms of free energy, it’s the best combination to bring the atoms close enough to form hydrogen bonds without distorting the structure of the DNA backbone. One last additional requirement to form the double helix molecule is that the bases can only pair if the complementary strands are antiparallel, i.e. the polarities of the strands (5’ and 3’) are oriented in an opposed manner (see Figure 1.2). The discovery of the double helix structure of the DNA [2] in 1953 was crucial to answer 17 18 CHAPTER 1. DNA AND SEQUENCE ALIGNMENT Figure 1.1: DNA molecule. a) DNA nucleotides are formed by a Phosphate group, a 5-carbon sugar and a nitrogenous base. b) Sugars that compose RNA/DNA differ by an OH/H group on the second carbon. c) The nitrogenous bases are Guanine, Adenine, Cytosine, Thymine (DNA only) and Uracil (RNA only). two important questions: (i) how the information is stored in a chemical form and (ii) how this information is passed from the mother cell to its daughters. The answer to the first question is that the biological information in living organisms is encoded in the sequence of nucleotides along DNA strands. Therefore, the genetic information is analogous to a text with a 4-letter alphabet: A, C, G and T. In consequence, different organisms differ from each other because they have different nucleotide sequences. The discovery of the DNA structure also shed some light to the second question. The fact that the DNA molecule contains two strands bearing identical information rapidly suggested a mechanism of DNA replication: unwinding the two strands and filling them separately with the complementary nucleotide sequence would give rise to two new double stranded DNA molecules with the same information. 1.1.2 Origins of DNA There are two main types of nucleic acids, which differ in the type of sugar contained in their sugar-phosphate backbone (Figure 1.1b). The acid based on the sugar deoxyribose is known as deoxyribonucleic acid (DNA) and contains the bases A, G, C and T (Sec. 1.1.1). On the other hand, the acid based on sugar ribose, known as ribonucleic acid (RNA), contains the bases A, C, G and Uracil (U), see Figure. 1.1c. Despite their structural similarities, the RNA is commited to many more cellular tasks than its sister nucleic acid. For instance, RNA is usually found in cells in the form of a single-stranded molecule, whereas DNA is almost always in the form of a double 1.1. BIOLOGICAL INFORMATION IS STORED USING DNA 19 Figure 1.2: Structure of the DNA double helix. The nitrogenous bases hold together through hydrogen bonds: A and T with two bonds, C and G with three. The nucleotide pairs form stacks through sugar-phosphate bonds. stranded polynucleotide chain. Being single stranded, the RNA backbone is flexible, allowing the polymer chain to bend back on itself and form weak bonds between different parts of the same molecule. In fact, these types of internal associations form specific shapes that are dictated by its sequence (Figure 1.3). The shape of the RNA molecule, in turn, may recognize other molecules and bind to them selectively or act as a catalyst in some chemical reactions. However, besides its enzymatic roles, the RNA can also store and replicate genetic information in the same manner as DNA. Most notably, RNA works in all living organisms as an intermediate in the transfer of genetic information in the form of messenger RNA (mRNA), bearing the information and guiding the synthesis of proteins. The triple role of RNA (enzymatic, protein synthesis and information-bearing) strongly suggests that early stages of life emerged from a combination of RNA and proteins. Such early RNA-protein world would imply the existence of genetic code prior to DNA, which is consistent with the ubiquitous use of messenger RNA (mRNA) and transfer RNA (tRNA) as protein-building machinery. Given the success of this early form of life, an immediate question arises: why would the transition from RNA to DNA be so complete as to eradicate all RNA-based genomes of living organisms? The straightforward answer is that DNA genomes would have had substantial advantage with respect to RNA genomes in terms of the reliable production of progeny genotypes. In effect, the absence of the −OH group on desoxyribose renders DNA much more structurally stable than RNA. In addition, the use of Thymine in place of Uracil evades one of the most common sources of mutations in RNA: the accidental production of Uracil via the deamination of Cytosine (see the similarities in Figure 1.1c). These advantages for DNA-based organisms may 20 CHAPTER 1. DNA AND SEQUENCE ALIGNMENT G G G A C C C U A G C U U A A A U C G A A U U U A U G C A U U A C G U A A A A U U U U A U G A U A C G C A U G C G C A U G C (A) (B) Figure 1.3: Single-stranded RNA molecules can form secondary structures. a) Self-annealing of complementary nucleotides from the same RNA strand. b) Representation of the secondary structure. Source [1]. have facilitated its survival in less permisive environments where they could develop more complex biological functions. 1.1.3 Genes and genomes Table 1.1: Genome size and protein-coding gene count of model organisms. Organism Genome size [bp] Coding genes Escherichia coli (Bacterium) 4.6·106∼4.400 Sacchromyces cerevisiae (Yeast) 12.5·106∼6.000 Neurospora crassa (Fungus) 39.9·106∼10.000 C. Elegans (Nematode) 100.2·106∼20.000 Arabidopsis Thaliana (Plant) 135 ·106∼27.000 Drosophila Melanogaster (Fly) 180 ·106∼13.000 Danio Rerio (Zebra Fish) 1.7·109∼26.000 Mus musculus (Mouse) 2.8·109∼23.000 Homo Sapiens (Human) 3.3·109∼20.000 In eukaryotic cells, the large amounts of DNA required to encode all the information needed to sustain cellular life, are packaged into chromosomes. Chromosomes are very long doublestranded DNA molecules found in the cell nucleous (Figure 1.4). These DNA molecules fit readily inside the nucleous and, after they are replicated, they can be easily distributed between the two daughter cells. On the other side, prokaryotic cells typically carry their genes on a single, circular DNA molecule called bacterial chromosome, found in the cytoplasm. The DNA in human species is packed into 24 different chromosomes, each consisting of a fine 1.1. BIOLOGICAL INFORMATION IS STORED USING DNA 21 CHROMOSOMAL DNA AND ITS PACKAGING IN THE CHROMATIN FIBER (A) (B) 10 µ m 1 6 7 8 9 10 11 12 181716151413 19 20 21 22 X X 2 3 4 5 Figure 1.4: Human karyotype. Human cells have 23 pairs of chromosomes. Each cell contains two copies of chromosomes 1 to 22 and two copies of chromosome X (female) or one copy of chromosome X and one copy of chromosome Y (male). Source [1]. thread of DNA and a set of proteins that fold and pack it into a compact structure. Such complex of protein and DNA is called chromatin. The main function of the chromosomes is to carry the genes (Figure 1.5). A gene is a segment of DNA that is transcribed, i.e. that is converted to RNA either to be used as is (structural, catalytic, regulatory RNA...) or to guide the synthesis of a protein. The total genetic information carried by all the chromosomes of an organism constitutes its genome. As it may be expected, there exists a correlation between the genome size and the complexity of the species, Table 1.1 summarizes the genome sizes of the most widely used model organisms. 1.1.4 Evolution of genomes The evolution of the current living species from the ancestral forms of life has been an impressive journey. The living organisms had been continuosly adapting to get through the selective pressure of the environment, a process that wouldn’t have been possible without the capacity of the genomes to change and generate new genes. Remarkably, the genes that provide an organism with selective advantage do not arise as a whole; they are instead the product of thousands or even millions of years of collection of non-deleterious single nucleotide changes, combined with the horizontal transfer of bigger sequence blocks. Single nucleotide changes or mutations may be produced by the exposure to radiation, doublestrand breaks on DNA, replication errors, etc. However, the evolution of genomes due to single mutations is an extremely slow process, considering that error rates during DNA synthesis in humans are in the 10−6to 10−8range. Besides, only an insignificant part of such mutations will remain, given that deleterious mutations are rapidly displaced by selective pressure. On the other side, more sophisticated mechanisms, which include: sequence duplication and recombination, virus infections or plasmid transformation, among others, allow the transfer of bigger blocks of DNA, a process called horizontal gene transfer (HGT). As a result of HGT, the genomes of the more 22 CHAPTER 1. DNA AND SEQUENCE ALIGNMENT heterochromatin regulatory DNA sequences human chromosome 22 in its mitotic conformation, composed of two double-stranded DNA molecules, each 48 × 106 nucleotide pairs long one gene of 3.4 × 104 nucleotide pairs ×10 ×10 ×10 10% of chromosome arm ~40 genes 1% of chromosome arm containing 4 genes protein RNA folded protein gene expression exon intron (A) (B) (C) (D) Figure 1.5: Organization of genes in the human genome. a) Representation of chromosome 22. b) A ten-fold expansion of a fragment of chromosome 22 with about 40 genes indicated in red. c) An expanded portion of (b) showing four genes. d) Representation of one of the genes of chromosome 22 where the regulatory region and its 9 exons are indicated. Source [1]. complex species are full of duplicated sequences that may belong, for instance, to a functional substructure present in many genes, a massive infection from ancestral virus or an accidental duplication. Such repeated sequences are so significant that fill up to 50% of the Human genome, whereas the protein coding sequences only represent the 2% (Figure 1.6). We will see in the following chapters that such repetitive structure of the genomes is the major source of difficulties in the field of sequence mapping. 1.1.5 DNA sequencing technologies DNA sequencing is a method or technology that is used to measure the precise order of the four bases (A,C,G,T) in a strand of DNA. The first sequencings methods: Chemical and Sanger sequencing were invented in 1977. These methods were very slow and expensive, requiring enormous human power, but were able to generate reads of several hundreds of nucleotides [3]. Later in the early 2000’s, Next Generation sequencing technologies were developed, currently yielding bilions of reads in less than 24 hours. A summary of the current sequencing technologies along with their properties is presented in Table 1.2. 1.2 Sequence alignment Sequence alignment is a process in which sequences of DNA, RNA or protein are arranged to identify the regions of highest similarity based on a score model. In other words, alignments are 1.2. SEQUENCE ALIGNMENT 23 10 0 20 30 40 50 60 70 80 90 100 LINEs SINEs introns retroviral-like elements simple sequence repeats nonrepetitive DNA that is neither in introns nor codons protein-coding regions DNA-only transposon “fossils” TRANSPOSONS segmental duplications GENES REPEATED SEQUENCES UNIQUE SEQUENCES percentage Figure 1.6: A representation of the nucleotide sequence content of the sequenced Human genome. Source [1]. Table 1.2: DNA sequencing technologies in current use. Name Read length [bp] Reads Time Error rate Cost/Mb Illumina 150+150bp >100M 24hrs ∼1% 0.1$ PacBIO up to 30kbp ∼100K 1-4hrs ∼15% 0.5$ Pyrosequencing 700bp 1M 24hrs <1% 10$ SOLiD 50+50bp 1G 1 week <0.5% 0.1$ Sanger 900bp <100 hours <0.1% 2000$ measurements of similarities between sequences. Alignments can be local or global, depending on whether subsequences of the query sequence are expected to be found split in distinct regions along the reference (local alignment) or the query sequence is expected to be found as a whole (global alignment). An example of a local alignment would be to align the mRNA sequence of a gene that contains introns against its reference genome, whereas global alignments would be used to align sequences of DNA that have been extracted from the genome itself. On the other side, sequence mapping is the process of finding the most probable original locus of a short sequence in a much larger reference, e.g. its reference genome. In the following sections we describe and discuss the complexity and purpose of some basic algorithms used for sequence alignment. Sequence mapping will be descripted in the following chapter. Even though there are a myriad of sequence alignment algorithms in the literature, the vast majority are based on the same approach: dynamic programming methods. The alignment between two sequences is computed by applying a recurrence relation throughout a matrix of m·nterms, called edit matrix or alignment matrix, where mand nare the respective sequence lengths. Such relation is based on a substitution matrix, the score matrix, that establishes a relationship between the possible sequence modifications and their respective penalties. The technique of dynamic programming can be applied to produce global alignments via the Needleman-Wunsch algorithm, and local alignments via the Smith-Waterman algorithm, described below. 24 CHAPTER 1. DNA AND SEQUENCE ALIGNMENT 1.2.1 Needleman-Wunsch algorithm The Needleman-Wunsch algorithm [4] is used to compute the global distance between two sequences. Let qbe a query sequence and tbe the reference sequence, with lengths mand n, respectively. Hence, the alignment matrix Sis an integer matrix with dimensions m×n, i.e. S∈Zm×n. We will refer to the i-th character of either sequence with a subindex, i.e. as tiand qi. Let us also introduce a simple score matrix, which defines the update values for character match, mismatch, gap open and gap extend (Table 1.3). Table 1.3: Examples of alignment scores. Levenshtein min-gap Pm(match) 0 −1 Ps(mismatch) 1 4 Po(gap open) 1 6 Pe(gap extend) 1 1 Other, more sophisticated matrices, take into account the probability of nucleotide transition (or aminoacid, in case of protein) drawn from empirical models of sequence evolution (Figure 1.7). Figure 1.7: BLOSUM62 matrix, used to score alignments between evolutionarily divergent protein sequences. Once the score matrix is defined, the Needleman-Wunsch algorithm is initialized as follows: each character of the query sequence qiis associated with the i+ 1-th row, similarly the reference characters tiare placed one per column, following the same trend. The first row and column are not associated to any character because they represent the first run of gaps. Hence, the first row and column are initialized by recursively applying a gap extension penalty Pe, as shown in Figure 1.8. Then, the matrix cells are computed row-wise or column-wise starting from the top left corner with the following update rules: 1.2. SEQUENCE ALIGNMENT 25 1234560 1 2 3 4 5 6 7 8 9 ACGCTTTAA A T G C A A Figure 1.8: Initialization of a Needleman-Wunsch alignment between ATGCAA and ACGCTTTAA. S[i, j] =          S[i−1, j −1] + Pm,if qi=tj. min(S[i−1, j −1] + Pm, S[i−1, j] + Pe, S[i, j −1] + Pe),if qi6=tjand gap is open. min(S[i−1, j −1] + Pm, S[i−1, j] + Po, S[i, j −1] + Po),otherwise. (1.1) 1 2 3 4 5 60 1 2 3 4 5 6 789 ACGCTTTAA A T G C A A 1 2 3 4 5 60 1 2 3 4 5 6 7 8 9 A C G C T T T A A A T G C A A 012345678 112344567 221234567 322123456 433223445 544333444 1 5 6 7 8 9 5 3 9 6 7 8 6 9 2 8 9 7 5 8 1 7 8 9 3 9 9 7 5 8 9 4 7 9 7 10 11 12 14 15 10 10 11 12 13 10 11 11 12 13 13 11 15 11 ACG C T T T A A ATGC---AA ACG C T T T A A A T G C - - - A A 1 2 3 4 5 60 1 2 3 4 5 6 7 8 9 ACGCTTTAA A T G C A A a 1234560 1 2 3 4 5 6 789 A C G C T T T A A A T G C A A b 012345678 112344567 221234567 322123456 433223445 544333444 1 5 6 7 8 9 5 3 9 6 7 8 6 9 2 8 9 7 5 8 1 7 8 9 3 9 9 7 5 8 9 4 7 9 7 10 11 12 14 15 10 10 11 12 13 10 11 11 12 13 13 11 15 11 Figure 1.9: Needleman-Wunsch matrix for q=ATGCAA and t=ACGCTTTAA (top row) and alignment path based on backtracking through valid cell transitions (bottom row). Green: match/mismatch. Red: Gap open/extend. a) Score matrix for Levenshtein distance (Pm= 0, Ps= 1, Po= 1, Pe= 1). b) Score matrix for Pm=−1, Ps= 4, Po= 6, Pe= 1. 32 CHAPTER 2. FINDING NEEDLES IN A HAYSTACK text. The tree is built following the procedure illustrated in figure 2.3: to insert the suffix ti,, the root node is selected, if the branch tidoes not exist, a new node is added to this branch. Then, the same process is recursively applied starting at the new node and following ti+1. The position of the suffix iis stored at the last node of the branch (the leaf). ATGAC 1 A T A T G A C 1 A T G A C 1 T G A C 2 G A C 3 C 4 C 5 Figure 2.3: Process of construction of a tree structure containing all the suffixes of the text ATGAC. The position of the suffixes are stored in the leaf nodes. To retrieve the occurrences of a seed in the index, the branches corresponding to the nucleotides of the seed are followed. The sequence is not present in the reference if, during this process, a branch is not found. Otherwise, the seed exists in the reference but the search may terminate in an intermediate node. When this happens, all the branches downstream must be followed to retreive the positions of the seed in the text. This index presents an expensive memory footprint (O(m2)) if the suffixes are stored at full. Besides, the exploration until the leaf becomes very demanding, with a worst case of O(m). To solve these problems and make this structure efficient, the suffixes are pruned at a predefined length l. The tree structure has a worse overall performance and footprint compared to the hash table. Assuming that the tree is pruned at length l, each query has a cost O(l) and the memory footprint is asymptotically O(lm). However, when querying subseeds of length l′< l, the cost is O(4l−l′) only in the worst case, because only the existing seed terminations (branches) will be followed. A much more efficient implementation of the tree-like index is the suffix tree [10]. Suffix trees store more than one letter per branch and reuse repeated parts of the text to boost the compression, achieving O(m) space and O(m) access time. 2.2.3 Suffix arrays The suffix array is a powerful data structure used to index and compress big texts [11]. It is an array containing the start positions of the suffixes after sorting them in lexicographical order. In other words, the suffix array of a text tof length mis: SAt= [i1, i2,...,im] where tij,< tik,iif j < k. (2.6) 2.2. EFFICIENT STRING SEARCH STRUCTURES 33 in equation (2.6), the symbol <denotes the lexicographical order comparison between two strings. Before computing the suffix array, a unique, lexicographically smallest symbol (usually $), must be appended at the end of the text (tm= $). An example of the suffix array of GATGCGAGAGATG is shown in Figure 2.4. Note that t=GATGCGAGAGATG$ has the $ symbol at the end of the text, which increases the text length by one (m= 14). The list of numbers at the top of Figure 2.4 is the suffix array of t, each with its corresponding suffix shown below. Noteworthy, the suffix array can be computed by traversing a suffix tree of tfrom 14 7911 2513 6810 1412 3 G A T G C G A G A G A T G $ G A T G $ G C G A G A G A T G $ T G $ T G C G A G A G A T G $ G A G A T G $ G A G A G A T G $ G $ C G A G A G A T G $ A T G C G A G A G A T G $ A T G $ A G A T G $ A G A G A T G $ $ Suffix array of GATGCGAGAGATG Sorted suffixes Figure 2.4: Suffix array of GATGCGAGAGATG. The numbers on the top represent the positions of the suffixes (the suffix array). Below, the suffixes sorted in lexicographical order. left (branch AAAA...A) to right (branch TTTT...T). The positions stored at each leaf coincide with the indices stored at the suffix array. We can use the suffix array to query exact sequences from the text. Since the suffixes are sorted, one possible method is to use the bisection algorithm [?] on the suffix array, which takes O(log2m) for a query qof any length. The memory footprint is compact, taking only O(m) space. 2.2.4 BW Transform and FM index Burrows-Wheeler Transform The Burrows-Wheeler Transform (BWT) is a lossless text transform based on text permutation [12]. The transform of a text tof length mwill be denoted as BWt= BWT(t). The BWT is performed in two steps: (i) Compute the suffix array of t(SAt). (ii) The BWT of tis the list of preceding characters of each position of the suffix array. Formally, BWt[i] = t[SAt[i]−1] (2.7) for SAt[i]>1 and BWt[i] = t[m] if SAt[i] = 1. 34 CHAPTER 2. FINDING NEEDLES IN A HAYSTACK Following the previous example shown in Figure 2.4, we construct the Burrows-Wheeler transform by taking the preceding character of each suffix (Figure 2.5). A fundamental property of the Burrows-Wheeler transform is: Property 1. Each nucleotide in the BWT appears in the same relative order as in the Suffix Array. That means that the first Ain the BWT will be also the first Aappearing in the SA, the fourth Gin the BWT is the same as the fourth Gin the SA, and so on. 14 7911 2513 6810 1412 3 $ G A T G C G A G A G A T G $ A G A T G $ T G C G A G A G A T G $ A T G $ A T G C G A G A G A T G $ A G A G A T G $ C G A G A G A T G $ T G $ G C G A G A G A T G $ G A T G C G A G A G A T G $ G A T G $ G A G A T G $ G A G A G A T G $ G $ Sorted suffixes (BWT) Preceding Figure 2.5: The preceding character of the suffixes shown in Figure 2.4. The preceding character of the first suffix is the last character of the text ($). To prove Property 1, consider the first character of the BWT shown in Figure 2.5, BWt[1] = G. Note that the full suffix starting at this position is the concatenation of BWt[1] with the suffix tSAt[1],. Since tSAt[i],> tSAt[j],for any j > i, BWt[1] = Gwill be also the first Gto appear in the suffix array. The property is illustrated in Figure 2.6. The Burrows Wheeler transform has other interesting properties, like the rearrangement of the text into runs of similar characters. This allows for effective text compression using simple algorithms like run-length encoding. The Ferrangina-Manzini Index and Backward Search The Burrows-Wheeler transform is an interesting structure but it cannot be used as is for the purpose of sequence mapping. The Ferrangina-Manzini (FM) index is a combination of the BWT with other structures which allows to query any sequence of length nin O(n) time, regardless of the text size [13]. Let us illustrate how the Burrows-Wheeler transformed text can be used to look for an arbitrary sequence qin the text, for instance, q=GAGA (Figure 2.7). All the occurrences of GAGA in the text 2.2. EFFICIENT STRING SEARCH STRUCTURES 35 14 7911 2513 6810 1412 3 $ G A T G C G A G A G A T G $ A G A T G $ T G C G A G A G A T G $ A T G $ A T G C G A G A G A T G $ A G A G A T G $ C G A G A G A T G $ T G $ G C G A G A G A T G $ G A T G C G A G A G A T G $ G A T G $ G A G A T G $ G A G A G A T G $ G $ Sorted suffixes (BWT) Preceding Figure 2.6: The correspondence between the preceding character and its order of appearance in the suffix array. Note that the oder of appearance is conserved for each nucleotide set. end with A. Each Ais also the first letter of some suffix of the text. Because such suffixes all start with A, they are stored next to each other in the suffix array, namely between positions 2 and 5 (Figure 2.7). However, only those suffixes preceded by a Gcan potentially contain the query. This is exactly the information encoded in the Burrows-Wheeler transformed text BWt: the preceding character. In this concrete example, all the As are preceded by a G, so the text contains 4 suffixes that start with GA. So far we know there are 4 suffixes in the text that start with GA, but how do we continue extending the query? Now we need to search for all the Apreceding the suffix GA. Indeed, we just learned that the set of Gfrom the suffix GA are the 2nd, the 3rd, the 4th and 5th on the BWT. Therefore, they also will be the 2nd, 3rd, 4th and 5th Gin the Suffix Array (Property 1). So now we can continue from the range in the SA covering from the 2nd until the 5th G, and check for the preceding Ain the BWT. Now we find two Athat correspond to the 1st and 2nd Ain the Suffix array. We would continue in the same fashion until the completion of the query q. This process is called backward search because the query starts with the last nucleotide and iteratively queries the preceding nucleotides. The BWT is necessary but not sufficient to perform the backward search on an index. We need additional information to: (i) get the positions in the suffix array where a suffix is present and (ii) the order of occurrence of each nucleotide in the BWT. Since the Suffix Array is a representation of the sorted suffixes, all the occurrences of the same letter will appear together, i.e. first all the A, then all the C, and so on (Figure 2.4). Therefore, to know the position of the first appearance of each letter, we will directly store them in an array called C. It is straightforward to retrieve the full range of any nucleotide from C, for instance the range of Ais C[A] to C[C]−1. In general, the full range of the i-th letter of the alphabet Σ is [C[Σ[i], C[Σ[i+ 1] −1]]. 36 CHAPTER 2. FINDING NEEDLES IN A HAYSTACK G A A G $A T A AACTGGGGGG GG G T TGGGCAAAA$ 2 3 4 5 $A T A AACTGGGGGG GG G T TGGGCAAAA$ 1 2 $A T A AACTGGGGGG GG G T TGGGCAAAA$ 2 3 $A T A AACTGGGGGG GG G T TGGGCAAAA$ 14 7911 2513 6810 1412 3 Sorted text BWT Figure 2.7: Backward search using the preceding character information. The inteval of the matching suffix is shrinked every iteration, when the preceding nucleotide is added. To store the order of occurrence of the nucleotides in the BWT we will use the Occ structure. This structure, shown in Figure 2.8, is the individual cummulative count of each nucleotide in the BWT. The cummulative count directly indicates the order of appearance of a nucleotide in any position of the BWT. Figure 2.8 illustrates the query of q=GAGA in t, now using the complete index with Cand Occ. First we start looking for the full range of A, this is from C[A] until C[C]−1. Then we check the occurrences of Gwithin this range: (i) we find one appearance of Gbefore C[A] checking Occ(G, C[A]−1) = 1 and (ii) at the end of the range, we have Occ(G, C[C]−1) = 5. With a simple subtraction we conclude that the range contains 4 G, from the 2nd until the 5th. Now we would like to obtain the range of the suffix array covering these G. Since C[G] points to the 1st G, the range covering from the 2nd until the 5th Gis from C[G] + 1 until C[G] + 4. Once we got a new range, we can start again the process by checking the occurrences of the next nucleotide of the query. In general, we define the range update process for an arbitrary query qof length nas sp[i+ 1] = C[q[n−i]] + Occ(q[n−i], sp[i]−1) ep[i+ 1] = C[q[n−i]] + Occ(q[n−i], ep[i]) −1(2.8) for i= 1 . . . n −1 and sp[0] = C[q[n]], ep[0] = C[q[n] + 1] −1. The range at the i-th step of the query is completely determined by [sp[i], ep[i]], where sp and ep denote the start and end points of the BWT ranges, respectively. Once the update process finishes, we obtain the range [sp[n], ep[n]], which contains the positions of qin the suffix array. The number of occurrences of qin the text is ep[n]−sp[n] + 1 and their positions in twill be the values of the Suffix Array stored at positions 2.2. EFFICIENT STRING SEARCH STRUCTURES 37 G G 12345 A A G 666666666 A AG TC$ 00000000122234 T 00000011111222 C 00000001111111 C[G]+Occ(G,5) C[C]-1C[A] C[A]+Occ(A,11) C[G]+Occ(G,1)+1 C[G]+Occ(G,3) C[G]+Occ(G,1)+1 C[A]+Occ(A,7)+1 Occ BWT C $A T A AACTGGGGGG Figure 2.8: Backward search using only the C and Occ tables. The suffix intervals are updated with (2.8) reading the query backwards. from sp[n] to ep[n]. Note that the update algorithm described in (2.8) takes exactly nupdate steps, which only require the Suffix Array, the Occ table and the Carray as auxiliary structures. Hence, we conclude that the backward search algorithm has a complexity of O(n). The memory footprints for the different structures are: O(m) for the Suffix Array, O(kΣk) for the Carray and O(kΣkm) for the Occ table, resulting in an overall memory footprint O(kΣkm). However, with the use of simple compression techniques, the final size of the index may be reduced to less than mbytes. Bi-directional FM index There exists a variant of the FM-index that supports both backward and forward search, as well as switching from backward search to forward search, or vice versa. This index is called bidirectional FM index [14]. This index is just an extension of the FM index described above, in which the reverse complement of the text is appended to the original text. That is, if tis the original text to be indexed and (·)Rdenotes the reverse complement operation, then the input to the FM index is “ttR$”. The main advantages of having the reverse complement of the text stored in the index are: •the possibility of searching in both DNA strands with just one query. •performing a backward search operation on the reverse strand is equivalent to a forward search on the forward strand, and vice versa. For that, we need to know the position of the start and end pointers for both strands for a given query, e.g. if we perform a backward search of ACTGCA and compute its sp and ep pointers in a 38 CHAPTER 2. FINDING NEEDLES IN A HAYSTACK given index using (2.8), the pointers of the reverse complement of the query (reverse strand) spqR and rpqRare necessary to perform bi-directional search. To keep track of the positions of the pointers in the forward query and its reverse complement, we define the following property: Property 2. For any given sequence q, the number of occurrences of qand qRon a bi-directional FM index structure is the same. As an example, we will search the query ACTGCA starting from the middle, i.e first do backward search on ACT and then extend the query forward querying GCA. We start the backward search with the nucleotide T, but we need to store the pointers both for the forward and reverse complement of the query. For the first nucleotide this process is straightforward; we find the sp and ep pointers as: spT=C[T], epT=C[T+1]−1. And the same for the reverse complement: spTR=C[TR] = C[A] and epTR=C[A+1] −1. Since we know from Property 2 that epq−spq=epqR−spqR, we do not need to store the four pointers. Instead, the search state in the bi-directional FM index is completely defined by a triplet: the forward pointer fpq=spq, the reverse pointer rpq=spqR and the interval size szq=epq−spq+ 1. Using this notation, we derive a second property: Property 3. For any given sequence q,fpq=rpqRon a bi-directional FM index structure. Once the triplet fpT, rpT, szThas been computed, one can extend the search either forward or backward. To continue with our example, we extend the forward strand of the query backwards, adding a C. We do so with the backward search update step described in (2.8). This will yield the updated values fpCT and szCT, but we still need to keep simultaneous track of the pointer in the reverse complement strand: rpCT. Note that a backward extension on the forward strand is equivalent to a forward extension on the reverse strand (the equivalent search of CT in the reverse strand is AG). Thus, to know rpCT we need to perform a forward search of the nucleotide Gon the reverse strand of the query. The process to update rpTto rpCT relies on very basic properties of the bi-directional FM index and the suffix array. Our current interval ITR= [rpT, rpT+szT−1] are all the positions of the suffix array with suffixes starting with TR=A(Figure 2.8). Since the forward-extended suffix AG also contains the suffix A, the new pointer rpCT must lie in the current interval ITR, i.e. ICTR= [rpCT, rpCT +szCT −1] is a subinterval of ITR. More generally, we can divide any interval Iqin 5 subintervals, one for each possible forward extension: IqA,IqC,IqG,IqT and IqN, as shown in Figure 2.9. In such context, the following identities verify: Iq=IqA∪IqC∪IqG∪IqT∪IqN |Iq|=|IqA|+|IqC|+|IqG|+|IqT|+|IqN|(2.9) where |Iq|=szq. Provided that szCT =szAG from Property 2, we can obtain the interval sizes for the forward extension as |IqX|=szqX=sz(qX)R=szXRqR, therefore the sizes obtained with the backward search can be reutilized to compute the subinterval sizes |IqX|=szXRqR(2.10) 2.2. EFFICIENT STRING SEARCH STRUCTURES 39 AAACGATT AACATTGA AATTAGTA ACATGGAG ACCTACAC ACGGGCTT AGAGTGTC AGCAAAGG AGGTATTT ATCTCCGT ATTGCGGA CAACAAGA IA IAA IAC IAG IAT fpAA fpA fpAC fpAG fpAT Figure 2.9: Subintervals of the suffix A. The subintervals are consecutive in the suffix array. The interval of the suffix Ais denoted by IA, the subintervals are IAA,IAC,IAG,IAT and fpis the position of the first suffix of the interval. where NR=N. Once the subinterval sizes are known, we use Property 3 to derive the update rules for rpq: rpTq=rpq rpGq=rpq+szTq rpCq=rpq+szTq+szGq rpAq=rpq+szTq+szGq+szCq rpNq=rpq+szTq+szGq+szCq+szAq (2.11) Back to our example, we already computed fpCT and szCT with the backward search update rules, we can now follow Equation 2.11 and obtain the reverse strand query pointer as rpCT = rpT+szTq+szGq. To perform a forward search, one only needs to apply the same procedure to the reverse strand. In effect, assume that we already computed the triplet fpACT,rpACT,szACT for the first half of the query and we would like to continue extending the query (ACTGCA) forward. To do so, we perform a forward extension of Gin the forward strand. Again, performing a forward extension on the forward strand is equivalent to a backward extension to the reverse strand, so we only need to apply the same process but swapping the strands. We update the reverse pointer rpACTG =fpCAGT with a backward search on GR=Cand compute the forward pointer as fpACTG =fpACT +szACTA +szACTC. 40 CHAPTER 2. FINDING NEEDLES IN A HAYSTACK 2.3 The sequence neighborhood In this section we will introduce the concepts of sequence neighborhood and neighbor sequences. The neighbor annotation of a text twill be very useful to determine sequence similarities and will be exploited in some steps of the mapping algorithm to speed-up the process and improve the quality of the classification. Let us first define the sequence neighborhood. A k-neighborhood is the complete set of sequences that are knucleotides long, i.e. the permutation with replacement of kelements taken from the set Σ = {A,C,G,T}. A k-neighborhood contains 4kdifferent sequences of length k(kmers). For instance, the 1-neighborhood is {A,C,G,T}, the 2-neighborhood {AA,AC,AG,AT,CA, CC,CG,CT,GA,GC,GG,GT,TA,TC,TG,TT}, and so on... We say that two k-mers are τ-neighbors iif their distance under a certain metric is equal to τ. We consider two different metrics for DNA sequences, namely the Hamming distance where only substitutions are accounted as differences, and the Levenshtein distance where both substitutions and indels (insertions and deletions) are accounted as differences2. Both metrics are suitable for the methods introduced below. 2.3.1 Neighborhood annotation algorithm In future sections, we will assist the detection of repeats and accurately compute the probability of correct mapping with a neighborhood annotation. The annotation consists in computing the τ-neighbors of all the k-mers of a reference text and store, for each of them, (i) the distance to the closest neighbor and (ii) the number of neighbors at such distance. This information very is valuable because sequences that have, for instance, a high count of 1-neighbors are more prone to be incorrectly mapped compared to the ones whose closest neighbor is at greater distance. The design of an efficient algorithm to annotate the neighborhood of every k-mer of a long text is not straightforward. A naive approach would be to compare all the k-mers of the text with each other. Such algorithm has a complexity of O(m2k) for a genome of size m. In the human genome, where m∼109, this approach would take ages to complete. In this section we propose a divide-and-conquer algorithm to efficiently compute a neighborhood annotation. This algorithm exploits the advantages of the bi-directional FM index and can compute a full annotation of the human genome with k= 48, τ= 4 in less than 100 CPU hours. The algorithm proceeds as follows: 1. Select the next k-mer from the suffix array (in lexicographical order). 2. Query the sequence using backward search. If the sequence has more than one matching position, set τ= 0. 3. Otherwise, perform a divide and conquer search up to τmismatches, using bi-directional search and starting the search by the interval without mismatches. 2Introduced in Section 1.2 2.3. THE SEQUENCE NEIGHBORHOOD 41 4. Annotate the distance to the closest neighbor ˆτand the number of matches at distance ˆτin the position of the genome (available at the suffix array) where the k-mer was taken from. Block search algorithm For these cases in which the query sequence is not exactly repeated in the genome, we have designed a divide and conquer algorithm to perform inexact k-mer search, up to τmismatches. This algorithm is called block search because it divides the k-mer in τ+ 1 blocks and performs a recursive inexact matching using the bi-directional FM index. The algorithm starts by dividing the sequence in τ+ 1 blocks, at this level blockcount is τ+1. Then, the blocks are grouped in two sides, left and right, and we recursively apply the same division to both sides. Once blockcount is 1, an exact search is performed and the result of the search is returned to the calling function. When the block search call on the left side has returned, we extend the search until the end of the right side, allowing up to blockcount-1 mismatches. The same procedure is applied once the recursive call to the right side has returned, extending until the end of the left side. On the top level, the recursive call will return all the matches from the left side with up to τ/2 differences, and each one of these matches will be extended forward until the end of the query, allowing a total of τmismatches. The other recursive call will return all the matches of the right side with up to τ/2 mismatches and they will also be extended using backward search until the beginning of the query, allowing up to τmismatches. The aggregate of the forward and backward extension is the set of sequences in the index that match the query with up to τmismatches. The block division and extend process is illustrated in Figure 2.10 and the algorithm is described in Algorithm 1. The algorithm presented achieves a notable speedup over the naive algorithm described above. The improved efficiency of the FM index reduces the complexity from O(m2k) to O(mk τ3τ), where the binomial-exponential term represents the exhaustive tree-like traversal of the index to find inexact matches. The block search algorithm (described in Algorithm 1) is efficient because it reduces the search space of the index where the expensive inexact search is performed. In other words, since the block search always starts by an exact search of k/(τ+ 1) nucleotides, the subsequent dead-end mismatched extensions are more likely to fail immediately unless the queried sequence is a true neighbor. In a best case scenario (random genomes), the search space and hence the complexity would be consistently reduced by at least a factor 4k/τ+1 after the first exact search. 2.3.2 k-mer uniqueness Ak-mer sis τ-unique in a reference text tif neither d-neighbor (for d= 0 . . . τ) of sis a suffix of t. It is straightforward to check the uniqueness of susing the previously described neighborhood annotation. We can obtain the genomic locus of swith a backward search on tand then check the distance of the closest neighbor in the annotation. If the annotated distance is greater than τ then sis τ-unique in t. 48 CHAPTER 3. MAPPING ALGORITHM the true sequence is not the seed but originates from one of its neighbors: P(neighbor) = X τ P(neighbor|τdifferences) ·P(τdifferences) (3.3) We observe from the neighborhood annotation that the closest neighbor is at distance τc, therefore P(neighbor|τdifferences) = 0 for τ < τc. Assuming also that P(neighbor|τc+1differences) ·P(τc+1 differences) ≪P(neighbor|τcdifferences) ·P(τcdifferences) we approximate P(neighbor) = Pτ≥τcP(neighbor|τdifferences) ·P(τdifferences) P(neighbor) ≈P(neighbor|τcdifferences) ·P(τcdifferences) (3.4) this lets us obtain an upper bound for P(neighbor), considering that P(τcdifferences) ≪1, P(neighbor) < P (neighbor|τcdifferences) (3.5) or, equivalently P(neighbor) <Nc l τ8τ(3.6) Table 3.1 summarizes this probability for various combinations of Ncand τcat the minimum seed distance for the Illumina setting (l= 19). Table 3.1: Upper bound probability of incorrect seed (l= 19). τc= 1 τc= 2 τc= 3 Nc= 1 6.6·10−310−42·10−6 Nc= 10 0.07 10−32·10−5 Nc= 100 0.66 0.01 2 ·10−4 After the first round of seeding, if a seed shows a P(neighbor) smaller than a predefined threshold (∼10−4) it is considered significant enough, thence only this seed is aligned and the rest are discarded. 3.3 Aligning Once a seed has passed the significance filter, it is used as an anchor to start a more exhaustive comparison between the query and the reference. Such comparison is called sequence alignment. Standard methods for sequence alignment were described in Section 1.2. 3.3. ALIGNING 49 3.3.1 Sequence alignment The alignment process starts at the seed, i.e. coordinates where the read and the reference partially match. Then two Needleman-Wunsch (NW) alignments are performed, extending the seed forward (towards the end of the read) and backwards (towards the beginning of the read). The overall score of the alignment is computed as the sum of both extensions. In the current version of the software we used a naive non-optimized implementation of the NW algorithm, however, SIMD [21, 22] and GPU [23] optimized versions of this algorithm have been published, reporting speedups of several orders of magnitude. 1. seed read reference 2. extend read reference read reference Figure 3.1: Seed and extend algorithm. Seeding consists in finding short exact matches between the read and the reference. The extension step performs a full alignment between the read and the reference at the position of the seeds. 3.3.2 Split reads and breakpoint algorithm Another interesting feature of our mapping algorithm is its ability to map split reads. Split reads are sequences constituted by more than one DNA fragment. Such reads are deliberately generated in experiments like Hi-C [20], in which the different fragments that constitute the sequenced DNA molecule may originate from very distant positions of the genome. In this context, the problem generalizes to map an unknown number of subsequences of unknown lengths. One possible way to detect the breakpoint between two different fragments is to infer the location of an abrupt decrease of sequence identity during alignment. In effect, during the Needleman-Wunsch extension, one expects high identity between the read and the reference as long as the alignment extends over the seeded fragment. However, when the alignment reaches the breakpoint between fragments, the rest of the extension behaves approximately like an alignment between random sequences. We exploit this fact and design an optimal breakpoint detection algorithm based on the Maximum Likelihood (ML) breakpoint detection model presented in [24]. 50 CHAPTER 3. MAPPING ALGORITHM split read reference alignments fragment 1 fragment 2 Figure 3.2: Seed and extension over a split read. The split read is composed of two distant fragments of the reference (blue and red). At the extension step, the alignment proceeds passed the fragment border, where the sequences do not coincide anymore (represented in gray on the reference). In the breakpoint detection algorithm we model our read as a sequence S= (S1,...,Sn), where the Siare independent random variables following a binomial probability distribution pr(Si= 1) =    θ0(i= 1,...,b) θ1(i=b+ 1,...,n) (3.7) and pr(Ri= 0) = 1 −pr(Ri= 1); and θ0and θ1are the known alignment mismatch probabilities but the change-point bis unknown. The random variable outputs 0 and 1 represent matches and mismatches between the read and the reference, respectively. For our model of alignment over a split-read, we estimate θ0to be the expected error probability of two truly-matching sequences, which depends on the sequencing technology error rate, the mutation rate and the divergence of the reference text and the real genome. We expect the value of θ0for Illumina reads mapped on a well annotated genome to be θ0<0.05. On the other side, θ1represents the error read past the split-read boundary, which we approximate to a comparison of two random sequences, being θ1>0.35 for an alignment with insertions, deletions and substitutions. Under the model (3.7), the likelihood function of (S1,...,Sn) is b Y i=1 θSi 0(1 −θ0)1−Si n Y i=b+1 θSi 1(1 −θ1)1−Si(3.8) so that the log likelihood conditional on b=t, with known θ0and θ1, can be written as L(t) = t X i=1 Silog θ0 θ1+ (1 −Si) log 1−θ0 1−θ1+ n X i=1 {Silog θ1+ (1 −Si) log(1 −θ1)}(3.9) Hence the maximum likelihood estimate of the breakpoint ˆ bis the value of twhich maximizes the 3.4. MAPPING QUALITY 51 sequence Xt= t X i=1 Silog θ0 θ1+ (1 −Si) log 1−θ0 1−θ1 (t= 1,...,n) (3.10) The maximization step described in (3.10) is applied periodically during the alignment process to track the position and the likelihood of a potential breakpoint. The algorithm stops when Xˆ b exceeds a defined threshold, or at the end of the alignment otherwise. A detailed relationship between the the breakpoint confidence and the likelihood Xˆ bcan be found in [24]. 3.4 Mapping quality At this point of the algorithm we obtained many candidate mapping positions and computed the alignment score for them. But there is still one crucial step: to provide a mapping confidence score, i.e. the probability that the association between the read and the reported locus is correct. The mapping quality model is what ultimately makes the difference between a good and an average mapper, because the final users will process their reads and trust the assignments based on a threshold confidence score. Hence, the mappers should be characterized by: (i) how many of the input reads is the mapper able to find in the reference? (sensitivity) and (ii) how many of the high confidence mappings are actually wrong assignments? (false positive rate). In this section we present two simple models to evaluate the mapping confidence, which used in combination yield better confidence estimates compared to other widely-used mappers [25, 26]. The mapping quality reported by our mapper is computed as: MAPQ = −10 log (pr(wrong assignment)) (3.11) 3.4.1 Alignment score model This widely-used mapping quality model is based on the alignment scores of the extended seeds. The idea is simple: sort the alignment scores of the seed extensions. The locus with highest alignment score is considered the true mapping position of the read. In case there are many n1>1 best matches, each match is considered equally likely to be the true origin of the sequence. Hence, the probability of incorrect assignment is pr(wrong assignment) = 1 −1/n1. As the gap between the best and the second best alignment scores grows, the probability that the best alignment is indeed the correct mapping position becomes evident. In effect, this is analogous to the neighbors model, a big gap in the alignment score indicates that the reference sequence is somehow unique and therefore it is unlikely that its similitude with the read is due to an artifact. When the alignments report only one n1= 1 best alignment, the model computes the mapping quality with an heuristic which takes into account the difference between the best and the second best alignment scores ∆12, the number of second best score matches n2and the identity (1 minus 52 CHAPTER 3. MAPPING ALGORITHM the mismatch rate) of the best alignment η: QA=f(η)·(c∆12 −10 log10(n2)) (3.12) where fis a function and ca constant. This model is powerful but may show substantial performance differences depending on the sequencing technology and the reference genome, because the parameters of the heuristic f, c are usually calibrated by fitting a curve on simulated data. This model alone, however, already gives extraordinary results for well-calibrated inputs [25]. 3.4.2 The neighbors model The neighbors model follows the same principle of the alignment score model: quantify the reliability of the read-locus assignment through the exploration of the close neighborhood. This model, however, is both faster and more precise thanks to the availability of exhaustive neighborhood information. It also allows a notable reduction of the necessary alignments at the extension step, since one can assess the state of the neighborhood with a single seed extension, whereas the same is not possible using the alignment score model, in which at least two alignments are required. Consider the following sequences TR,TB,and TN, which represent the read, the sequence in the reference that best matches the read and its closest neighbor, respectively. TBis known and has been obtained through a seed-and-extend process applied to TR. The closest neighbor of TBis generally unknown but we can infer some information about TNusing the neighborhood annotation. Since TBand TNexist in the reference, we will denote their positions with lBand lN. We also denote the Levenshtein distance between two sequences T1and T2as d(T1, T2). d(TR,TN) TN TB TR d(TB,TN) d(TR,TB) Figure 3.3: Local neighborhood of the read TR, its best match TBand the closest neighbor of the best match TN. The Lenvenshtein distance between sequences is denoted by d(·,·). The diagram of Figure 3.3 illustrates the local neighborhood composed by TR,TBand TN. Note, however, that this neighborhood does not represent the annotation since, in general, TR does not belong to the reference. The distance d(TR, TB) is known because TRand TBhad been aligned previously during the seed-and-extend algorithm. The distance d(TB, TN) can be obtained from the neighborhood annotation, but the remaining distance d(TR, TN) is generally unknown. 3.4. MAPPING QUALITY 53 The fact that d(TR, TS) is unknown greatly complicates the construction of a model to evaluate the mapping quality. The goal of the model is to estimate the probability that TRdid not originate from TB, which we approximate by the probability that TRoriginated from any of its closest neighbors TN. To derive an estimation of such probability, we make several reasonable assumptions on d(TR, TN): Assumption 1. The distance d(TR, TB)is small enough to assume that the whole d(TR, TB)- neighborhood of TRhas been explored during the seeding stage. In other words, we assume that the seeding strategy is fully sensitive up to d(TR, TB)mismatches. Assumption 2. As a result of assumption 1, we conclude that TBis the closest neighbor of TR and the inequality d(TR, TN)>d(TR, TB)must hold. Otherwise, if d(TR, TN)≤d(TR, TB)we would have performed a seed extension of TRon TNand d(TR, TN)would be known. We start to build our model leveraging on assumptions 1 and 2, with the hypothesis that the true origin of TRis lNinstead of lB. From our definition of mapping quality, it is reasonable to approximate the probability of incorrect mapping by the probability that TRoriginated from lN even when TBlies closer in the neighborhood space. T A TN T A TR TB original sequence: (TR=TN) T A G TN T A A TR TB common mismatch: d(TR,TB)+=1 d(TR,TN)+=1 T A TN T T TR TB divergence of TN: d(TR,TN)+=1 T A TN C A TR TB correction: d(TR,TB)-=1 d(TR,TN)+=1 Figure 3.4: On our hypothesis for the mapping score, TRoriginated from TN. This figure summarizes the three possible scenarios when a nucleotide of TRis modified. From our hypothesis we conclude that the physical sequence taken from the reference is TN but it has been exposed to modifications during the experiments and/or the sequencing steps, causing the final read to lie closer to TB. In Figure 3.4 we summarize the possible modifications that the molecule can suffer and how do they affect the position of TRrelative to TNand TB. The top-left panel illustrates the original sequence, where TRis copied from TN, and their differences 54 CHAPTER 3. MAPPING ALGORITHM with TB. The only way to push TRtowards TBis through nucleotide modifications, with three possible scenarios: •The first is a common mismatch, when the modified nucleotide is not a mismatch between TBand TN, the distances from both references to TRincrease by one. •The second case is when a mismatched nucleotide between TNand TBis modified but it is not set equal to the one in TB. So, TRdiverges from TNbut not from TB, because the mismatch already existed between TRand TBat that position. •The third case is the same as case 2 but the modified nucleotide is set to match the one in TB, causing a correction with respect to TB. In this case the distance to TNincreases by one and d(TR, TB) is decreased by one. Now the question is how these modification events combined to finally situate TRcloser to TB. Following Figure 3.3, we can express the distances of the local neighborhood as d(TR, TN) = M+C+D d(TR, TB) = d(TB, TN) + M−C(3.13) where the distances and all the other terms are non-negative: Mare the common mismatches, D the divergent mismatches and Cthe corrections. Besides, the sum of corrections and divergences must be equal or smaller than the distance between TBand TN: D+C≤d(TB, TN) (3.14) Plugging (3.13) into assumption 2, it is clear that at least one correction or two divergences must happen to verify d(TR, TB)<d(TR, TN): d(TB, TN) + M−C < M +C+D(3.15) which simplified, yields d(TB, TN)<2C+D(3.16) where, in effect, for the smallest distance d(TB, TN) = 1 we need C≥1 or D≥2. Our interest is to find the probability of the most likely divergence of TRtowards TB. Since divergences are more likely to happen compared to corrections, we will start by determining the minimum number of corrections necessary to verify the hypothesis. The value of Chas two restrictions, the first is that Mon (3.13) must be non-negative. The limiting case is M= 0, which plugged into (3.13) yields the inequality d(TR, TB)−d(TB, TN) + C≥0, hence C≥max(1,d(TB, TN)−d(TR, TB)) (3.17) The second constrain is derived by applying the triangle inequality to Figure 3.3, i.e. d(TR, TN)≤ 3.4. MAPPING QUALITY 55 d(TR, TB) + d(TB, TN), 2C+D≤2d(TB, TN).(3.18) The two constrains together give a lower bound (3.17) and upper bound (3.18) for C, that we will denote CLand CUrespectively. Once the range [CL, CU] has been found, we can determine Di and Mifor i= [L, U] from and (3.13): D= max(0,d(TB, TN)−2C+ 1) M= d(TR, TB)−d(TB, TN) + C(3.19) Finally, we model the probability that TRoriginated from lNgiven Cicorrections and Di divergences on a sequence of length Las: pri(origin is TN) = d(TB,TN) Cid(TB,TN)−Ci Di·3Di L Mi+Di+Ci·3Mi+Di+Ci =d(TB,TN) Cid(TB,TN)−Ci Di L Mi+Di+Ci·3Mi+Ci (3.20) and the mapping quality is obtained from the maximum of these probabilities MAPQ = −10 log(max i(pri(origin is TN))).(3.21) Final remarks on the mapping quality The mapping qualities are computed independently for the neighbors and the alignment score models. The final mapping quality is the minimum of these two. Also, the neighbors model does not have neighbor annotation for any length L, since the neighborhood annotations are computed for discrete values of k. Therefore, Lis selected to be the largest k < n, where nis the length of the query. The neighbor model for the mapping quality may be then computed for all the k-mers of the read. In such case, one should keep the maximum among all the qualities obtained. Chapter 4 Results To test the results of this work, we implemented all the algorithms described above in C language. The source code of the mapper is available at http://github.com/ezorita/mapper. The mapping performance of our algorithm is tested and discussed in this chapter. We compare all the results of our mapper with two of the most widely used mappers: bwa-mem [25] and bowtie2 [26]. The performance of the three mappers has been tested on simulated Human and Drosophila Illumina data generated with wgsim (http://github.com/lh3/wgsim). Datasets of 1 million single-end 50bp reads were generated for different sequencing error rates. Namely, we considered three usual sequencing scenarios: good (1% error rate), standard (2%) and average (5%). Wgsim was run in command line with parameters -e R -N 1000000 -1 50 -2 0, where Rwas set to 0.01, 0.02 or 0.05 depending on the scenario. The simulated reads are presented in fastq format without meaningful quality information (all bases are assigned the same quality score). The sequence headers contain the real locus and the strand of the read. After running the three mapping algorithms on the datasets the read headers are parsed and compared to the mapped locus and strand. Reads are considered well mapped if the estimate in the same strand and within 100 basepairs of the actual locus. The algorithms were benchmarked on a dual-processor Intel Xeon E5-2687W v2 system with 256GB of DDR3-RAM at 1866 Mhz. All softwares were set to run single-core. Our mapper and bwa-mem were ran using default parameters, bowtie2 was setup to use the very-sensitive configuration. Our benchmark includes three measurements: •Sensitivity expressed as the fraction of correctly mapped reads with respect to the total input read count. •Positive error rate is the rate of false positives per true positive. •Throughput expressed as the rate of true positives per second. We believe that these three magnitudes provide a good characterization of the performance of a mapper. Almost all benchmarks evaluate the sensitivity, but it is equally important to provide 56 57 an accurate estimate of the expected number of incorrect mappings for each quality score. This will ultimately help the users decide which quality score threshold is better suited for their needs. We combine these magnitudes in two-dimensional plots to show the comparison of sensitivity versus positive error rate, and throughput versus positive error rate. The first provides valuable information to tune the acceptable quality threshold, clearly showing how many correct mappings one must disregard in order to reduce the false positive rate. The latter is a representation of how efficiently the computational power is spent. Classification performance The classification and mapping performence achieved by the different algorithms is represented in Figure 4.1. This result compares the sensitity versus the false positive rate achieved in the Human datasets with 1% (left), 2% (right) and 5% (bottom) error rate. Each point represents the values for a different mapping quality threshold, i.e. a threshold on mapping quality is applied on the mapping output and all the reads below the threshold are discarded. The sensitivity and false positive rates are therefore measured with the reads whose quality is above the threshold. This process is repeated for all the possible quality values, starting from 0 (the rightmost points). The general trend of the mappers is the one expected: the false positive rate decreases as the quality increases, but so does the sensitivity. Our algorithm outperforms the bwa-mem and bowtie2 in the three configurations, achieving a good response even in the worst configuration. Remarkably, our algorithm consistently reports 100.000 more well-mapped sequences with false positive rates below 10−4at the standard Illumina setting (2%). Moreover, our mapper shows a smooth response on average sequencing (5% error), recovering >65% of the reads in all the possible configurations, whereas the other mappers drop well below 50% as soon as they reach a false positive rate of 10−4. Computational efficiency In this section we assess the computational efficiency of the algorithms. To do so, we measure the rate of true positives produced at the output for a target error read. We prefer a direct measurement of how well the computational resources are spent rather than the absolute running time, since an algorithm may run very fast but produce poor results. The measurements of computational efficiency are shown in Figure 4.1 for Human Genome reads in the two extreme cases of 1% error rate (left) and 5% error rate (right). We expect the algorithms to perform faster when the data contains few errors, because the seeding is more effective and it is more likely that a few alignments will suffice to produce an estimate. This is what we observe if we compare the average true positive rates. All the algorithms perform faster when the error rate is small. On the other side, our algorithm takes advantage of the seed significance filter to speed up the mapping process when the seed is considered highly significant, which happens more often if the read quality is good. On the other side, for 5% error, both bwa-mem and bowtie2 outperform our mapper for false positive rates above 10−4. Hence, the good performance of our mapper observed 64 BIBLIOGRAPHY [21] M. Farrar, “Striped SmithWaterman speeds database searches six times over other SIMD implementations,” Bioinformatics, vol. 23, no. 2, pp. 156–161, Jan. 2007. [Online]. Available: http://dx.doi.org/10.1093/bioinformatics/btl582 [22] T. Rognes, “Faster smith-waterman database searches with inter-sequence simd parallelisation,” BMC Bioinformatics, vol. 12, no. 1, pp. 1–11, 2011. [Online]. Available: http://dx.doi.org/10.1186/1471-2105-12-221 [23] S. A. Manavski and G. Valle, “Cuda compatible gpu cards as efficient hardware accelerators for smith-waterman sequence alignment,” BMC Bioinformatics, vol. 9, no. Suppl 2, pp. S10–S10, Mar 2008, 1471-2105-9-S2-S10[PII]. [Online]. Available: http://www.ncbi.nlm.nih.gov/pmc/articles/PMC2323659/ [24] D. V. Hinkley and E. A. Hinkley, “Inference about the change-point in a sequence of binomial variables,” Biometrika, vol. 57, no. 3, pp. 477–488, 1970. [Online]. Available: http://biomet.oxfordjournals.org/content/57/3/477.abstract [25] H. Li, “Aligning sequence reads, clone sequences and assembly contigs with bwa-mem.” arXiv:1303.3997v2, 2013. [Online]. Available: http://arxiv.org/abs/1303.3997 [26] B. Langmead and S. L. Salzberg, “Fast gapped-read alignment with bowtie 2,” Nat Meth, vol. 9, no. 4, pp. 357–359, Apr 2012, brief Communication. [Online]. Available: http://dx.doi.org/10.1038/nmeth.1923