-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtseq.py
More file actions
executable file
·213 lines (163 loc) · 6.07 KB
/
Copy pathhtseq.py
File metadata and controls
executable file
·213 lines (163 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python
'''
_version=0.0.1
'''
import sys, os, time, threading
from collections import OrderedDict as Ordic
from RNAseqpipe.progsuit import Configuration, Prog_Rsp
mythreads = []
threadLock = threading.Lock()
def write_ed(i,info,threadname,outfiles):
#write end info of multiprocess result
#i the ith thread
threadLock.acquire()
outfiles[i] = info
mythreads.remove(threadname)
threadLock.release()
def waittd(max_p=8):#waiting for all threads done
while len(mythreads):
if len(mythreads) < max_p:break
time.sleep(1)
class Mythread(threading.Thread):
def __init__(self,conf,file,path,gff,plotqa=True,silence=False,tdindx=-1,\
name='',outfiles=[]):
super(Mythread,self).__init__()
self.conf = conf
self.file = file
self.path = path
self.gff = gff
self.qa = plotqa
self.silence = silence
self.tdindx = tdindx #index of thread
self.name = name
self.outfiles = outfiles
def run(self):
r,w = os.pipe()
pid = os.fork()
if pid:
os.close(w)
self.parent(r)
else:
os.close(r)
self.child(w,self.qa)
def child(self,w,qa):#run htseq-count and htseq-qa
if qa:
fqa = htseq_qa(self.conf,self.file,self.path,self.silence)
fcount = htseq_count(self.conf,self.file,self.path,self.gff,self.silence)
#fqa, fcount: qa,count file path (string)
os.write(w,fcount)
os._exit(0)
def parent(self,r):
info = ''
data = os.read(r,64)
while data:
info += data
data = os.read(r,64)
write_ed(self.tdindx,info,self.name,self.outfiles)
def get_filename(path):
return os.path.splitext(os.path.split(path)[1])[0]
def addends(value,ends="/"):
if value.endswith(ends):
return value
else:
return value+"/"
def htseq_count(conf,bam,outpath,gff,silence=False,):
progname = "htseq_count"
filename = get_filename(bam)
order1 = Ordic([(bam,"")])
order1[gff] = ""
outfile = addends(outpath)+filename+".count"
order2 = Ordic([(">",outfile)])
prog = Prog_Rsp(conf,progname,order1,order2,silence)
prog.run()
return outfile
def htseq_qa(conf,bam,outpath,silence=False):
progname = "htseq_qa"
filename = get_filename(bam)
outfile = addends(outpath)+filename
order1 = {bam:"","-o":outfile}
order2 = {}
htseq_qa = Prog_Rsp(conf,progname,order1,order2,silence)
htseq_qa.run()
return outfile
def catcount(conf,files,outfile,silence=False):
#merge count file
progname = "count_merge"
order1 = Ordic([(file,"") for file in files])
order2 = Ordic([("-o",outfile)])
prog = Prog_Rsp(conf,progname,order1,order2,silence)
return prog.run()
def htseqpip(conf,files,mpath,gff,outpath=None,plotqa=True,p=8,silence=False):
#run htseq_count and merge
#files: bam/sam
#outfiles: [countfile0,countfile1,] don't put out qa file
#for no more use of it.
#merged file write to mpath,
#each count file write to outpath
assert isinstance(files,list)
if outpath and len(files) != len(outpath):
error_handle(1,"htseq-count")
outfiles = [None for i in range(len(files))]
for i in range(len(files)):
file = files[i]
try:
path = outpath[i]
except:
path = os.path.dirname(file)
tname = "thread%d"%i
mythread = Mythread(conf,file,path,gff,plotqa,silence,i,name=tname,outfiles=outfiles)
mythreads.append(tname)
mythread.start()
waittd(p)
#waiting for all threads done
waittd(0)
outfile = addends(mpath)+time.strftime("merged%y_%m_%d_%H.count",time.localtime())
catcount(conf,outfiles,outfile)
#outfiles are single count file
#outfile is the merged file which delete the lines startswith __
return outfile
class HTcount(Prog_Rsp):
def __init__(self,Conf,progname,order1={},order2={},silence=False,appendorder=""):
super(HTcount,self).__init__(Conf,progname,order1,order2,silence)
self.apdord = appendorder
def run(self):
#for outside use
order = self.gen_order(self.progname,self.data)
order += self.apdord
sys.stderr.write(order+"\n")
return self.run_order(order,self.progname,self.silence)
def error_handle(code,name="unknown"):
if code == 1:
sys.stderr.write("Error: outpaths should be as many as bam/sam files are")
if code:
sys.stderr.write("Error in %s step.\n" % name)
sys.exit(20)
def test(argv):
with open(argv[0]) as conf:
myconf = Configuration(conf)
bams = argv[1:4]#3 test file
outpath = argv[4]
gff = argv[5]
print(htseqpip(myconf,bams,outpath,gff))
def main(argv):
import argparse, sys, os
parser = argparse.ArgumentParser(description='htseq pip(count each bam/sam and merge result)')
parser.add_argument('infile',help='bam/sam files',nargs='+')
parser.add_argument('-c','--conf',type=argparse.FileType('r'),nargs='?',help='configuration file')
parser.add_argument('-g','--gff_file',nargs='?',help='gff file')
parser.add_argument('-qa','--plotqa',default=False,action='store_true',\
help='whether plot qa reports')
parser.add_argument('-p','--proc',nargs='?',help='processer numbers',type=int,default=8)
parser.add_argument('-o','--outpath',nargs='?',help='out file path',default='./')
args = parser.parse_args(argv)
myconf = Configuration(args.conf)
htseqpip(myconf,args.infile,args.outpath,args.gff_file,plotqa=args.plotqa,p=args.proc)
if __name__ == '__main__':
import sys
#htseq bamf gff outpath configuration
#with open(sys.argv[4]) as conf:
# myconf = Configuration(conf)
#htseq_count(myconf,sys.argv[1],sys.argv[3],gff=sys.argv[2])
#htseq-qa bam outpath
#htseq_qa(myconf,sys.argv[1],sys.argv[2])
main(sys.argv[1:])