200 likes | 374 Views
Sequence formats. Say we get this protein sequence in fasta format from a database:. >FOSB_MOUSE Protein fosB. 338 bp MFQAFPGDYDSGSRCSSSPSAESQYLSSVDSFGSPPTAAASQECAGLGEMPGSFVPTVTA ITTSQDLQWLVQPTLISSMAQSQGQPLASQPPAVDPYDMPGTSYSTPGLSAYSTGGASGS
E N D
Sequence formats Say we get this protein sequence in fasta format from a database: >FOSB_MOUSE Protein fosB. 338 bp MFQAFPGDYDSGSRCSSSPSAESQYLSSVDSFGSPPTAAASQECAGLGEMPGSFVPTVTA ITTSQDLQWLVQPTLISSMAQSQGQPLASQPPAVDPYDMPGTSYSTPGLSAYSTGGASGS GGPSTSTTTSGPVSARPARARPRRPREETLTPEEEEKRRVRRERNKLAAAKCRNRRRELT DRLQAETDQLEEEKAELESEIAELQKEKERLEFVLVAHKPGCKIPYEEGPGPGPLAEVRD LPGSTSAKEDGFGWLLPPPPPPPLPFQSSRDAPPNLTASLFTHSEVQVLGDPFPVVSPSY TSSFVLTCPEVSAFAGAQRTSGSEQPSDPLNSPSLLAL Now we need to compare this sequence to all sequences in some other database. Unfortunately this database uses the phylip format, so we need to translate: Phylip Format: The first line of the input file contains the number of sequences and their length (all should have the same length) separated by blanks. The next line contains a sequence name, next lines are the sequence itself in blocks of 10 characters. Then follow rest of sequences.
Sequence formats Fasta Phylip >FOSB_MOUSE Protein fosB. 338 bp MFQAFPGDYDSGSRCSSSPSAESQYLSSVDSFGSPPTAAASQECAGLGEMPGSFVPTVTA ITTSQDLQWLVQPTLISSMAQSQGQPLASQPPAVDPYDMPGTSYSTPGLSAYSTGGASGS GGPSTSTTTSGPVSARPARARPRRPREETLTPEEEEKRRVRRERNKLAAAKCRNRRRELT DRLQAETDQLEEEKAELESEIAELQKEKERLEFVLVAHKPGCKIPYEEGPGPGPLAEVRD LPGSTSAKEDGFGWLLPPPPPPPLPFQSSRDAPPNLTASLFTHSEVQVLGDPFPVVSPSY TSSFVLTCPEVSAFAGAQRTSGSEQPSDPLNSPSLLAL So we copy and paste and reformat the sequence: and all is well. Then our boss says “Do it for these 5000 sequences.” 1 338 FOSB_MOUSE MFQAFPGDYD SGSRCSSSPS AESQYLSSVD SFGSPPTAAA SQECAGLGEM PGSFVPTVTA ITTSQDLQWL VQPTLISSMA QSQGQPLASQ PPAVDPYDMP GTSYSTPGLS AYSTGGASGS GGPSTSTTTS GPVSARPARA RPRRPREETL TPEEEEKRRV RRERNKLAAA KCRNRRRELT DRLQAETDQL EEEKAELESE IAELQKEKER LEFVLVAHKP GCKIPYEEGP GPGPLAEVRD LPGSTSAKED GFGWLLPPPP PPPLPFQSSR DAPPNLTASL FTHSEVQVLG DPFPVVSPSY TSSFVLTCPE VSAFAGAQRT SGSEQPSDPL NSPSLLAL
We need automatic filter! • A program that reads any number of fasta sequences and converts them into phylip format (want to run sequences through a filter) • Program structure: • Open fasta file • Parse file to extract needed information • Create and save phylip file • We will use this definition for the fasta format: • The header starts with > • The word immediately following the ">" is a unique ID; next two words are the name of the sequence, the rest of the header is a description. • All lines of text are shorter than 80 characters.
Pseudo-code fasta→phylip filter • Open and parse fasta file • From each header extract sequence ID and name • Open phylip file • Write “1” followed by sequence length • Write sequence ID • Write sequence in blocks of 10 • Close file
The other way too: pseudo-code phylip→fasta filter • Open phylip file • Find first non-empty line, ignore! • Parse next line and extract first word (sequence ID) • Read rest of line and following lines to get the sequence, skipping blanks • Read next sequences • Open fasta file, and for each sequence: • Write “>” followed by sequence name • Write sequence in lines of 80 • Close files
More formats? phylip-fasta • Boss: “Great! What about EMBL and GDE formats?” Coding, coding,.. : 12 filters! phylip fasta fasta - phylip
Still more formats? • Boss: “Super. And Genebank and ClustalW..?” Coding, coding, coding, ..: 30 filters • Next new format = 12 new filters! • This doesn’t scale.
Intermediate format • Use an internal format as intermediate step: • Two formats: four filters phylip-internal phylip internal-phylip internal internal-fasta fasta - internal fasta
Intermediate format • Six formats: 12 filters (not 30) • New format: always two new filters only i-format
Let’s build a structured set of filters! • Each x2internal filter module: parse file in x format, extract information, return sequence(s) in internal format • Each internal2y filter module: save i-format sequences in (separate) file(s) in y format • Example: Overall phylip-fasta filter: • import phylip2i and i2fasta modules • obtain filenames to load from and save to from command line • call parse_file method of the phylip2i module • call the save_to_files method of the i2fasta module
Internal representation of a sequence Attributes: type (DNA/protein), name, and a unique ID number Isequence.py (part 1)
Example: fasta/phylip filter First fasta2internal. Each x2internal filter module: parse file in x format, extract information, return sequence(s) in internal format fasta2i.py
Then internal2phylip. Each internal2y filter module: save each i-format sequence in separate file in y format i2phylip.py
Putting the parts together: Fasta/phylip filter • Import parse_file method from fasta2i module • Import save_to_files method from i2phylip module • Obtain filenames to load from and save to from command line • Call parse_file method • Call the save_to_files method fasta2phylip.py NB: nothing in code about phylip and fasta below this point..
Sketch for i2embl filter module Use i2phylip filter as template, much of the code can be reused. NB: Same method name save_to_files Only these parts have to be rewritten
Complete fasta/embl filter (assuming we have the i2embl filter..) Almost the same code as the fasta2phylip filter: only change is that the method save_to_files is imported from new module fasta2embl.py