All right welcome everyone so uh today we're talking about sequential pattern matching so basically how do we take a string of actions for example and a string of text how do we take language uh translation basically text in one language text in another language how do we take genes in one genome and genes in another genome how do we take states of a Model that represents the world with different time points in that model across different times in the world so basically what we're going to look at is techniques that are very foundational computation and
that are extremely broadly applicable biologically and you know well beyond so we're going to look at uh sequence alignment as a basis for this and specifically dynamic programming which has been pervasive in computer Science as a way to sort of build larger problems as by combining smaller ones and I think this is something that that's pervasive and then we're going to talk about uh very rapid look up through hashing by basically taking the sequential patterns making codes out of them and then searching up these codes very rapidly so we're going to start by introducing the
concept of comparative genomics and evolution as a first application of this and then we're going To look at how we can use computation and reuse it using a very simple example for dynamic programming which is just simply Fibonacci number computation whether you're going to do it recursively from the top down or iteratively from the bottom up and we're going to see the principles of reusing this computation from the bottom up then we're going to talk about what allows us to now apply this principle of dynamic programming to sequence alignment by Understanding this alignment Matrix that
represents every possible alignment between a prefix of one sequence all the way to J and the prefix of another sequence all the way to I and by aligning larger and larger sub strings of two sequences that we're trying to align will be able to sort of build up the overall solution and then we'll introduce the concept of traceback to actually not just construct the maximum score but also construct the maximum Path that gives us the score and then we're going to see how this actually takes us from an exponential runtime to align a pair of
sequences a pair of these trajectories a pair of these sequential uh patterns to a quadratic time then we're going to look at specific applications within the world of alignment uh such as local alignment linear time alignment and linear space alignment by building on what we've Learned and then we're going to introduce the concept of hashing in other words pre-computing these patterns and then searching them very rapidly and specifically hashing is pervasive again in computer science and uh I want I want you to start thinking about content based search other words I have a document and
I want to search for other documents not based on the title not based on some keywords not based on Citations but based on the content and for text this is something that you can now do with large language models you can basically look at the latent embedding representation of a document and then search for other documents you can think of that as a hashing approach that basically just do does context sensitive hashing that basically says you know based on the location of the space that I'm landing in other documents that are similar are going to
Be close in that space and we're going to talk about the concept of probabilistic algorithms and expected runtime and we're going to put all of these ideas together in this blast algorithm which is very humbly named basic local alignment and search tool but is blasting fast and has like 30,000 citations for version one and 35,000 citations for version two so no pressure um and then we're going to talk about inexact matching and recursive blast and Then we're going to finish with a probabilistic foundations of alignment scor so basically for the first four hours we'll be
doing this and about the next three hours I hope you guys have blocked until like 7 pm tonight because uh it's probably what it should take but we're going to go a little faster all right everybody excited good menu it's like you're ordering at the restaurant you're like I'll have page one so yeah we're having page one okay and Then on Thursday we're going to have page two and I I'll show you page two but here's the basic idea the basic idea is we have some sequential data and this could be frames of a video
of a tennis player sort of making a movement and then you want to translate that in perhaps a script that basically says you know man is swinging and you know the other players running and the ball falls to the side and all kinds of other stuff okay everybody with me here and then the Aliens show up so probably that's not part of the secret so basically the the whole point is that up until now we've been thinking about every data frame in isolation basically the key idea is that the best decoding of a given image
might actually be the alien's Landing but given the context of everything else in the sequential data we want to constrain the model not just based on the current likelihood or maximum posterior decoding of a given Frame but based on all of the frames nearby who's with me here yes awesome great so so again there's a lot of technologies that are uh there today but we're not going to throw all of that to you immediately we're going to eventually throw it all at you but like progressively so that it like intellectually doesn't hurt as much okay
so the question is if I have sequential data how do I represent the Rel relationship between different time Points and how do I represent that relationship across long time slots long time intervals or short time intervals everybody with me here so basically the key idea is it's not every frame in isolation but instead the context of the surrounding frames will basically help guide my interpretation sounds good and then we're you know there's a whole host of families of models we're going to look at Hidden Mark of model models today we're going to look at actually
on Thursday we're going to look at uh you know there there's also gausian mixture models there's Dynamic neural networks and uh you know we're going to sort of start introducing those at at different points in the lecture basically the the there's a lot of sequential models in the age of deep learning so the recurrent neural network was one of the first uh introductions to to this SK space for how how you can use uh a decoder that is aware of time And the challenge of that was how fast information was decaying it was basically decaying
exponentially as you would go through time and then the concept of memory was inserted the concept of long shortterm memory and the concept of long shortterm memory is that you can choose to remember something or you can choose to forget something and there's a remember or a forget gate that basically allows you to now as soon as you've seen something decide that you're Going to need it keep remembering it until you decide that it's time to forget it and how do you decide that well that's the beauty of it the beauty of it is that
you decide that from the data itself so the machine learning model decides that by looking at that data so what you're effectively doing in these models is that you're providing the tools the mechanisms for the model to be able to decide to remember or to forget and so on so forth so there's Gting recurrent units and long shortterm memory modules and a lot of that has been uh basically steamed rolled over with Transformers okay and the concept of Transformers is you have an explicit positional encoding and therefore you can do anything with them okay but
I don't want you to sort of jump into Transformers without understanding the intellectual context in which they fall and that Intellectual context basically means first of all we need to understand contextual information we need to understand time series data we need to understand sequential data sets and that challenge of remembering from previous times to current time everybody with me here awesome good again page one of the menu uh we're going to start with comparative genomics we're going to talk about conservation mutational processes and basically reading Evolutions Notebook by using Evolution to understand genomes and a
lot of people in the class are actually interested in compara to genomics projects for their final projects so it's a nice thing to introduce early on the chairs that uh and you know this desk is made of wood okay it's our cousins the trees it's a little weird to say that but as you're breathing you're constantly fighting viruses and bacteria and these are our Cousins the viruses and the bacteria when you're going to the gym and you know you to really wash your your shoes or just wear sandals there's fungi growing there and these are
our cousins the fungi every single life form on this planet has been around in a continuous chain linking every single thing alive and it's just phenomenal to realize that that basically there's been this Uninterrupted chain of three plus billion years of life in uh you know in this planet and uh comparative genomics is the field of comparing these different life forms to each other by aligning their genomes and basically understanding the processes that gave rise to one versus the other everybody with me here and it's an extraordinary wealth of Life out there I mean again
we are extremely privileged to be among this Extraordinarily beautiful planet but you can see both both the living organisms as part of the beauty of life on Earth but also the inventions that happen at every one of these branching points as you know part of the legacy of life on Earth everybody from here and unfortunately the beauty of evolution is the Simplicity but also the brutality namely that you know over evolutionary time things just go extinct and sometimes they go extinct because That evolutionary Niche has been wiped out by competition maybe that species was so
great at capturing that one Niche that it just extinguished that Niche and all of the food is now gone and you know you could argue about selection for altruism maybe as part of that and sometimes there's like an asteroid that will just you know come and wipe out every organism that has you know big enough lungs to require enough oxygen and then they're gone and Then that gives rise to you know the emergence of other lineages like us uh so we're kind of lucky that this uh you know asteroid killed the dinosaurs because eventually mammals
evolved into humans so alignment is the process of taking chunks of the Genome of one species and then figuring out where they map on another species and again this could be a series of frames in a movie and this could be Text translating what is happening and then these operations between them could be a mapping from one language to another language everybody with me here so basically in this sequential data mapping what you basically have is uh sequences from multiple species that we know are sampled from an ancestor they're basically uh ancestrally the same and
then one of them grew up I don't know in Hawaii and the other one grew up in New York and they kind of grew different over time everybody with me here so what evolutionary alignment and comparative genomics is about is inferring the series of events that diverged from a common ancestor sounds good and this is going to be the first part of the lecture later on we're going to talk about alignment that doesn't necessarily assume a common ancestor which is more like search I'm want to take this Gene Here and search everywhere in all of
the other species on the planet for is the there some kind of similar sequence without necessarily assuming common ancestry for everyone sounds good awesome so again genom white alignments span the entire genome and that's very powerful and comparative genomics allows us to identify functional element what does that mean that means that when you compare the genomes of human as the reference here with dog with mouse with Rat with chicken with fish what you end up with is that different regions are conserved at different rates so if you look here this is the coding model of
a gene dbh that has 12 exons these are the coding accents that encode proteins and then a non-coding accent here which is the after the stop codon you continue translation and that's the less thick bar and in between these little arrows are the introns which are getting spliced out and They're not translated into protein raise your hands if you're with me here can I get a 543 to1 how well are you fing fantastic great so now we can ask what is the level of conservation at every five nucleotide interval every 10 nucle interal or for
every single letter of the genome and what you end up with is these blocks of evolutionary conservation okay and what you notice immediately is that these blocks very much correspond to to protein coding Accents so the proteins that code for the regions that code for protein that encode all of the building blocks if you wish of a cell they're very well conserved everybody with me here you'll also notice that there's some regions that are not very well conserved so for examp that that sorry that are not annotated as accents that are equally well conserned is
everybody with me here so basically between ex 5 and six there's this big conservation Chunk that you know is not annotating what do you think could be going on here raise your hand yeah speak up a little bit yeah I mean probably this CH something that originated in between chicken and red ahuh so what you're looking at is not only How Deeply conserved it is but you're also noticing that it's not conserved at all in the chicken and the fish right so you're you're immediately Saying aha this region here is not just about the evolutionary
conservation of how strongly selected it appears to be by purifying selection in other words by sort of maintaining the the sequence that's there but also also how far is it conserved everybody with me here so as we start thinking about a tree we should basically be thinking both about the strength of conservation the strength of selection but also the extent of selection the how far Something's conserved right so the first thing you're noticing is aha maybe it was just recently evolved and that's going to cool right so it's not just like oh yeah genomes were
perfect 3.5 billion years ago and you know we've been losing this ancestral awesomeness since then you know Evolution makes mistakes and you know replication will very often lead to mutations and substitutions but if replication was Perfect I like to joke that we'd still be perfectly replicating bacteria that basically we wouldn't have evolved so part of the beauty of evolution are the mistakes because the mistakes will sometimes actually help either in the same environment or a new environment everybody from here so basically again this is an example of something that arose after Divergence from chicken and
that subsequently these mistaken you know Branch actually became beneficial and selected within the mbody awesome yeah ah so the next question is what the heck does it do right so what do you think it does yeah could be some type of Target Gene regulator yeah yeah it could be a regulatory region so what fraction of the human genome codes for protein 1.5% it's insane we have so much genome thank you so much and so little of it actually includes proteins how much of the genome is there's a lot of estimates you could say well how
much of it is under selection but when you ask that the question is where with what confidence with how many species at what lineage at what evolutionary distance Etc but estimates range between 7% and 20% that might actually be function right under under Selection so that means that only a tiny fraction of the functional elements in the human genome are actually protein coding genes we can now reliably annotate about 20,000 protein coating genes each of them has you know a bunch of exin so there's a much larger number of these little tall blue chunks but
then these other elements that might be functional and conserved and you can see some of them here they actually make up a lot more Landscape than the protonic gen and if you now ask what fraction of disease causing mutations overlap protein coding exence it's only about 7% so you can look at the 7% in two ways first you could say well by chance it would expect 1.5% so they're hugely over represented in prot coding genes but threefold and more everybody with me here so basically yes protein coding Mutations are over represented in the disease Associated
variant the vast majority 93% of them are just non-coding so as you start thinking about what causes disease in Alzheimer's in schizophrenia in you know nearly every complex trait the vast majority of that is actually non-coded so the question is is this non-coded because that that's one possibility why is it conservative if it doesn't code for protein one possibility is that it's not cing what's Another possibility yeah just by chances like was good one possibility is that conserved just by chance that it's not protein coding it's not not coding and it's just conserved by chance
what's another possibility another possibility is that it actually is protein coding who built this blue annotation humans we did humans make mistakes as the Latins the Romans would Say so to air is human so humans make mistakes maybe this annotation just not perfect and my group has been working on annotating the protein coding set of the human genome for the last 20 plus years and we've discovered thousands of new protein coating exdents this could be one of them maybe this is actually protein Cod I love talking about these because I like to say that the
beauty of interdisciplinary Collaborations is that if we had only computer scientists the computer scientist would say oh the biology must be perfect if we only had biologist they will say oh the computer science must be perfect the beauty of having interdisiplinary collaboration where both are in the room is that we are doubting every part of the pipeline and suddenly you're like well it's probably a problem with my code and the biologist would say oh you know P This thing happens and you know similarly the experimentalist will say oh I probably used the wrong vial for
this one and the computation person will say oh whoa this happens so I want to doubt everything including the reference annotation of the human genome sometimes you'll find something that looks a like a protein coding Gene and you know I I remember when I was U 19 years old I was basically looking at the human genome and I found that within the hawk genes These are the developmental genes that cause the anteroposterior patterning of humans that basically cause you to have a head and arms and legs Etc these genes which are arguably the most important
genes in human development I I looked at them in multiple species I'm like that can't be a mutation it's probably sequencing error and I I told the people who were sequencing The genome excuse me I think that these are errors and they're like oops okay we Look into that it was errors so it's so important to be able to doubt the primary data to just doubt every part of the pipeline and one possibility is that whoever those now blue annotations just missed one of them and that's completely normal everybody with me here hi digress but
hope I hope you guys are enjoying all of these digressions raise your hands if you're with me yeah awesome which wasar stuff good again there's a lot of lessons to be to be Learned here um all right so we can now start estimating the level of constraint we can basically say how do we do this we could count the number of edit operations we could estimate the number of mutations including some back mutations we could incorporate information about the neighborhood we could estimate the probability of a hidden State and we're going to talk about hmm
on Tuesday or sorry on Thursday so in two days um we can use Philogyny to estimate the Tre mutation rate or the rejected substitutions we're going to talk about philogyny later in the semester and we could also allow different portions of the tree to have different rates and we're going to talk about phenetics and phenomics later on so there's many many different ways of measuring what constraint means but after we've developed these ways we can then say how do I measure this track how do I infer this track everybody from Here and then when I
have that I will be able to infer conserved elements but to even have that the first thing that I need to know is what are the corresponding regions between these species is everybody with me here I need to know where does this chunk of the human genome map to in dog and mouse and rat and fish Etc okay so that's the first building block yeah um okay everybody with me but after you've been able to do this After you've mapped every nucleotide meticulously on top of every other nucleotide across multiple species magic happens okay and
what's that magic that magic is that you see the beautiful little stars what are these Stars they are places of perfect conservation these are letters that have not changed in about 16 million years between these species so what are we looking at here here we're looking at sakaris sakaris zacharo mikis zacharo is Sugar mikis is fungus so sugar fungus C CA uh so uh this is the uh yeast that makes beer in other words um yeah Baker yeast makes beer makes bread makes everything everybody from here our best friend um so s and then paradoxus
mikai Banus three close relatives roughly at the distances of mouse and dog and right everybody with me here good so now what we need to figure out in this lecture is how to line up these nucleotides on top of each other and that's what dynamic Programming and Alignment will be about but after you've done that what you basically see is that in these islands of conservation you basically see that there's a beautiful match with known regulatory control elements remember you're asking you know if it's not protein coating what does it do well this is very
creatively named the Tata binding protein what does it bind Tata this is a mig1 this is galor this is a Galactose regulator so when the yeast encounters glucose it will eat up all the glucose and it will shut off galactose metabolism but when it doesn't have glucose namely when migan does not bind here and migan represses galactose genes this is basically a glucose sensor so basically when the regulator that senses glucose senses glucose it binds here and shuts off both of these genes but when there is no glucose and there is Galactose then gfor binds
here sensing galactose the signal is sent to gor hey gfor turn on G one and G 10 and these are Transporters and metabolized that basically will let in gluc galactose and then shot it up everybody can hear so G four recognizes cgg spaced by 11 nucleotides from ccg CG ccg CG ccg let's read it on the opposite strand on the opposite strand is CG ccg CG ccg okay it's kind of cool it's a pal drum it's a reverse palindrome basically on The CC Strand and on the Watson strand basically read the same way everybody with
me here so that basically means that by reading Evolution we can infer functional elements everybody here good so how do we read Evolution so how do we align stuff and we're going to talk about later on uh how the patterns of evolution of regulatory motives as I showed you are different from those of micr rnas and they're different from RNA structures and they're different from Prote coding genes and so on so forth so you know a big chunk of my career early on was basically how do we Define evolutionary signatures for all these different types
of functions to basically understand the human genome and those signatures have now become the foundation for the protein coding Gene set through programs that we wrote you know RNA structures micr rnas regulatory motives okay is kind of cool you you guys can have an impact you know just Understanding the basic building blocks and if you look at Alpha fold extraordinary Advanced but what makes Alpha fold so awesome at the basis is a multiple species alignment so sequence alignment and genome alignment is crucial to nearly everything and inferring protein structure without it is silly because it
provides such extraordinary information about which residues are covariant which Residues are important which residues are conserved everybody with me here awesome all right so now how do we align these chunks of the genome how do we basically build these basic genome alignments what we're going to do is model an evolutionary process we're going to basically say that I'm going to Define some events that will allow me to express Evolution as a forward process and I'm going to work it backwards to try to infer what are the events that Most likely R gave rise to the
sequences that I see everybody here so basically basic building look number one mutation change a letter to another letter why because polymeres is doing an extraordinary job but it can't be perfect so as it's transcribing as it's sort of you know painstakingly copying one version into another like an you know monk in the you know Middle Ages transcribing the ancient scripts it's basically copying the DNA and again Transcription is used for RNA so I apologize for this as it's replicating the ancient text into a new copy for the next cell that is about to be
divided it sometimes makes a mistake and that will basically uh give us one evolutionary uh operation another evolutionary operation is like it's busy you know uh copying a and then it's so you know uh eager to get to the G it forgets to copy the C okay and these are deletions it could also simp Mistakenly incorporate an additional nucleotide maybe there's a little bit of a pause it's sort of you know it's got a nucleotide at hand it will put it in and then it'll find the next one okay and again there's reasons why polymerus
will sometimes slip sometimes if it has like a a a a a a it will just add another a because it kind of lost track of where it is again I'm anthropomorphizing but it's actually what happens everybody can here okay awesome so then you start with The sequence and you end with the sequence and the question is you know how did you get from one to the other so basically that's the forward process that's the simulation the inverse process is okay I have the two end points I want to figure out how I got from
one to the everybody with me here awesome so how are we going to do this we're going to formalize the problem we're going to go from this biological question to a computational Formalization we're going to define the set of evolutionary operations talked about insertion deletion imation and that's basically all encompassing it allows us to basically Define everything else as you know made of that and we can include for example symmetric operations that allow for time reversibility why because we're not going to be comparing uh a genome from 60 million years ago to human we're going
to be comparing Mouse and human that diverg 60 million years Ago so basically we're going to have to walk back 60 million years and forward 60 million years and we want these to be reversible so that I can take human and mouse and say okay what are the operations that I need to transform one into the other everybody here and of course when you have a philogyny then you start inferring the state of the ancestor based on the state of all of the uh genomes that I have and we going have a fellow genetics lecture
later in The semester everybody with me here awesome again there's some exceptions where methylated cpg n nucleotides will basically say change for cpg to CPA in a non symmetric way but you know don't worry about that second we need to Define an optimality Criterion basically says I have human I have Mouse and I could decide to just go from Human all the way to bat and then from bat back to mouse or I could say well I want a minimum number of operations everybody Me here again we could talk about the minimum number of operations
where every every operation every action has the same cost but then I might be tempted to say oh well there were three billion insertions and three billion deletions and then I deleted the entire Human Genome and I inserted the entire Mouse genome right so two approximations great um but it's terrible obviously it gives you zero information basically our Goal is not just hey can I make fun from the other it's to actually infer what was the most likely series of events and that's why we want to have you know some kind of aoms Razor or
ify the minimum number of operations but also the minimum cost because maybe three mutations might be more likely than two deletions for example okay just because of the way that polymeris works a deletion might be just a more costly operation maybe when I start deleting When I deleting 10 nucleotides deleting 30 nucleotides is more or less the same cost maybe the an initial Gap initiation cost and then the Gap extension cost is much smaller is everybody with me here so again how are we coming up with these based on the frequency of these types of
events it's not just oh I'm pulling up these scores out of a hat no we wanted a probabilistic interpretation of those events everybody with me here so that basically means that if these events are Very rare for example insertion deletion then they will have higher cost if mutation is very common it will have a lower cost and you can think about is frequency to cost transformation as a minus log 10 P okay so if something is very rare it will basically have a very high minus log 10p value and a very high cost everybody from
here awesome all right then when we have this Optimality Criterion for example minimizing either the number of oper or the total cost of operations by weighing them based on how unlikely they are we want to design an algorithm that achieves that optimality or approximates it so basically the question is is that solution even tractable well that depends on the assumptions that we're making in translating this problem and there's a lot of trade-offs on one side you want biology relevance so you want Correctness you want to handle all the special cases you want to be very
predictable on the other hand you have computational desired D diata for example you want it to be trackable you want it to be computable you want it to be easy to implement you want to you know sort of simplify you want to have an easy algorithm a rapid runtime so therefore you're going to be making some assumptions and those assumptions might limit the generalization to special Cases but it might speed up dramatically your algorithm so as you guys are designing your final projects I want you to always think about these tradeoffs sometimes I'll have to
make some assumptions that will make things feasible but then I will have to you know treat the special cases afterwards everybody with me here awesome all right so here's one formulation longest common substring what a substring means it means no insertion and deletions I'm Taking a string and finding a portion of that string so what's the longest common substring between S1 and S2 here how many characters and what is it so TCA sounds great three anybody can find fast bigger yeah AGT TCA AG GT AGT sorry say it again oh do you mean like no
no no no no no gaps no gaps sorry don't Take yeah midle out so if I align the end I only get three letters if I align the middle TCA then I get the extra G here so I get four letters okay so now how did you guys do it what algorithm did you run how did you do it in your head raise your hands yeah actually you spoke before let's get another person how did you run yeah go ahead Series like three letters comp good so basically for every series of three letters I can
just go and you know search for it in both so basically that's sort of seeding and then extending great put another Al and that sort of leverages human pattern matching so basically humans are good at sort of finding patterns and therefore you can kind of seed multiple patterns and kind of extend them out Yeah you keep one sequence fix speak up please you can keep one fixed and then almost imagine the other one like sliding good good good good so that's the human version this is the machine version The Machine version basically says oh why
don't I keep one sequence fixed and I'm just going to slide the other one past it so basically for every starting point of sequence one I'm going to choose a starting point of sequence two and then keep trying until I run out Of matches okay so now we can start arguing about the runtime of these algorithms maybe the machine version where you're basically just starting every seed and then you know extending how how long will that run how many starting points are there there's n starting points in one sequence how many are there in the
other sequence M starting points in the other sequence okay so I can basically for every n and M pair of starting points then I want to Check every pair okay and that can be up to M okay sounds like an N Cube algorithm is it actually n Cube not really it's only in Cube if I go go all the way to the end to then say oh it was zero right like I can start here and here and say say oh mismatch mismatch mismatch like why why just like you find one mismatch start again okay
so basically maybe I don't have to go all the way to the end and then it's just an N squar Out is Everybody with me here yes raise your hands 5 4 3 2 1 who's who's with me okay cool F five is is great okay so that's you know just trying every offset and for every offset I basically start checking algorithms I start checking uh you know matches and then I find you know the offset plus one gives me a three match and the offset minus two gives me a four match okay so that's
longest common sub string assuming that there's no Insertions or deletions but we know there are polymer makes those mistakes so then let's look at longest common sub sequence and when you look at lowest Comm common sub sequence you now see that you have an a a g a t and then a TCA at the end so I can get a length of six as a match everybody with me here and what is that length of six it basically implies that there are two deletions one mutation and one insertion does that look familiar it's Basically exactly
the signal that I showed you at the beginning okay where I had two mutations a deletion sorry I mutation two delions and then new sech okay so every single time I create an alignment I'm implying implicitly a series of events sounds good so basically this is the longest common subsequence rather than substring where I can allow gaps and there's insertions and um deletions the third formulation so this Is common sub sequence well all I care about is is it identical or not but how often does c change into a g versus C changing to a
t or an A the frequency with which different letters of the DNA get replaced has to do with their structure an A and A G kind of look alike a c and a t kind of look alike so when I'm copying very often I'll replace letters that actually look a little more like each other okay so Basically I might want to penalize a switch between an A and A G less than a switch between an A and either a t or a c everybody can hear so AG substitutions and TC substitutions are much more frequent
and the reason is that the nucleotides frankly look similar one of them has like two rings and the other one has one ring so the one ring I the one ring nucleotides get replaced more easily and the two ring get replaced more easily everybody with me here let Just go structurally they look more similar and it's the same thing with amino acids amino acids get replaced with different frequencies and the reason there is twofold one hand it's the codon encoding of some amino acids that is very similar to some other amino acids therefore TG and
TCG might be more easily replaced everybody with me here that's one reason the other reason and that's mutation the other reason is Selection selection basically means that amino acids with similar properties will be more frequently swapped okay so if you look at I don't know isol losing and loing and you look at what is the score of substituting one for the other it's actually not even a penalty it's a positive score why because if you find another alignment that aligns a tryptophan with an isoline you're like that's not a real Alignment that's not due to
divergent evolution from a common ancestor If instead you say oh well you know in this alignment here I see that an isolution is aligned with a Lucin you're like well that's probably a real alignment everybody with me here so the frequencies with which amino acids get substituted with each other tells you about the Rarity again of these events and therefore tells you about the score That you should be using in this minus log p uh equivalence between scores and frequencies everybody can here awesome who feels that they're learning a lot good who feels that they
want to stand up and stretch yes I do so please everybody stand up and it's mandatory you don't have a choice everybody stand up and stretch reach for the guy please good now take your right arm and look For your left neighbor with your right arm now your left arm all the way to the other side and now shake shake shake your hands and now please do this with me hop good what am I doing I'm stimulating blood flow to your brain why because I'm gonna need it all right and of brain what appreciates these
breaks yes good awesome good so we've learned a lot and uh we're going to start applying it now and now We're going to start talking about alignment algorithms up until now we've been talking about probabilities we've been talking about translating biology to computer science we've been talking about you know the process and so so forth now we're just going to do algorithms okay and algorithms are super fun and uh let's start doing so we now have these two sequences you know it's been our example from the beginning a c GT C A DCA and I
want to know um how Many possible alignments are there between these two sequences I have n characters here I have M characters there how many alignments are there you can think of an alignment as basically placing gaps because if I know where to put the gaps the rest is obvious is everybody here and how many ways are there to place the gaps at every position I could choose to place a gap or not okay and therefore with a total maximum length of N plus M because frankly n plus n basically means I have all of
Miles here and all of human here remember the part where I'm deleting three billion letters and I'm adding three billion letters not what happened but you know it is the worst possible case because after that it's just like yeah well I have you know seven billion gaps okay so that you know at that point this is what I call a non-boring alignment so non-boring alignment is where you don't have a gap Aligned with a gap so how many non-boring alignments are there well we can get into the details of the math but it's basically all
of the ways that I can place you know M gaps let's say into an N plus M squ and a very easy way to think about this is that at every position I can choose Gap or no Gap binary Choice gap or no Gap binary Choice gap or no Gap binary Choice how many binary choices basically n plus M how many options two to the N plus m M Okay and whether it's 3 to the M plus M whether I really need the M plus M you know it doesn't matter what matters is that it's
a exponential number of possible alignment can everybody see that yeah awesome good so now how do I align how do I search through an exponential number of alignments two to the end possible alignments in polinomial time and that's where dynamic programming comes in um so every time I say Dynamic Program you should seeing uh and there will be times throughout the lectures okay so what is dynamic programming dynamic programming is a way to turn exponential runtimes into polinomial run times it's a way to avoid repeated work okay and uh very simple introduction is through uh
let's say the computation of a recursive function so we're going to start with Fibonacci numbers fonan numbers are absolutely sunning and beautiful and they're found Everywhere and if you look at the length of the size of the notus shell as it's growing out it starts with 1 1 2 3 5 8 13 21 34 and it's got this beautiful spiral why is that super easy because at every time it grows roughly whatever size it is okay because it has a scaffold upon which it can grow and therefore every time it kind of adds the previous
two numbers to the next number and then what you end up with is 1 + 1 2 1 + 2 3 2 + 3 5 5 + 8 13 13 Plus 8 21 And so so forth with me here so Fibonacci numbers are simply oh and yeah so um sorry that's my little cat Okay so Fibonacci numbers are expressed uh very very simply where every number is the sum of the last two numbers and they're found everywhere in nature including the number of rabbits that you get at every generation the spirals in your Romanesque in
a cone flour The Ordering of leaves the number of leaves per height of a tree and so so for Okay now I can Define them recursively I can write a super simple python script that basically says you know def Pon of n if Nal 1 or n equals 2 return one otherwise return n minus one plus n minus everybody with me here how does that work you basically call Fibonacci of six it will call Fibonacci of five Fibonacci of four but Fibonacci of five will call Fibonacci of 4 and fonal 3 and to solve fonal
4 I'm going to have to call fonal 3 and fonal 2 and to solve Fonal three I'm going to have to call fibon of two fibon one how many times did I call Fibonacci of two a bunch of times fibon of three a bunch of other times you four a bunch of times as well Etc at every point I'm doubling the number of things that I need to do and to to solve Fibonacci of seven I basically need to have a tree that's as big as six and a tree that's as big as five okay
so with every increasing single step of the Fibonacci Number that I'm calling I'm doubling effectively the number of operations everybody here so I hope you can all see that it's an exponential function basically because every time I'm kind of adding up roughly that number then eventually it grows exponentially everybody here awesome so then what's another approach instead of just solving it from the top down I'm going to solve it from the bottom up you ask me to compute Fibonacci 100 I'm Going to do one better I'm going to compute every number between zero and 100
and that's actually cheaper than Computing 100 without saving my work why because I can make a table and I can fill out every entry in that table from the bottom up I'm going to say well that's super easy 1 1 2 3 5 8 Etc and then I want 100 no problem I just look at the 100th entry why is that faster because I've only computed Fibonacci of two once and Then when I need it here I'm going to reuse it and when I need it here I'm going to reuse it I'm going to need
it I'm going to reuse it when I need it here I'm going to reuse it so I don't have to compute it over and over and over again is everybody me here so that's the principle of bottom up computation that's the principle of I'm going to make a table and every sub problem that I'm going to need at any point I need it I will have it already Computed can I get a 543 to1 for how well you're following here awesome great so what did etive solution do it revealed The Identical sub problems it ordered
the computation to enable reuse of the results systematically filled in the table of the results and it expressed the larger problems from their subp parts and the ordering mattered the naive top down was super slow because the results of the small problems were Not available so we had to recompute them every time but the systematic bottom up approach was successful because it filled in the table of sub problems um in order and it looked up the solutions instead of Rec Compu everybody with me here aw all right now how does that apply to um sequence
alignment what we need to figure out is how can I store every possible subalignment of two Sequences so that it's available when I need it everybody with me here so I have to align a sequence of n nucleotides and a sequence of M nucleotide let's say a th and a th and I want to create a data structure that will store the results so that I can reuse them okay how am I going to do that how do we apply the programing to sequence here's what I'm going to do I will basically say that the
score Not the solution but the score of aligning sequence one all the way to the 14th character and sequence to all the way to the 10th character is going to be stored at position 1410 in a matrix every here so I'm going to compute the best alignment recursively and I'm going to say that you know at every break point I will be able to store the score of aligning all the way up to that character and all the Way up to that character in this Matrix okay and then when I want to compute the score
of aligning all the way to here and a then I will reuse the well all the way to here as a sub problem everybody with me here so I'm going to basically compute every possible subalignment score again not the full alignment just the score of that alignment all the way up to a Point and then the next one I'm going to say well it's just that plus inserting a mismatch between C and G okay so I'm going to compute this one iteratively by building on the one computed all the way to the gray ones so
I've computed all the way to the gray acgt to TA whatever that score might be I don't care I will reuse that score to infer the score of also aligning CNG G is everybody with me here so this Diagonal Arrow basically says I can calculate that by inserting the cost of aligning C with g whatever that cost might be and I can look it up to whatever score I had from before going all the way to the previous in this one like IUS one and J minus one can I get a 54321 how well you're
falling see some fives all fives all fives great okay awesome any questions awesome good so that's one option another way to get that is to Basically say well I'm going to now build on this alignment all the way to there and this alignment all the way to here and simply insert a gap aligned with a G okay so basically I can extend this one by nothing and I can extend this one by one and suddenly it's the same thing but it's going to be whatever score I have here plus the cost of a gap everybody
here awesome and then the third option Is I'm going to build on Gray up to here HGT four characters here and three characters there and I'm going to insert an additional character only from one sequence aligned with a gap in the other sequence and here I'm going to pay the penalty of a gap so what's my procedure my procedure is wherever I am in the The Matrix I've so I've stored all of the numbers that I will need and then I will choose to build this alignment from one of only three options either I'm going
To take whatever score I had up and to the left and I'm going to add the additional cost of this mismatch or I'm going to go straight up and I'm going to add the additional cost of a gap or I'm going to go to the left and add the additional cost of a gap but for 3 to one who's with me awesome so what I'm doing is that I'm building up a more complex problem from a simpler problem and I'm going to store these results in position 53 because I'm Aligning five nucleotides to three nucleotides
I'm going to look up either 42 or 52 or for treatment okay plus whatever cost all right and these entries are just points in this Matrix so now that's kind of cool I can basically fill out the Matrix because every time I will need to compute an entry I can just have the one above diagonally and to the left already precomputed then how can I fill out the whole Matrix well I just go top row down This way or I can do column by column or I can go row you know diagonal by diagonal doesn't
matter as long as I have the three numbers already computed when I'm about to compute the fourth okay now when I get to the bottom here I basically have a score of a line in this to that sounds good but that's a score that's not an alignment what I need then is to figure out how did I get there and how did I get there what I'm Going to do is at every point when I choose that oh the best score is given by that I'm going to store an arrow pointing to who gave me
the best score okay so at every position of computed I stored an arrow for who gave me the best score if two different ones give me the best score no problem I'll store two arrows either one will do okay and then just choose one at around every everybody with me here so I'm now Computing the scores and some Little breadcrumbs that tell me how I can get back home and these breadcrumbs will basically point up left or diagonal at every position and when I'm at the bottom right I will Trace back these pointers and get
to construct the full align is that kind of fun awesome all right so guess what every path through the Matrix is a onetoone mapping to an alignment of two sequences because as I'm walking back I'm like oh this gave me the best score that gave me the best score this gave me the best score that gave me the best SC best score best SC best score I'm tracing that but what am I doing I'm basically saying oh I'm constructing an alignment of a with a c with c g with c t with t g with
g a with a and an insertion here and two deletions there everybody with me here is this kind of fun right we basically took the problem of Aligning something with something else this could be a sequence of events in an image frame with a sequence of you know um transcriptions of what's happening at the video it could be a sequence in English to a sequence in French it could be a set of genes to a set of genes it could be a set of nucleotides to set of nucleotides it could be a set of hidden
States in a hidden State Mark of model a hidden Mark of model to a set of observations that approach will apply Everywh and that approach I can just do by progressively building it from the bottom everybody with me here awesome who thinks this like awesomely awesome I think it's awesome all right so basically this this one to one mapping between alignments and paths and this path exactly corresponds this alignment of match match match insertion mutation match match insertion match deletion Okay everybody with me here and again notice I said them backwards because you know it's
equivalent deletions in One Direction these insertions in okay so every entry corresponds to score and every path corresponds to a okay good you guys basically got it so we can start at the top left compute all the way to the bottom right save our little arrows and then start at the bottom right walk up by following the little arrows and what we get is an 11 Boom okay and this cute animations and okay what was the key the key is that I'm only using local update rules why because I'm Computing an N byn Matrix or
you can think of it as simply n Square I'm Computing the whole Matrix how long does it take me to compute every point in that Matrix it takes me a constant time that's the beauty that's the power and now I've computed all the possible scores and there are only n s Scores that's what's crazy even though there's an exponential way to Traverse that Matrix because at every point I can decide to go up to the left or diagonal okay so there's an exponential number of possible paths but I found the best one in quadratic time
because to fill the Matrix I just need quadratic time and I just need to remember my pointers and by following the pointers I'm coming Back sounds good awesome okay and then if you really want to have fun you can basically uh let's see uh alignment Excel you can basically um code it all up in Excel it's kind of fun sorry I'm a geek um so basically here I'm basically saying oh well here's my align score Matrix okay I can basically change the score to be 3 and3 and if I change that notice the alignment below
is changing if I change that to I don't Know two and two notice the alignment is actually changing how's that possible well it's possible because I'm basically first saying okay what is the local Point only match or mismatch between all of these nucleotides okay and that's just looking up from here here everybody here then I can basically say oops then I can basically say okay now here what Is the maximum of going from the top from the left or from diagonal and this is just the maximum of three possibilities it's either this one plus whatever
cost is a gap or that one plus whatever cost is a match plus that one you know with whatever cost is a gap everything here okay and then so that gives me the scores all the way down to the bottom the fun part begins um I can basically now say okay let's draw the arrows and these arrows pointing only to The left if the maximum was only that score and it's pointing diagonal if the maximum was only that score it's pointing up if the maximum is only that score okay that's super simple I basically concatenate
if that sum of scores was matching to the one above then I say oh yeah well above gave me that score that's fine if it only to the left then oh yeah left gave me that score it's only to the diagonal oh diagonal gave me that Score everybody with me here and then I can basically trace the path how by basically saying well if I see uh you know pointing up then I'm just going to concatenate whatever sequence that corresponds to and so on so forth and then in the end I can actually compute the
actual alignment up here and uh if I change this to I don't know 02 hopefully the alignment will change as well well in this particular case it didn't change but you know in Some cases it will change you see how now it changed well think this is kind of fun okay I I had too much fun coding this um but um yeah I didn't even think I was going to build a whole thing but you know oh could I do this and I don't even know how to code in Excel by the way I just
got to look it up but anyway so you can basically do alignment in Excel spreadsheet by basically sort of figuring out all of these dependencies and it's super simple okay all right so We talked about finding the path so nothing's missing and then the time needed and the space needed are M * n okay now why is this so powerful it's so powerful because we can now start thinking about alignments as simply paths through a matrix and what's an alignment that goes all the way up and then all the way to the left this is
the oh I'm just deleting one you know three billion nucleotides of human and I'm adding Three billion nucleotides of mass is that a great alignment not really right so I'm paying the cost of six billion deletions that can't possibly be a reasonable way okay so now the the question is how far off the diagonal should I go how many gaps should I tolerate before I start saying nah don't even bother looking there because by the time you go all the way out there and you come back you're going to pay such a large penalty it's
very unlikely to Surpass the score that just went close to the deag is everybody with me here so I can make a heuristic I can basically say I can basically say why don't I only search within some bounded Windows surround the diagonal right everybody with me raise your hands awesome great so that's basically bounded alignment basically says great I don't have to go elsewhere is this an optimal Solution no no there's some weird cases where you know going all the way out there will be the best align do I care about those cases probably not
maybe when I exit here maybe there's a beautiful diagonal way all the way out there that I'm missing out I'm basically going here paying a bunch of gaps but then I get all of the benefit of a perfect match and then come back in okay yeah some cases I will miss them everybody here what could I do to Capture those well that's the next section what I can do is effectively hashing I can basically just take every CER that's long enough in matches seed my alignment with all those cers and then just go search through
them okay it's like saying oh in a video panel I can sort of find you know some repeated actions for example person opens the door and walks in while they focus on his head okay that's a common pattern I Can kind of search for it everywhere and I can kind of align that to the transcript of a video to basically figure out if it matches everybody with me here awesome so that's a way to do bounded uh computation it's a heuristic it's not guaranteed to be optimal optimal but it's super prac now I'm going to
make a small parenthesis which is super awesome and it basically says can I compute the best score using less space how much space Did I need I need to store all of my pointers and therefore I needed a thousand by a thousand space I needed you know a million entries to remember that entire Matrix that I need to go through to to trace back my pointers every everybody with me here yeah give me a 543 to1 who's with me on this one okay bunch of five this is great okay now what if I just wanted
the score at the bottom right how much space Do I need now raise your hands is it n Square I just want the score lar linear why because you can compute the diagonal and go down that way yeah I can basically take every vertical compute all the way to the end I can take every horizontal and go to the to the end or I can take every diagonal go the way to the end I don't need to store the previous one after I've computed this and I have those scores can just throw Away everything before
and I get the score all the way down to the bottom right who sees this raise your hands yes awesome okay now if I want to also find the PATH if I want to do Trace back could I do it no I see a bunch of people waving no now the social psychology would I be asking you if I couldn't do it so what could be a way so here's a crazy way I can compute with only one column at a time or let's Say two columns at a time still being here I can compute
the best score going all the way to here everybody with me there right and I can compute this score going from left to right I can also compute the score going from right to left can just run exactly the same operations in the other order completely symmetric everybody with me here now if I compute from the left to the right and I store not just these two columns that I'm using but also the middle column and I'm just going to the scores going from the left to the right I'm also going to do the same
thing from the right to the left I'm going to compute best score all the way to here and save only the scores here of course the scores and the arrows everybody with me here then the fun part begins I can just sum up the two columns in the middle and see which column gave me the Best score that's the score of going from the left all the way to anywhere in the middle and from the right all the way to anywhere in the middle the sum of those two scores is the best alignment score between
these two matrices and most importantly I don't care about the score because the score I got on bottom right here and then on the on the top left here but if I get the best score here then I know that the best alignment must Go through there can I get a 543 to1 here be honest I don't expect all fives I see some threes this is great good I know the method is working threes good so that basically means that if I go from the left to the right and I store both the scores and
the arrows from the right to the left and I score both the scores and the arrows then in the middle I have the score of the best alignment going through that middle column and the Arrows but most importantly I know what point it went through why is that super awesome because I now know how to break up that Matrix and I don't need to worry anything here and anything here I don't need to go through there because I know that the best score is going through that point here that basically means that I can run
the algorithm recursively here and then find the best midpoint traversal and Score and arrows through here and I can do that recursively so then in the end I'm Computing only half the Matrix here the first time around only a quarter of the Matrix here the second time around only an eighth of the Matrix here the third time around at every subsequent iteration and in the end it's just like 1+ 1/ 12 plus 1/4 plus 1/8 will that get above two not really okay so I can basically Recursively figure out this Middle Point and then get
to an MN algorithm everybody with me here was that fun good all right so um but much more interestingly I'm only saying this because it's fun and you know why why do math if you can't do some fun hard stuff but much more importantly we don't want just going from the top left to the bottom right sometimes I want to find local stretches that are of high score Is everybody with me here and what are these local stretches these are basically local alignments these are alignments of a sub part of my sequence and I can
do this local alignment by basically saying well you know guess what there was a rearrangement in evolution insertion and mutation and deletion not the only three operations sometimes there's a rearrangement sometimes there's an inversion sometimes there's a translocation sometimes There's a duplication do everybody can hear so now we're capturing a broader set of evolutionary operations and we can find those by searching for local alignments I can take my algorithm and change it slightly and basically instead of just starting from the top left and ending at the bottom right I can start anywhere by choosing another
zero in my three options I can also have the option of just start from scratch and I can end anywhere instead Of always ending at the bottom right everybody with me here awesome I could also say well you know I'm sequencing part of the genome I don't care that this doesn't match all the way to the end of my sequencing read uh you know because the other read might just not be complete so you know maybe I don't want any end Gap penalties and I can you know uh adjust the algorithm oford okay so um
now it gets to something much more fun so the much more Fun is can I find these little local Snippets of strong alignment way way faster how am I going to do that I'm GNA do that by basically hashing and here's the idea the idea starts with a carp Rabin algorithm okay and it basically says I want to match that sequence 31415 which you all know and love okay it's part of the mitb version um and I want to match that really really fast In a super super long sequence how can I do that I
can just simply interpret that sequence as a number you know like 10 10,000 pi oh thats really love it 10,000 pies um and I can search that sequence super super fast everybody with me here so at every point I say does the number match instead of saying do these strings match yes and does the number match has a simple answer yes or no and if the number matches then the sequences match And I'm doing great okay so what can I do I can basically simply compute X and then for every position compute Yi and then
if they match then then they match sounds great this doesn't actually work why doesn't work because Computing Yi is not a unit operation it basically says well take the first digit so I have to read it multiply by 10,000 add up the second digit multiply by a th third digit Multiply by 100 next digit multiply by 10 and so so forth everybody with me here so the problem is that this one is too expensive by itself so so however I can do it using the previous one how by basically saying well I've already computed 23,41
why don't I subtract 20,000 multiply by 10 and add five three operations instead of counting every character everybody here so if I compute This using the previous one this works but the problem is that these numbers can now become enormous basically if I'm trying to match a string of length a thousand I don't have integers that big in my computer I'll have to represent it as a combination of many integers is everybody with me here so what I'm going to have to do instead is basically do a mod operation I don't care to match exactly
I just care to match I don't know one in a thousand make sure that they don't conflict all the time everybody from here so basically what I'm going to do is compute the next number based on the previous one but make them small so that they're computable in linear time okay and the problem is that this doesn't work now because every now and then I'll have a furious hit because I've taken these thousand long stretches and I've you know computed them down to I don't know 100 numbers so there's 100 slots even though I could
have two to the 10,000 so now every now and then I'll have a collision with what probability exactly one in the number of entries that I have in the space that I've mapped them to is everybody with me here awesome and that's basically the foundation for hashing the foundation for hashing is that instead of computing this one character at a time I'm going To map it down to a smaller space where I'm going to look for immediately if they if there's if they don't match I don't need to look at the 10,000 letter string so
I'm saving a thousandfold if I have a th000 entries because I only have one in a thousand chance of matching something that's not me is everybody with me here and if I have 10,000 entries I have only one chance in 10,000 I have a million entries One Chance in a Million okay so then I'm saving a ton of computation because I don't have to compute every digit okay so that's basically the key idea I'm Computing these numbers by subtracting the upper digit shifting by one and then adding the lower digit and so on so forth
okay so now this gives us the foundations for this blast algorithm and I mentioned it has what oh I said 20,000 citation sorry a 100,000 citations everybody with me here so Anyway blast is amazingly awesome and it's only getting more awesome by time you can see that it's getting even more sighted what's the key idea of blast it basically says instead of searching my entire string in a giant database I'm going to now break up my string into smaller chunks and I'm going to search every Chunk in a database of cers and then I'm going
to start exactly as the algorithm that you had mentioned at the beginning we're going from the Inside out I'm going to find Little Seeds of matches and I'm going to extend them out who's with me here 543 to1 yes awesome great so that's the key idea I'm basically going to start with my Cy break it up and I'm going to search in a data base and extend from the center out but it gets even cooler than that instead of just saying I'm going to search for exact matches I'm saying I'm going to search for a
neighborhood of Similar building blocks so if I have the three amino acids pqg I'm going to search for pqg that has a score of 15 P where the Q became e that gives me a slightly lower score P RG and so so forth all the way to some threshold okay so I'm searching even for inexact matches so here I found an inexact match and I extended it out and notice there's not a single stretch of three perfectly aligning amino acids everybody with me here Awesome good so you basically did it this was all of the
stuff that we wanted to cover of basically how to do all that and we already talked about the probabilistic fun foundations we already talked about you know the likelihood ratio of matching versus not matching and the fact that the more rare an amino acid is the higher the information content if that amino acid matches and the more unlike these amino acids are the More uh you know the matching them will be so we covered all of page one which is talked about comparative genomics we talked about how to model Evolution we talked about the principles
of dynamic programming ordering our computation from the bottom up we talked about sequence alignment and why and how dynamic programming applies by taking this Matrix of all subalignment scores getting to the best score all the way to the bottom right we talked about tracing The pointers that we saved to create that path we talked about both linear time bounded dynamic programming searching along the diagonal we called about this crazy linear space where you basically keep breaking it up in half and you know Computing but much more important than that you only need one column at
a time to get the best score then we talked about hashing by basically being able to sort of compute these interpret the strings as numbers And then search for those numbers and those numbers are basically hashes it's it's sort of taking this and projecting it to a lower dimensional space instead of having two to the 10,000 possibilities I only have a th possibilities but that basically means one in a thousand I'm going to get collision between two two things that are actually different for those I'll just have to do the original computation of checking them
one by one and then the Last part was this probabilistic foundations of basically what is the chance of matching or mismatching based on how often are those nucleotide substituted for each other in a related sequence versus by chance which is an unrelated model and this likelihood ratio between the related and the unrelated model is basically the score of matching or not matching who here that they've learned a lot today good awesome I saw 10 up not just five This is great I agree with the 10 all right awesome see you guys on Thursday we're going
to talk about KARK of models and how the same alignment applies to now align state to actual reservations and epoms thank you