annotate pyPRADA_1.2/prada-guess-ft @ 2:74f6c4fcab2f draft

Uploaded
author siyuan
date Tue, 25 Feb 2014 11:07:15 -0500
parents acc2ca1a3ba4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
1 #!/usr/bin/env python
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
2
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
3 #GUESS-ft:General UsEr defined Supervised Search for fusion transcript.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
4 #This program is to perform targeted search of gene fusions from RNAseq data.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
5 #It uses samtools, pysam package. It requires a bam file and other reference files.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
6 #Note that it is a less accurate tool than prada-fusion.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
7
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
8 import pysam
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
9 import subprocess
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
10 import os
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
11 import time
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
12 import sys
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
13 import bioclass
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
14 import ioprada
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
15
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
16 args=sys.argv
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
17
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
18 #Get all parameters
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
19 help_menu='''\nUsage: prada-guess-ft GeneA GeneB -conf xx.txt -inputbam X -mm 1 -minmapq 30 -junL X -outdir X -unmap X\n
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
20 **Parameters**:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
21 -conf the configure file. see prada-fusion -conf for details
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
22 -inputbam the input bam file
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
23 -mm number of mismatch allowed
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
24 -minmapq mininum mapping quality for reads to be used in fusion finding
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
25 -junL length of exons to be used for junctions. see prada-fusion
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
26 -outdir output directory
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
27 -unmap the numapped reads. useful if user need to run guess-ft multiple times
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
28 '''
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
29
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
30 if '-h' in args or '-help' in args or len(args)==1:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
31 print help_menu
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
32 sys.exit(0)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
33
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
34 ##################################################################################
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
35 Gene_A=args[1]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
36 Gene_B=args[2]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
37 if '-inputbam' not in args:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
38 sys.exit('Input BAM needed')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
39 else:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
40 i=args.index('-inputbam')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
41 sourcefile=args[i+1]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
42 if '-outdir' not in args:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
43 outdir='./'
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
44 else:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
45 i=args.index('-outdir')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
46 outdir=args[i+1]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
47 if not os.path.exists(outdir):
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
48 os.mkdir(outdir)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
49 if '-unmap' not in args: #get unmapped.bam yourself
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
50 unmapbam='%s/one.end.unmapped.bam'%outdir
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
51 extract_mask=1
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
52 else:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
53 i=args.index('-unmap')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
54 unmapbam=args[i+1]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
55 extract_mask=0
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
56 if '-mm' not in args:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
57 mm=1
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
58 else:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
59 i=args.index('-mm')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
60 mm=int(args[i+1])
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
61 #minimum mapping quality for reads as fusion evidences
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
62 if '-minmapq' not in args:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
63 minmapq=30
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
64 else:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
65 i=args.index('-minmapq')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
66 minmapq=int(args[i+1])
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
67
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
68 if '-junL' not in args:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
69 sys.exit('-junL needed')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
70 else:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
71 i=args.index('-junL')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
72 junL=int(args[i+1])
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
73
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
74 prada_path=os.path.dirname(os.path.abspath(__file__)) ####
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
75 ref_search_path=[prada_path,os.getcwd()] #search path for ref file if not specified in command line
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
76
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
77 if '-conf' in args:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
78 i=args.index('-conf')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
79 reffile=args[i+1]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
80 if os.path.exists(reffile):
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
81 pass
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
82 else:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
83 for pth in ref_search_path:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
84 new_reffile='%s/%s'%(pth, os.path.basename(reffile))
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
85 if os.path.exists(new_reffile):
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
86 reffile=new_reffile
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
87 break
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
88 else:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
89 sys.exit('ERROR: ref file %s not found'%reffile)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
90 else:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
91 reffile='%s/conf.txt'%prada_path
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
92 if not os.path.exists(reffile):
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
93 sys.exit('ERROR: No default conf.txt found and none specified')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
94
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
95 #reference files
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
96 refdict=ioprada.read_conf(reffile)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
97 ref_anno=refdict['--REF--']['ref_anno']
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
98 ref_map=refdict['--REF--']['ref_map']
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
99 ref_fasta=refdict['--REF--']['ref_fasta']
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
100 featurefile=refdict['--REF--']['feature_file']
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
101
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
102 samtools='%s/tools/samtools-0.1.16/samtools'%prada_path
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
103 bwa='%s/tools/bwa-0.5.7-mh/bwa'%prada_path
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
104
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
105 print 'GUESS start: %s'%time.ctime()
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
106 print 'CMD: %s'%('\t'.join(args))
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
107
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
108 ##################################################################################
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
109 ##get gene position information
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
110
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
111 gA,gB=ioprada.read_feature_genes(featurefile,Gene_A,Gene_B)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
112 if gA is None:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
113 sys.exit('%s not found'%Gene_A)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
114 if gB is None:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
115 sys.exit('%s not found'%Gene_B)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
116
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
117 #Generate unmapped reads.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
118 if extract_mask:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
119 cmd='%s view -b -f 4 -F 8 %s > %s'%(samtools,sourcefile,unmapbam)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
120 cmdout=subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,shell=True)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
121 while True:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
122 if cmdout.poll() is None:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
123 print 'Extracting unmapped reads...'
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
124 time.sleep(120)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
125 pass
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
126 if cmdout.poll()==0:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
127 print 'Extracted unmapped reads'
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
128 break
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
129 if cmdout.poll() is not None and cmdout.poll() != 0:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
130 raise Exception('Error extracting unmapped reads')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
131
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
132 #Generate junction db
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
133 juncfile='%s_%s.junction.fasta'%(Gene_A,Gene_B)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
134 cmd='perl %s/make_exon_junctions.pl %s %s %s %s %s %d > %s/%s'%(prada_path,Gene_A,Gene_B,ref_anno,ref_map,ref_fasta,junL,outdir,juncfile)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
135 cmdout=subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,shell=True)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
136 while True:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
137 if cmdout.poll() is None:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
138 print 'Generating junction db. Waiting...'
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
139 time.sleep(20)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
140 pass
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
141 if cmdout.poll()==0:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
142 print 'Generated Junction DB.'
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
143 break
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
144 if cmdout.poll() is not None and cmdout.poll() != 0:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
145 raise Exception('Error generated Junction DB.')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
146
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
147 #read into the sam file, get gene-strand information
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
148 print 'Finding discordant read pairs.'
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
149 samfile = pysam.Samfile(sourcefile, "rb" )
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
150
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
151 #mapping quality based read collection
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
152 #strand information considered
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
153 #mismatch filter considered
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
154 a_reads=[]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
155 b_reads=[]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
156 for alignedread in samfile.fetch(gA.chr,gA.start-1,gA.end):
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
157 if alignedread.mapq >= minmapq:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
158 readastrd='-1' if alignedread.is_reverse else '1'
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
159 mmf=[x[1] for x in alignedread.tags if x[0]=='NM'][0]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
160 if readastrd==gA.strand and mmf <= mm:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
161 a_reads.append(alignedread)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
162
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
163 for alignedread in samfile.fetch(gB.chr,gB.start-1,gB.end):
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
164 if alignedread.mapq >= minmapq:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
165 readbstrd='-1' if alignedread.is_reverse else '1'
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
166 mmf=[x[1] for x in alignedread.tags if x[0]=='NM'][0]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
167 if readbstrd != gB.strand and mmf <= mm:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
168 b_reads.append(alignedread)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
169 samfile.close()
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
170
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
171 ar_ids=[x.qname for x in a_reads]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
172 br_ids=[x.qname for x in b_reads]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
173 disc=list(set(ar_ids).intersection(set(br_ids))) #discordant pairs
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
174
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
175 #detect read pair that one end map to one partner, yet the other does not align
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
176 print 'Determining potential junction spanning reads.'
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
177 rpaonly=[] #reads that only map to gene A -- going to map the other end to junctions
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
178 for rd in a_reads:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
179 if rd.mate_is_unmapped:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
180 rpaonly.append(rd)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
181 rpbonly=[] #reads that only map to gene B -- going to map the other end to junctions
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
182 for rd in b_reads:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
183 if rd.mate_is_unmapped:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
184 rpbonly.append(rd)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
185 rpaonly_names=[x.qname for x in rpaonly]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
186 rpbonly_names=[x.qname for x in rpbonly]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
187
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
188 #find read sequences
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
189 print 'Extracting unmapped read sequences.'
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
190 print 'mate unmapped read for gene A:',len(rpaonly)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
191 print 'mate unmapped read for gene B:',len(rpbonly)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
192 samfile=pysam.Samfile(unmapbam,'rb')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
193 taga='%s-%s_a'%(Gene_A,Gene_B)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
194 tagb='%s-%s_b'%(Gene_A,Gene_B)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
195 resfq_a=open('%s/unmapreads_%s.fq'%(outdir,taga),'w')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
196 resfq_b=open('%s/unmapreads_%s.fq'%(outdir,tagb),'w')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
197 for item in samfile:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
198 if item.qname in rpaonly_names:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
199 resfq_a.write('@%s\n'%item.qname)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
200 resfq_a.write('%s\n'%item.seq)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
201 resfq_a.write('+\n')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
202 resfq_a.write('%s\n'%item.qual)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
203 if item.qname in rpbonly_names:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
204 resfq_b.write('@%s\n'%item.qname)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
205 resfq_b.write('%s\n'%item.seq)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
206 resfq_b.write('+\n')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
207 resfq_b.write('%s\n'%item.qual)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
208 resfq_a.close()
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
209 resfq_b.close()
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
210 samfile.close()
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
211
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
212 ##indexing junction db
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
213 print 'Aligning reads to junction db'
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
214 cmd='%s index %s/%s'%(bwa,outdir,juncfile)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
215 cmdout=subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,shell=True)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
216 while True:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
217 if cmdout.poll() is None:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
218 time.sleep(3)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
219 pass
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
220 if cmdout.poll()==0:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
221 print 'Junction DB indexed.'
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
222 break
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
223 if cmdout.poll() is not None and cmdout.poll() != 0:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
224 raise Exception('Error building junction db index')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
225
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
226 ##align the unmapped reads to junction database
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
227 for rs in [taga,tagb]:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
228 cmd='%s aln -n %d -R 100 %s/%s %s/unmapreads_%s.fq > %s/%s.sai'%(bwa,mm,outdir,juncfile,outdir,rs,outdir,rs)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
229 cmdout=subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,shell=True)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
230 while True:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
231 if cmdout.poll() is None:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
232 time.sleep(5)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
233 pass
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
234 if cmdout.poll()==0:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
235 print 'Aligned unmapped reads group %s'%rs
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
236 break
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
237 if cmdout.poll() is not None and cmdout.poll() != 0:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
238 raise Exception('Error aligning unmapped reads for group %s'%rs)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
239
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
240 cmd='%s samse -n 1000 %s/%s %s/%s.sai %s/unmapreads_%s.fq > %s/%s.sam'%(bwa,outdir,juncfile,outdir,rs,outdir,rs,outdir,rs)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
241 cmdout=subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,shell=True)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
242 while True:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
243 if cmdout.poll() is None:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
244 time.sleep(2)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
245 pass
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
246 if cmdout.poll()==0:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
247 print 'Converting to sam for group %s'%rs
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
248 break
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
249 if cmdout.poll() is not None and cmdout.poll() != 0:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
250 raise Exception('Error converting to sam for group %s'%rs)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
251
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
252 #parse results
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
253 qualrd_a=[]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
254 junc_a=[]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
255 samfile=pysam.Samfile('%s/%s.sam'%(outdir,taga),'r')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
256 for rd in samfile:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
257 if not rd.is_unmapped and rd.is_reverse:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
258 qualrd_a.append(rd)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
259 junc_a.append(samfile.getrname(rd.tid))
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
260 samfile.close()
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
261 qualrd_b=[]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
262 junc_b=[]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
263 samfile=pysam.Samfile('%s/%s.sam'%(outdir,tagb),'r')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
264 for rd in samfile:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
265 if not rd.is_unmapped and not rd.is_reverse:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
266 qualrd_b.append(rd)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
267 junc_b.append(samfile.getrname(rd.tid))
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
268 samfile.close()
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
269
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
270 junc_span=[]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
271 junc_span.extend(qualrd_a)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
272 junc_span.extend(qualrd_b)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
273
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
274 junc_name=[]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
275 junc_name.extend(junc_a)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
276 junc_name.extend(junc_b)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
277
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
278 #Generate a summary report
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
279 sumfile=open('%s/%s_%s.GUESS.summary.txt'%(outdir,Gene_A,Gene_B),'w')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
280 sumfile.write('%s\t%s\n'%(Gene_A,Gene_B))
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
281 sumfile.write('\n')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
282 sumfile.write('>discordant\n')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
283 for rdname in disc:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
284 ia=ar_ids.index(rdname)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
285 ib=br_ids.index(rdname)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
286 reada=a_reads[ia]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
287 readb=b_reads[ib]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
288 mm_a=[x[1] for x in reada.tags if x[0]=='NM'][0]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
289 mm_b=[x[1] for x in readb.tags if x[0]=='NM'][0]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
290 ss1='%s\tF\t%s.%d.mm%d'%(reada.qname,Gene_A,reada.pos+1,mm_a) #pysam use 0-based coordinates
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
291 ss2='%s\tR\t%s.%d.mm%d'%(readb.qname,Gene_B,readb.pos+1,mm_b)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
292 sumfile.write('%s\n'%ss1)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
293 sumfile.write('%s\n'%ss2)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
294
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
295 sumfile.write('\n')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
296 sumfile.write('>spanning\n')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
297 for i in range(len(junc_span)):
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
298 rd=junc_span[i]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
299 jname=junc_name[i]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
300 mm_j=[x[1] for x in rd.tags if x[0]=='NM'][0]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
301 ss='%s\t%s.mm%d'%(rd.qname,jname,mm_j)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
302 sumfile.write('%s\n'%ss)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
303
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
304 sumfile.write('\n')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
305 sumfile.write('>junction\n')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
306 juncol=[]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
307 for item in set(junc_name):
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
308 nn=junc_name.count(item)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
309 juncol.append([item,nn])
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
310 juncol=sorted(juncol,key=lambda x:x[1],reverse=True)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
311 for item in juncol:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
312 sumfile.write('%s\t%s\n'%(item[0],item[1]))
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
313
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
314 sumfile.write('\n')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
315 sumfile.write('>summary\n')
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
316 sumfile.write('Number of Discordant Pairs = %d\n'%len(disc))
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
317 sumfile.write('Number of Fusion Reads = %d\n'%len(junc_span))
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
318 sumfile.write('Number of Distinct Junctions = %d\n'%len(set(junc_name)))
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
319
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
320 sumfile.close()
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
321 print 'Done: %s'%time.ctime()