annotate blat_coverage_report.py @ 0:30f0948c649c draft default tip

Imported from capsule None
author devteam
date Mon, 19 May 2014 12:34:01 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/env python
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
2
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
3 import os, sys
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
4
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
5 assert sys.version_info[:2] >= ( 2, 4 )
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
6
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
7 def stop_err( msg ):
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
8 sys.stderr.write( "%s\n" % msg )
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
9 sys.exit()
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
10
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
11 def reverse_complement(s):
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
12 complement_dna = {"A":"T", "T":"A", "C":"G", "G":"C", "a":"t", "t":"a", "c":"g", "g":"c", "N":"N", "n":"n" , ".":"."}
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
13 reversed_s = []
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
14 for i in s:
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
15 reversed_s.append(complement_dna[i])
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
16 reversed_s.reverse()
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
17 return "".join(reversed_s)
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
18
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
19 def __main__():
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
20 nuc_index = {'a':0,'t':1,'c':2,'g':3}
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
21 diff_hash = {} # key = (chrom, index)
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
22 infile = sys.argv[1]
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
23 outfile = sys.argv[2]
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
24 invalid_lines = 0
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
25 invalid_chars = 0
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
26 data_id = ''
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
27 data_seq = ''
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
28
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
29 for i, line in enumerate( open( infile ) ):
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
30 line = line.rstrip( '\r\n' )
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
31 if not line or line.startswith( '#' ):
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
32 continue
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
33 fields = line.split()
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
34 if len(fields) != 23: # standard number of pslx columns
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
35 invalid_lines += 1
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
36 continue
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
37 if not fields[0].isdigit():
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
38 invalid_lines += 1
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
39 continue
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
40 read_id = fields[9]
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
41 chrom = fields[13]
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
42 try:
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
43 block_count = int(fields[17])
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
44 except:
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
45 invalid_lines += 1
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
46 continue
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
47 block_size = fields[18].split(',')
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
48 read_start = fields[19].split(',')
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
49 chrom_start = fields[20].split(',')
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
50 read_seq = fields[21].split(',')
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
51 chrom_seq = fields[22].split(',')
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
52
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
53 for j in range(block_count):
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
54 try:
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
55 this_block_size = int(block_size[j])
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
56 this_read_start = int(read_start[j])
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
57 this_chrom_start = int(chrom_start[j])
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
58 except:
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
59 invalid_lines += 1
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
60 break
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
61 this_read_seq = read_seq[j]
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
62 this_chrom_seq = chrom_seq[j]
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
63
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
64 if not this_read_seq.isalpha():
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
65 continue
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
66 if not this_chrom_seq.isalpha():
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
67 continue
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
68
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
69 # brut force to check coverage
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
70 for k in range(this_block_size):
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
71 cur_index = this_chrom_start+k
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
72 sub_a = this_read_seq[k:(k+1)].lower()
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
73 sub_b = this_chrom_seq[k:(k+1)].lower()
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
74 if not diff_hash.has_key((chrom, cur_index)):
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
75 try:
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
76 diff_hash[(chrom, cur_index)] = [0,0,0,0,sub_b.upper()] # a, t, c, g, ref. nuc.
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
77 except Exception, e:
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
78 stop_err( str( e ) )
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
79 if sub_a in ['a','t','c','g']:
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
80 diff_hash[(chrom, cur_index)][nuc_index[(sub_a)]] += 1
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
81 else:
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
82 invalid_chars += 1
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
83
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
84 outputfh = open(outfile, 'w')
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
85 outputfh.write( "##title\tlocation\tref.\tcov.\tA\tT\tC\tG\n" )
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
86 keys = diff_hash.keys()
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
87 keys.sort()
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
88 for i in keys:
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
89 (chrom, location) = i
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
90 sum = diff_hash[ (i) ][ 0 ] + diff_hash[ ( i ) ][ 1 ] + diff_hash[ ( i ) ][ 2 ] + diff_hash[ ( i ) ][ 3 ] # did not include N's
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
91 if sum == 0:
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
92 continue
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
93 ratio_A = diff_hash[ ( i ) ][ 0 ] * 100.0 / sum
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
94 ratio_T = diff_hash[ ( i ) ][ 1 ] * 100.0 / sum
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
95 ratio_C = diff_hash[ ( i ) ][ 2 ] * 100.0 / sum
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
96 ratio_G = diff_hash[ ( i ) ][ 3 ] * 100.0 / sum
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
97 (title_head, title_tail) = os.path.split(chrom)
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
98 result = "%s\t%s\t%s\t%d\tA(%0.0f)\tT(%0.0f)\tC(%0.0f)\tG(%0.0f)\n" % ( title_tail, location, diff_hash[(i)][4], sum, ratio_A, ratio_T, ratio_C, ratio_G )
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
99 outputfh.write(result)
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
100 outputfh.close()
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
101
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
102 if invalid_lines:
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
103 print 'Skipped %d invalid lines. ' % ( invalid_lines )
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
104 if invalid_chars:
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
105 print 'Skipped %d invalid characters in the alignment. ' % (invalid_chars)
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
106
30f0948c649c Imported from capsule None
devteam
parents:
diff changeset
107 if __name__ == '__main__': __main__()