annotate mapping_to_ucsc.py @ 0:cf8d47e49c21 draft

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