annotate substitutions.py @ 0:190735ce4c2b draft

Imported from capsule None
author devteam
date Tue, 01 Apr 2014 09:12:31 -0400
parents
children 385a5c3d7244
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/env python
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
2 #Guruprasad ANanda
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
3 """
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
4 Fetches substitutions from pairwise alignments.
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
5 """
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
6
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
7 from galaxy import eggs
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
8
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
9 from galaxy.tools.util import maf_utilities
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
10
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
11 import bx.align.maf
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
12 import sys
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
13
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
14 def stop_err(msg):
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
15 sys.stderr.write(msg)
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
16 sys.exit()
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
17
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
18
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
19 if len(sys.argv) < 3:
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
20 stop_err("Incorrect number of arguments.")
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
21
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
22 inp_file = sys.argv[1]
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
23 out_file = sys.argv[2]
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
24 fout = open(out_file, 'w')
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
25
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
26 def fetchSubs(block):
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
27 src1 = block.components[0].src
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
28 sequence1 = block.components[0].text
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
29 start1 = block.components[0].start
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
30 end1 = block.components[0].end
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
31 len1_withgap = len(sequence1)
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
32
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
33 for seq in range (1, len(block.components)):
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
34 src2 = block.components[seq].src
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
35 sequence2 = block.components[seq].text
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
36 start2 = block.components[seq].start
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
37 end2 = block.components[seq].end
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
38 sub_begin = None
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
39 sub_end = None
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
40 begin = False
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
41
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
42 for nt in range(len1_withgap):
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
43 if sequence1[nt] not in '-#$^*?' and sequence2[nt] not in '-#$^*?': # Not a gap or masked character
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
44 if sequence1[nt].upper() != sequence2[nt].upper():
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
45 if not(begin):
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
46 sub_begin = nt
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
47 begin = True
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
48 sub_end = nt
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
49 else:
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
50 if begin:
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
51 print >> fout, "%s\t%s\t%s" % ( src1, start1+sub_begin-sequence1[0:sub_begin].count('-'), start1+sub_end-sequence1[0:sub_end].count('-') )
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
52 print >> fout, "%s\t%s\t%s" % ( src2, start2+sub_begin-sequence2[0:sub_begin].count('-'), start2+sub_end-sequence2[0:sub_end].count('-') )
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
53 begin = False
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
54 else:
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
55 if begin:
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
56 print >> fout, "%s\t%s\t%s" % ( src1, start1+sub_begin-sequence1[0:sub_begin].count('-'), end1+sub_end-sequence1[0:sub_end].count('-') )
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
57 print >> fout, "%s\t%s\t%s" % ( src2, start2+sub_begin-sequence2[0:sub_begin].count('-'), end2+sub_end-sequence2[0:sub_end].count('-') )
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
58 begin = False
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
59
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
60
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
61 def main():
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
62 skipped = 0
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
63 not_pairwise = 0
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
64 try:
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
65 maf_reader = bx.align.maf.Reader( open(inp_file, 'r') )
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
66 except:
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
67 stop_err("Your MAF file appears to be malformed.")
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
68 print >> fout, "#Chr\tStart\tEnd"
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
69 for block in maf_reader:
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
70 if len(block.components) != 2:
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
71 not_pairwise += 1
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
72 continue
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
73 try:
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
74 fetchSubs(block)
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
75 except:
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
76 skipped += 1
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
77
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
78 if not_pairwise:
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
79 print "Skipped %d non-pairwise blocks" % (not_pairwise)
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
80 if skipped:
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
81 print "Skipped %d blocks" % (skipped)
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
82
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
83
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
84 if __name__ == "__main__":
190735ce4c2b Imported from capsule None
devteam
parents:
diff changeset
85 main()