Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/allpy/diff/b7b963287d31/lib/block.py
Дата изменения: Unknown
Дата индексирования: Sat Mar 1 10:21:35 2014
Кодировка:
allpy: lib/block.py diff

allpy

diff lib/block.py @ 112:b7b963287d31

broken: lib::block::to_fasta -> save_fasta
author boris <bnagaev@gmail.com>
date Sat, 23 Oct 2010 13:16:12 +0400
parents 217d83a617c3
children 90e2c77b69b7
line diff
     1.1 --- a/lib/block.py	Sat Oct 23 12:57:07 2010 +0400
     1.2 +++ b/lib/block.py	Sat Oct 23 13:16:12 2010 +0400
     1.3 @@ -23,32 +23,35 @@
     1.4  
     1.5      """
     1.6      
     1.7 -    def __init__(self,project,sequences,positions):
     1.8 +    def __init__(self, project, sequences=None, positions=None):
     1.9 +        """
    1.10 +        Builds new block from project
    1.11 +        if sequences==None, all sequences are used
    1.12 +        if positions==None, all positions are used
    1.13 +        """
    1.14 +        if sequences == None:
    1.15 +            sequences = project.sequences
    1.16 +        if positions == None:
    1.17 +            positions = range(len(project))
    1.18          self.project = project
    1.19          self.sequences = sequences
    1.20          self.positions = positions
    1.21  
    1.22 -    def to_fasta(self,file):
    1.23 -        """writes the block as an alignment in fasta-format into the file.
    1.24 -
    1.25 +    def save_fasta(self, out_file, long_line=60):
    1.26 +        """
    1.27 +        Saves alignment to given file in fasta-format 
    1.28 +        Splits long lines to substrings of length=long_line
    1.29 +        To prevent this, set long_line=None 
    1.30 +        
    1.31          No changes in the names, descriptions or order of the sequences
    1.32          are made.
    1.33 -
    1.34          """
    1.35          for sequence in self.sequences:
    1.36 -            file.write(">%s %s\n"%(sequence.name,sequence.description))
    1.37 -            string_index=0
    1.38 -            for position in self.positions:
    1.39 -                if string_index>=60:
    1.40 -                    file.write("\n")
    1.41 -                    string_index=0
    1.42 -                if self.project.alignment[sequence][position]==None:
    1.43 -                    file.write("-")
    1.44 -                    string_index+=1
    1.45 -                else:
    1.46 -                    file.write(self.project.alignment[sequence][position].code)
    1.47 -                    string_index+=1
    1.48 -            file.write("\n")
    1.49 -
    1.50 -
    1.51 -
    1.52 +            out_file.write(">%(name)s %(description)s \n" % sequence.__dict__)
    1.53 +            monomers = [self.project.alignment[i] for i in sorted(self.positions)]
    1.54 +            string = ''.join([m.type.code1 if m else '-' for m in monomers])
    1.55 +            if long_line:
    1.56 +                for i in range(0, len(string) // long_line + 1):
    1.57 +                    out_file.write("%s \n" % string[i*long_line : i*long_line + long_line])
    1.58 +            else:
    1.59 +                out_file.write("%s \n" % string)