annotate blat_mapping.py @ 0:807e3e50845a draft default tip

Imported from capsule None
author devteam
date Mon, 19 May 2014 12:33:35 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/env python
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
2
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
3 import os, sys
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
4
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
5 assert sys.version_info[:2] >= ( 2, 4 )
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
6
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
7 def reverse_complement(s):
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
8 complement_dna = {"A":"T", "T":"A", "C":"G", "G":"C", "a":"t", "t":"a", "c":"g", "g":"c", "N":"N", "n":"n" , ".":"."}
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
9 reversed_s = []
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
10 for i in s:
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
11 reversed_s.append(complement_dna[i])
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
12 reversed_s.reverse()
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
13 return "".join(reversed_s)
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
14
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
15 def __main__():
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
16 nuc_index = {'a':0,'t':1,'c':2,'g':3,'n':4}
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
17 coverage = {} # key = (chrom, index)
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
18 invalid_lines = 0
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
19 invalid_chrom = 0
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
20 infile = sys.argv[1]
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
21 outfile = sys.argv[2]
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
22
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
23 for i, line in enumerate( open( infile ) ):
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
24 line = line.rstrip('\r\n')
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
25 if not line or line.startswith('#'):
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
26 continue
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
27 fields = line.split()
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
28 if len(fields) < 21: # standard number of pslx columns
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
29 invalid_lines += 1
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
30 continue
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
31 if not fields[0].isdigit():
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
32 invalid_lines += 1
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
33 continue
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
34 chrom = fields[13]
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
35 if not chrom.startswith( 'chr' ):
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
36 invalid_lines += 1
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
37 invalid_chrom += 1
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
38 continue
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
39 try:
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
40 block_count = int(fields[17])
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
41 except:
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
42 invalid_lines += 1
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
43 continue
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
44 block_size = fields[18].split(',')
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
45 chrom_start = fields[20].split(',')
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
46
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
47 for j in range( block_count ):
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
48 try:
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
49 this_block_size = int(block_size[j])
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
50 this_chrom_start = int(chrom_start[j])
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
51 except:
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
52 invalid_lines += 1
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
53 break
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
54 # brut force coverage
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
55 for k in range( this_block_size ):
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
56 cur_index = this_chrom_start + k
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
57 if coverage.has_key( ( chrom, cur_index ) ):
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
58 coverage[(chrom, cur_index)] += 1
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
59 else:
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
60 coverage[(chrom, cur_index)] = 1
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
61
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
62 # generate a index file
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
63 outputfh = open(outfile, 'w')
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
64 keys = coverage.keys()
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
65 keys.sort()
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
66 previous_chrom = ''
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
67 for i in keys:
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
68 (chrom, location) = i
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
69 sum = coverage[(i)]
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
70 if chrom != previous_chrom:
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
71 outputfh.write( 'variableStep chrom=%s\n' % ( chrom ) )
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
72 previous_chrom = chrom
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
73 outputfh.write( "%s\t%s\n" % ( location, sum ) )
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
74 outputfh.close()
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
75
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
76 if invalid_lines:
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
77 invalid_msg = "Skipped %d invalid lines" % invalid_lines
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
78 if invalid_chrom:
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
79 invalid_msg += ", including %d lines with chrom id errors which must begin with 'chr' to map correctly to the UCSC Genome Browser. "
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
80
807e3e50845a Imported from capsule None
devteam
parents:
diff changeset
81 if __name__ == '__main__': __main__()