Package biana :: Package utilities :: Module FastaWriter
[hide private]
[frames] | no frames]

Source Code for Module biana.utilities.FastaWriter

 1  """ 
 2      BIANA: Biologic Interactions and Network Analysis 
 3      Copyright (C) 2009  Javier Garcia-Garcia, Emre Guney, Baldo Oliva 
 4   
 5      This program is free software: you can redistribute it and/or modify 
 6      it under the terms of the GNU General Public License as published by 
 7      the Free Software Foundation, either version 3 of the License, or 
 8      (at your option) any later version. 
 9   
10      This program is distributed in the hope that it will be useful, 
11      but WITHOUT ANY WARRANTY; without even the implied warranty of 
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
13      GNU General Public License for more details. 
14   
15      You should have received a copy of the GNU General Public License 
16      along with this program.  If not, see <http://www.gnu.org/licenses/>. 
17   
18  """ 
19   
20  from math import ceil 
21   
22 -class FastaWriter(object):
23 """ 24 FastaWriter class to handle outputting sequences in fasta format with customized header 25 """ 26 27 # Sequence field in fasta file should not contain lines more than 80 characters (79 amino acids/nucleotides + '\n') 28 symbol_per_line = 79 29
30 - def __init__(self, out_method, one_line_per_sequence=False):
31 """ 32 FastaWriter constructer, creates an instance of FastaWriter 33 """ 34 self.out_method = out_method 35 self.one_line_per_seq = one_line_per_sequence 36 return
37
38 - def _convert_sequence_to_fasta(self, sequence_header, sequence):
39 """ 40 Converts given sequence into fasta format 41 """ 42 sequence_body = "" 43 if self.one_line_per_seq: 44 sequence_body = sequence 45 else: 46 number_of_lines = int(ceil(len(sequence)/float(self.symbol_per_line))) 47 #print number_of_lines, sequence 48 sequence_body = "\n".join([ sequence[i*self.symbol_per_line:(i+1)*self.symbol_per_line] for i in xrange(number_of_lines) ]) 49 return ">%s\n%s\n" %(sequence_header, sequence_body)
50 51
52 - def output_sequence(self, sequence_header, sequence):
53 """ 54 Outputs given sequence using out_method given in the initialization 55 """ 56 self.out_method(self._convert_sequence_to_fasta(sequence_header, sequence)) 57 return
58