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
6 print("usercase3 shisfts left all letters in specified vertical block of input alignment")
7 print("\n Use python usercase3.py -h for help")
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
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")
32 print("File %s was not found" % in_file)
39 print("Values -f %s and -t %s must be interer!" % (_from,to))
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!")
51 print("from must be greater than to!")
57 conservative.append((0,int(_from)))
58 if ito < alignment.length:
59 conservative.append((ito,alignment.length-1))
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.
71 markup = ["-"] * len(alignment.columns)
72 for begin, end in ranges:
73 for i in range(begin, end+1):
75 return "".join(markup)
77 def markup_to_blocks(markup):
78 """Convert markup line to a bunch of blocks, one for each sequential run."""
81 for mark, column in zip(markup, alignment.columns):
83 block = protein.Block.from_alignment(alignment, columns=[])
84 blocks[mark] = blocks.get(mark, []) + [block]
86 blocks[mark][-1].columns.append(column)
90 markup = ranges_to_markup(conservative)
91 blocks = markup_to_blocks(markup)
92 for block in blocks["-"]:
94 alignment.to_fasta(open(out_file, "w"))