annotate mapping_to_ucsc.py @ 1:5385aceef9e9 draft default tip

planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/mapping_to_ucsc commit a1517c9d22029095120643bbe2c8fa53754dd2b7
author devteam
date Wed, 11 Nov 2015 12:20:00 -0500
parents 601abbd22cea
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/env python
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
2
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
3 import sys, tempfile, os
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
4
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
5 assert sys.version_info[:2] >= (2.4)
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
6
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
7 def stop_err(msg):
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
8 sys.stderr.write(msg)
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
9 sys.exit()
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
10
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
11 def main():
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
12
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
13 out_fname = sys.argv[1]
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
14 in_fname = sys.argv[2]
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
15 chr_col = int(sys.argv[3])-1
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
16 coord_col = int(sys.argv[4])-1
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
17 track_type = sys.argv[5]
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
18 if track_type == 'coverage' or track_type == 'both':
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
19 coverage_col = int(sys.argv[6])-1
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
20 cname = sys.argv[7]
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
21 cdescription = sys.argv[8]
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
22 ccolor = sys.argv[9].replace('-',',')
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
23 cvisibility = sys.argv[10]
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
24 if track_type == 'snp' or track_type == 'both':
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
25 if track_type == 'both':
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
26 j = 5
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
27 else:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
28 j = 0
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
29 #sname = sys.argv[7+j]
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
30 sdescription = sys.argv[6+j]
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
31 svisibility = sys.argv[7+j]
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
32 #ref_col = int(sys.argv[10+j])-1
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
33 read_col = int(sys.argv[8+j])-1
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
34
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
35
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
36 # Sort the input file based on chromosome (alphabetically) and start co-ordinates (numerically)
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
37 sorted_infile = tempfile.NamedTemporaryFile()
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
38 try:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
39 os.system("sort -k %d,%d -k %dn -o %s %s" %(chr_col+1,chr_col+1,coord_col+1,sorted_infile.name,in_fname))
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
40 except Exception, exc:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
41 stop_err( 'Initialization error -> %s' %str(exc) )
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
42
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
43 #generate chr list
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
44 sorted_infile.seek(0)
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
45 chr_vals = []
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
46 for line in file( sorted_infile.name ):
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
47 line = line.strip()
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
48 if not(line):
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
49 continue
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
50 try:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
51 fields = line.split('\t')
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
52 chr = fields[chr_col]
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
53 if chr not in chr_vals:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
54 chr_vals.append(chr)
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
55 except:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
56 pass
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
57 if not(chr_vals):
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
58 stop_err("Skipped all lines as invalid.")
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
59
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
60 if track_type == 'coverage' or track_type == 'both':
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
61 if track_type == 'coverage':
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
62 fout = open( out_fname, "w" )
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
63 else:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
64 fout = tempfile.NamedTemporaryFile()
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
65 fout.write('''track type=wiggle_0 name="%s" description="%s" color=%s visibility=%s\n''' \
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
66 % ( cname, cdescription, ccolor, cvisibility ))
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
67 if track_type == 'snp' or track_type == 'both':
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
68 fout_a = tempfile.NamedTemporaryFile()
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
69 fout_t = tempfile.NamedTemporaryFile()
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
70 fout_g = tempfile.NamedTemporaryFile()
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
71 fout_c = tempfile.NamedTemporaryFile()
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
72 fout_ref = tempfile.NamedTemporaryFile()
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
73
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
74 fout_a.write('''track type=wiggle_0 name="%s" description="%s" color=%s visibility=%s\n''' \
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
75 % ( "Track A", sdescription, '255,0,0', svisibility ))
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
76 fout_t.write('''track type=wiggle_0 name="%s" description="%s" color=%s visibility=%s\n''' \
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
77 % ( "Track T", sdescription, '0,255,0', svisibility ))
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
78 fout_g.write('''track type=wiggle_0 name="%s" description="%s" color=%s visibility=%s\n''' \
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
79 % ( "Track G", sdescription, '0,0,255', svisibility ))
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
80 fout_c.write('''track type=wiggle_0 name="%s" description="%s" color=%s visibility=%s\n''' \
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
81 % ( "Track C", sdescription, '255,0,255', svisibility ))
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
82
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
83
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
84 sorted_infile.seek(0)
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
85 for line in file( sorted_infile.name ):
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
86 line = line.strip()
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
87 if not(line):
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
88 continue
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
89 try:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
90 fields = line.split('\t')
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
91 chr = fields[chr_col]
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
92 start = int(fields[coord_col])
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
93 assert start > 0
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
94 except:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
95 continue
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
96 try:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
97 ind = chr_vals.index(chr) #encountered chr for the 1st time
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
98 del chr_vals[ind]
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
99 prev_start = ''
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
100 header = "variableStep chrom=%s\n" %(chr)
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
101 if track_type == 'coverage' or track_type == 'both':
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
102 coverage = int(fields[coverage_col])
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
103 line1 = "%s\t%s\n" %(start,coverage)
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
104 fout.write("%s%s" %(header,line1))
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
105 if track_type == 'snp' or track_type == 'both':
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
106 a = t = g = c = 0
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
107 fout_a.write("%s" %(header))
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
108 fout_t.write("%s" %(header))
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
109 fout_g.write("%s" %(header))
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
110 fout_c.write("%s" %(header))
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
111 try:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
112 #ref_nt = fields[ref_col].capitalize()
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
113 read_nt = fields[read_col].capitalize()
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
114 try:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
115 nt_ind = ['A','T','G','C'].index(read_nt)
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
116 if nt_ind == 0:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
117 a+=1
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
118 elif nt_ind == 1:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
119 t+=1
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
120 elif nt_ind == 2:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
121 g+=1
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
122 else:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
123 c+=1
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
124 except ValueError:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
125 pass
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
126 except:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
127 pass
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
128 prev_start = start
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
129 except ValueError:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
130 if start != prev_start:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
131 if track_type == 'coverage' or track_type == 'both':
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
132 coverage = int(fields[coverage_col])
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
133 fout.write("%s\t%s\n" %(start,coverage))
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
134 if track_type == 'snp' or track_type == 'both':
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
135 if a:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
136 fout_a.write("%s\t%s\n" %(prev_start,a))
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
137 if t:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
138 fout_t.write("%s\t%s\n" %(prev_start,t))
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
139 if g:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
140 fout_g.write("%s\t%s\n" %(prev_start,g))
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
141 if c:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
142 fout_c.write("%s\t%s\n" %(prev_start,c))
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
143 a = t = g = c = 0
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
144 try:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
145 #ref_nt = fields[ref_col].capitalize()
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
146 read_nt = fields[read_col].capitalize()
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
147 try:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
148 nt_ind = ['A','T','G','C'].index(read_nt)
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
149 if nt_ind == 0:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
150 a+=1
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
151 elif nt_ind == 1:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
152 t+=1
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
153 elif nt_ind == 2:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
154 g+=1
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
155 else:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
156 c+=1
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
157 except ValueError:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
158 pass
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
159 except:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
160 pass
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
161 prev_start = start
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
162 else:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
163 if track_type == 'snp' or track_type == 'both':
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
164 try:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
165 #ref_nt = fields[ref_col].capitalize()
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
166 read_nt = fields[read_col].capitalize()
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
167 try:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
168 nt_ind = ['A','T','G','C'].index(read_nt)
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
169 if nt_ind == 0:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
170 a+=1
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
171 elif nt_ind == 1:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
172 t+=1
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
173 elif nt_ind == 2:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
174 g+=1
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
175 else:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
176 c+=1
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
177 except ValueError:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
178 pass
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
179 except:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
180 pass
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
181
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
182 if track_type == 'snp' or track_type == 'both':
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
183 if a:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
184 fout_a.write("%s\t%s\n" %(prev_start,a))
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
185 if t:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
186 fout_t.write("%s\t%s\n" %(prev_start,t))
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
187 if g:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
188 fout_g.write("%s\t%s\n" %(prev_start,g))
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
189 if c:
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
190 fout_c.write("%s\t%s\n" %(prev_start,c))
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
191
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
192 fout_a.seek(0)
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
193 fout_g.seek(0)
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
194 fout_t.seek(0)
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
195 fout_c.seek(0)
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
196
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
197 if track_type == 'snp':
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
198 os.system("cat %s %s %s %s >> %s" %(fout_a.name,fout_t.name,fout_g.name,fout_c.name,out_fname))
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
199 elif track_type == 'both':
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
200 fout.seek(0)
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
201 os.system("cat %s %s %s %s %s | cat > %s" %(fout.name,fout_a.name,fout_t.name,fout_g.name,fout_c.name,out_fname))
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
202 if __name__ == "__main__":
601abbd22cea Imported from capsule None
devteam
parents:
diff changeset
203 main()