0
|
1 #!/usr/bin/env python
|
|
2
|
|
3 import sys
|
|
4 from galaxy import eggs
|
|
5 import pkg_resources
|
|
6 pkg_resources.require( "bx-python" )
|
|
7 import bx.seq.twobit
|
12
|
8 import re
|
0
|
9
|
|
10 ##output columns: read_name chr prefix_start prefix_end TR_start TR_end suffix_start suffix_end TR_length TR_sequence
|
|
11
|
12
|
12 def readlengthinfer(sequence):
|
|
13 total_readlength=0
|
|
14 countlist=['M','I','S','X','=']
|
|
15 not_countlist=['D','P','N','H']
|
|
16 matchs=re.findall(r"(\d+)([A-Z=])",sequence)
|
|
17 for match in matchs:
|
|
18 if match[1] in countlist:
|
|
19 total_readlength+=int(match[0])
|
|
20 return total_readlength
|
|
21
|
0
|
22 samf = open(sys.argv[1],'r') #assumes sam file is sorted by readname
|
|
23 seq_path = sys.argv[2] #Path to the reference genome in 2bit format
|
|
24
|
|
25 ##maxTRlength=int(sys.argv[4])
|
|
26 ##maxoriginalreadlength=int(sys.argv[5])
|
|
27 maxTRlength=int(sys.argv[3])
|
|
28 maxoriginalreadlength=int(sys.argv[4])
|
|
29 outfile=sys.argv[5]
|
|
30 fout = open(outfile,'w')
|
|
31
|
|
32 twobitfile = bx.seq.twobit.TwoBitFile( file( seq_path ) )
|
|
33
|
|
34 skipped=0
|
|
35 while True:
|
|
36 read = samf.readline().strip()
|
|
37 if not(read): #EOF reached
|
|
38 break
|
|
39 if read[0] == "@":
|
|
40 #print read
|
|
41 continue
|
|
42 mate = samf.readline().strip()
|
|
43 if not(mate): #EOF reached
|
|
44 break
|
|
45 read_elems = read.split()
|
|
46 mate_elems = mate.split()
|
|
47 read_name = read_elems[0].strip()
|
|
48 mate_name = mate_elems[0].strip()
|
|
49 while True:
|
|
50 if read_name == mate_name:
|
|
51 break
|
|
52 elif read_name != mate_name:
|
|
53 #print >>sys.stderr, "Input SAM file doesn't seem to be sorted by readname. Please sort and retry."
|
|
54 #break
|
|
55 skipped += 1
|
|
56 read = mate
|
|
57 read_elems = mate_elems
|
|
58 mate = samf.readline().strip()
|
|
59 read_name = read_elems[0].strip()
|
|
60 mate_name = mate_elems[0].strip()
|
|
61 if not(mate): #EOF reached
|
|
62 break
|
|
63 mate_elems = mate.split()
|
|
64 #extract XT:A tag
|
|
65 #for e in read_elems:
|
|
66 # if e.startswith('XT:A'):
|
|
67 # read_xt = e
|
|
68 #for e in mate_elems:
|
|
69 # if e.startswith('XT:A'):
|
|
70 # mate_xt = e
|
|
71 #if 'XT:A:U' not in read_elems or 'XT:A:U' not in mate_elems: #both read and it's mate need to be mapped uniquely
|
|
72 # continue
|
|
73 read_chr = read_elems[2]
|
|
74 read_start = int(read_elems[3])
|
|
75 read_cigar = read_elems[5]
|
12
|
76 # if len(read_cigar.split('M')) != 2: #we want perfect matches only..cigar= <someInt>M
|
|
77 # continue
|
|
78 # read_len = int(read_cigar.split('M')[0])
|
|
79 read_len=readlengthinfer(read_cigar)
|
0
|
80 mate_chr = mate_elems[2]
|
|
81 mate_start = int(mate_elems[3])
|
|
82 mate_cigar = mate_elems[5]
|
12
|
83 # if len(mate_cigar.split('M')) != 2: #we want perfect matches only..cigar= <someInt>M
|
|
84 # continue
|
|
85 # mate_len = int(mate_cigar.split('M')[0])
|
|
86 mate_len=readlengthinfer(mate_cigar)
|
0
|
87 if read_chr != mate_chr: # check that they were mapped to the same chromosome
|
|
88 continue
|
|
89 if abs(read_start - mate_start) > (maxoriginalreadlength+maxTRlength):
|
|
90 continue
|
|
91 if read_start < mate_start:
|
|
92 pre_s = read_start-1
|
|
93 pre_e = read_start-1+read_len
|
|
94 tr_s = read_start-1+read_len
|
|
95 tr_e = mate_start-1
|
|
96 suf_s = mate_start-1
|
|
97 suf_e = mate_start-1+mate_len
|
|
98 else:
|
|
99 pre_s = mate_start-1
|
|
100 pre_e = mate_start-1+mate_len
|
|
101 tr_s = mate_start-1+mate_len
|
|
102 tr_e = read_start-1
|
|
103 suf_s = read_start-1
|
|
104 suf_e = read_start-1+read_len
|
|
105 tr_len = abs(tr_e - tr_s)
|
|
106 if tr_len > maxTRlength:
|
|
107 continue
|
|
108 if pre_e >= suf_s: #overlapping prefix and suffix
|
|
109 continue
|
|
110 tr_ref_seq = twobitfile[read_chr][tr_s:tr_e]
|
|
111 ##print >>fout, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s" %(read_name,read_chr,pre_s,pre_e,tr_s,tr_e,suf_s,suf_e,tr_len,tr_ref_seq)
|
|
112 fout.writelines('\t'.join(map(str,[read_name,read_chr,pre_s,pre_e,tr_s,tr_e,suf_s,suf_e,tr_len,tr_ref_seq]))+'\n')
|
|
113
|
|
114 print "Skipped %d unpaired reads" %(skipped)
|