annotate test-data/PEsortedSAM2readprofile.py @ 5:b27006b0a953

update to latest version
author devteam@galaxyproject.org
date Wed, 22 Apr 2015 12:19:28 -0400
parents ecfc9041bcc5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
1 #!/usr/bin/env python
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
2
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
3 import sys
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
4 from galaxy import eggs
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
5 import pkg_resources
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
6 pkg_resources.require( "bx-python" )
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
7 import bx.seq.twobit
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
8
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
9 ##output columns: read_name chr prefix_start prefix_end TR_start TR_end suffix_start suffix_end TR_length TR_sequence
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
10
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
11 samf = open(sys.argv[1],'r') #assumes sam file is sorted by readname
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
12 seq_path = sys.argv[2] #Path to the reference genome in 2bit format
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
13
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
14 ##maxTRlength=int(sys.argv[4])
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
15 ##maxoriginalreadlength=int(sys.argv[5])
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
16 maxTRlength=int(sys.argv[3])
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
17 maxoriginalreadlength=int(sys.argv[4])
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
18 outfile=sys.argv[5]
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
19 fout = open(outfile,'w')
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
20
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
21 twobitfile = bx.seq.twobit.TwoBitFile( file( seq_path ) )
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
22
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
23 skipped=0
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
24 while True:
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
25 read = samf.readline().strip()
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
26 if not(read): #EOF reached
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
27 break
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
28 if read[0] == "@":
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
29 #print read
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
30 continue
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
31 mate = samf.readline().strip()
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
32 if not(mate): #EOF reached
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
33 break
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
34 read_elems = read.split()
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
35 mate_elems = mate.split()
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
36 read_name = read_elems[0].strip()
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
37 mate_name = mate_elems[0].strip()
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
38 while True:
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
39 if read_name == mate_name:
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
40 break
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
41 elif read_name != mate_name:
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
42 #print >>sys.stderr, "Input SAM file doesn't seem to be sorted by readname. Please sort and retry."
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
43 #break
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
44 skipped += 1
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
45 read = mate
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
46 read_elems = mate_elems
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
47 mate = samf.readline().strip()
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
48 read_name = read_elems[0].strip()
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
49 mate_name = mate_elems[0].strip()
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
50 if not(mate): #EOF reached
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
51 break
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
52 mate_elems = mate.split()
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
53 #extract XT:A tag
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
54 #for e in read_elems:
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
55 # if e.startswith('XT:A'):
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
56 # read_xt = e
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
57 #for e in mate_elems:
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
58 # if e.startswith('XT:A'):
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
59 # mate_xt = e
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
60 #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
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
61 # continue
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
62 read_chr = read_elems[2]
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
63 read_start = int(read_elems[3])
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
64 read_cigar = read_elems[5]
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
65 if len(read_cigar.split('M')) != 2: #we want perfect matches only..cigar= <someInt>M
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
66 continue
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
67 read_len = int(read_cigar.split('M')[0])
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
68 mate_chr = mate_elems[2]
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
69 mate_start = int(mate_elems[3])
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
70 mate_cigar = mate_elems[5]
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
71 if len(mate_cigar.split('M')) != 2: #we want perfect matches only..cigar= <someInt>M
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
72 continue
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
73 mate_len = int(mate_cigar.split('M')[0])
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
74 if read_chr != mate_chr: # check that they were mapped to the same chromosome
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
75 continue
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
76 if abs(read_start - mate_start) > (maxoriginalreadlength+maxTRlength):
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
77 continue
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
78 if read_start < mate_start:
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
79 pre_s = read_start-1
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
80 pre_e = read_start-1+read_len
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
81 tr_s = read_start-1+read_len
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
82 tr_e = mate_start-1
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
83 suf_s = mate_start-1
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
84 suf_e = mate_start-1+mate_len
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
85 else:
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
86 pre_s = mate_start-1
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
87 pre_e = mate_start-1+mate_len
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
88 tr_s = mate_start-1+mate_len
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
89 tr_e = read_start-1
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
90 suf_s = read_start-1
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
91 suf_e = read_start-1+read_len
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
92 tr_len = abs(tr_e - tr_s)
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
93 if tr_len > maxTRlength:
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
94 continue
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
95 if pre_e >= suf_s: #overlapping prefix and suffix
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
96 continue
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
97 tr_ref_seq = twobitfile[read_chr][tr_s:tr_e]
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
98 ##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)
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
99 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')
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
100
ecfc9041bcc5 Deleted selected files
arkarachai-fungtammasan
parents:
diff changeset
101 print "Skipped %d unpaired reads" %(skipped)