Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/allpy/file/679494ad2f4e/test/usecase3.py
Дата изменения: Unknown
Дата индексирования: Mon Feb 4 04:27:22 2013
Кодировка:
allpy: 679494ad2f4e test/usecase3.py

allpy

view test/usecase3.py @ 336:679494ad2f4e

MSF output format by request implemented
author Andrei <aba@belozersky.msu.ru>
date Wed, 12 Jan 2011 20:23:22 +0300
parents 38888b46c342
children 20944c113ea3
line source
1 from allpy import protein
2 import sys
3 import optparse
5 if len(sys.argv)==1:
6 print("usercase3 shisfts left all letters in specified vertical block of input alignment")
7 print("\n Use python usercase3.py -h for help")
8 exit()
10 parser = optparse.OptionParser()
12 parser.add_option("-i", "--in-file", help="Input alignment in fasta format", dest="in_file")
13 parser.add_option("-o", "--out-file", help="Output refined alignment in fasta format", dest="out_file", default='result.fasta')
14 parser.add_option("-f", "--_from", help="First position of refining block", dest="_from")
15 parser.add_option("-t", "--to", help="Last position of refining block", dest="to")
17 options, args = parser.parse_args()
18 in_file = options.in_file
19 out_file = options.out_file
20 _from = options._from
21 to = options.to
24 if in_file==None or _from==None or to==None:
25 print("Missed command line parameters.")
26 print("Use python usercase3.py -h for help")
27 exit()
29 try:
30 f = open(in_file)
31 except:
32 print("File %s was not found" % in_file)
33 exit()
35 try:
36 ifrom = int(_from)
37 ito = int(to)
38 except:
39 print("Values -f %s and -t %s must be interer!" % (_from,to))
40 exit()
42 alignment = protein.Alignment.from_fasta(open(in_file))
44 alignment.length = len(alignment.columns)
46 if ito >= alignment.length:
47 print("to must be smaller than alignment length!")
48 exit()
50 if ifrom >= ito :
51 print("from must be greater than to!")
52 exit()
55 conservative = []
56 if ifrom > 1:
57 conservative.append((0,int(_from)))
58 if ito < alignment.length:
59 conservative.append((ito,alignment.length-1))
61 print conservative
63 #conservative = [(10,20), (40,50)]
64 #conservative = [(0,6),(18,37)]
66 def ranges_to_markup(ranges):
67 """Convert list of ranges to line of markup.
69 This has nothing to do with allpy.
70 """
71 markup = ["-"] * len(alignment.columns)
72 for begin, end in ranges:
73 for i in range(begin, end+1):
74 markup[i] = "+"
75 return "".join(markup)
77 def markup_to_blocks(markup):
78 """Convert markup line to a bunch of blocks, one for each sequential run."""
79 current = None
80 blocks = {}
81 for mark, column in zip(markup, alignment.columns):
82 if mark != current:
83 block = protein.Block.from_alignment(alignment, columns=[])
84 blocks[mark] = blocks.get(mark, []) + [block]
85 current = mark
86 blocks[mark][-1].columns.append(column)
87 return blocks
89 def main():
90 markup = ranges_to_markup(conservative)
91 blocks = markup_to_blocks(markup)
92 for block in blocks["-"]:
93 block.flush_left()
94 alignment.to_fasta(open(out_file, "w"))
96 main()