annotate lib/block.py @ 11:693943fed7b0
Sandbox: another failed attempt at using python+tk+text for rendering.
 | author | Danya Alexeyevsky <dendik@kodomo.fbb.msu.ru> | 
 | date | Thu, 10 Jun 2010 15:03:09 +0400 | 
 | parents | 757f2a1f8732 | 
 | children | 217d83a617c3 | 
 | rev | line source | 
| BurkovBA@0 | 1 #!usr/bin/python | 
| BurkovBA@0 | 2 | 
| BurkovBA@0 | 3 import sys | 
| BurkovBA@0 | 4 | 
| BurkovBA@0 | 5 import project | 
| BurkovBA@0 | 6 import sequence | 
| BurkovBA@0 | 7 import monomer | 
| BurkovBA@0 | 8 | 
| BurkovBA@0 | 9 class Block(object): | 
| BurkovBA@0 | 10     """ | 
| BurkovBA@0 | 11     Mandatory data: | 
| BurkovBA@0 | 12     *   self.project -- project object, which the block belongs to | 
| BurkovBA@1 | 13     *   self.sequences - set of sequence objects that contain monomers | 
| BurkovBA@0 | 14         and/or gaps, that constitute the block | 
| BurkovBA@1 | 15     *   self.positions -- list of positions of the project.alignment that | 
| BurkovBA@1 | 16         are included in the block | 
| BurkovBA@0 | 17 | 
| BurkovBA@0 | 18     How to create a new block: | 
| BurkovBA@0 | 19     >>> import project | 
| BurkovBA@0 | 20     >>> import block | 
| BurkovBA@0 | 21     >>> proj = project.Project(open("test.fasta")) | 
| BurkovBA@0 | 22     >>> block1 = block.Block(proj, proj.sequences, range(len(proj.alignment[proj.sequences[0]]))) | 
| BurkovBA@0 | 23 | 
| BurkovBA@0 | 24     """ | 
| BurkovBA@0 | 25 | 
| BurkovBA@0 | 26     def __init__(self,project,sequences,positions): | 
| BurkovBA@0 | 27         self.project=project | 
| BurkovBA@0 | 28         self.sequences=sequences | 
| BurkovBA@0 | 29         self.positions=positions | 
| BurkovBA@0 | 30 | 
| BurkovBA@0 | 31     def to_fasta(self,file): | 
| BurkovBA@0 | 32         """writes the block as an alignment in fasta-format into the file. | 
| BurkovBA@0 | 33 | 
| BurkovBA@0 | 34         No changes in the names, descriptions or order of the sequences | 
| BurkovBA@0 | 35         are made. | 
| BurkovBA@0 | 36 | 
| BurkovBA@0 | 37         """ | 
| BurkovBA@0 | 38         for sequence in self.sequences: | 
| BurkovBA@0 | 39             file.write(">%s %s\n"%(sequence.name,sequence.description)) | 
| BurkovBA@0 | 40             string_index=0 | 
| BurkovBA@0 | 41             for position in self.positions: | 
| BurkovBA@0 | 42                 if string_index>=60: | 
| BurkovBA@0 | 43                     file.write("\n") | 
| BurkovBA@0 | 44                     string_index=0 | 
| BurkovBA@0 | 45                 if self.project.alignment[sequence][position]==None: | 
| BurkovBA@0 | 46                     file.write("-") | 
| BurkovBA@0 | 47                     string_index+=1 | 
| BurkovBA@0 | 48                 else: | 
| BurkovBA@0 | 49                     file.write(self.project.alignment[sequence][position].code) | 
| BurkovBA@0 | 50                     string_index+=1 | 
| BurkovBA@0 | 51             file.write("\n") | 
| BurkovBA@0 | 52 |