annotate tools/metag_tools/mapping_to_ucsc.py @ 0:9071e359b9a3

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