Package Bio :: Package Fasta :: Module FastaAlign
[hide private]
[frames] | no frames]

Source Code for Module Bio.Fasta.FastaAlign

 1  """ 
 2  Code to deal with alignments written in Fasta format (OBSOLETE). 
 3   
 4  This module is considered obsolete and likely to be deprecated.  Please use 
 5  Bio.AlignIO instead for reading and writing alignments in FASTA format. 
 6   
 7  This mostly just uses the regular Fasta parsing stuff written by Jeff 
 8  to deal with all of the input and output formats. 
 9   
10  functions: 
11  o parse_file() 
12   
13  classes: 
14  FastaAlignment""" 
15  # standard library 
16  import string 
17  import os 
18   
19  # biopython 
20  from Bio.Align.Generic import Alignment 
21  from Bio import Alphabet 
22  from Bio.Alphabet import IUPAC 
23  from Bio import Fasta 
24   
25 -def parse_file(file_name, type = 'DNA'):
26 """Parse the given file into a FastaAlignment object. 27 28 Arguments: 29 o file_name - The location of the file to parse. 30 o type - The type of information contained in the file. 31 """ 32 if type.upper() == 'DNA': 33 alphabet = IUPAC.ambiguous_dna 34 elif type.upper() == 'RNA': 35 alphabet = IUPAC.ambiguous_rna 36 elif type.upper() == 'PROTEIN': 37 alphabet = IUPAC.protein 38 else: 39 raise ValueError("Invalid type %s passed. Need DNA, RNA or PROTEIN" 40 % type) 41 42 # create a new alignment object 43 fasta_align = FastaAlignment(Alphabet.Gapped(alphabet)) 44 45 # now parse the file and fill up the alignment object 46 align_file = open(file_name, 'r') 47 48 parser = Fasta.RecordParser() 49 iterator = Fasta.Iterator(align_file, parser) 50 51 cur_align = iterator.next() 52 while cur_align: 53 fasta_align.add_sequence(cur_align.title, cur_align.sequence) 54 55 cur_align = iterator.next() 56 57 return fasta_align
58
59 -class FastaAlignment(Alignment):
60 """Work with the Fasta Alignment format. 61 62 The fasta alignment format is basically the same as the regular ol' 63 Fasta format we know and love, except the sequences have gaps 64 (represented by -'s). 65 """
66 - def __init__(self, alphabet = Alphabet.Gapped(IUPAC.ambiguous_dna)):
68
69 - def __str__(self):
70 """Print out a fasta version of the alignment info.""" 71 return_string = '' 72 for item in self._records: 73 new_f_record = Fasta.Record() 74 new_f_record.title = item.description 75 new_f_record.sequence = item.seq.data 76 77 return_string = return_string + str(new_f_record) + os.linesep + os.linesep 78 79 # have a extra newline, so strip two off and add one before returning 80 return string.rstrip(return_string) + os.linesep
81