-
Notifications
You must be signed in to change notification settings - Fork 2
/
aaf_phylokmer_sba.py
executable file
·258 lines (231 loc) · 8.55 KB
/
aaf_phylokmer_sba.py
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# aaf_phylokmer.py
#
# Copyright 2015,2016 Huan Fan
# <hfan22@wisc.edu> & Yann Surget-Groba <yann@xtbg.org.cn>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
import sys, gzip, bz2, os, time
import multiprocessing as mp
from optparse import OptionParser
def smartopen(filename,*args,**kwargs):
if filename.endswith('gz'):
return gzip.open(filename,*args,**kwargs)
elif filename.endswith('bz2'):
return bz2.BZ2File(filename,*args,**kwargs)
else:
return open(filename,*args,**kwargs)
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
def runJob(command, sim):
print command
if not sim:
os.system(command)
return 1
usage = "usage: %prog [options]"
version = '%prog 20160419.2'
parser = OptionParser(usage = usage, version = version)
parser.add_option("-k", dest = "kLen", type = int, default = 25,
help = "k-mer length, default = 25")
parser.add_option("-t", dest = "nThreads", type = int, default = 1,
help = "number of threads to use, default = 1")
parser.add_option("-n", dest = "filter", type = int, default = 1,
help = "k-mer filtering threshold, default = 1")
parser.add_option("-o", dest = "outFile", default = 'phylokmer.dat.gz',
help = "output file, default = phylokmer.dat.gz")
parser.add_option("-d", dest = "dataDir", default = 'data',
help = "directory containing the data, default = data/")
parser.add_option("-G", dest = "memSize", type = int, default = 4,
help = "total memory limit (in GB), default = 4")
parser.add_option("-W", dest = "withKmer", action = 'store_true',
help = "include k-mers in the shared k-mer table, otherwise not")
parser.add_option("-s", dest = "sim", action = 'store_true',
help = "only print commands, do not run them")
(options, args) = parser.parse_args()
nThreads = options.nThreads
n = options.filter
memPerThread = int(options.memSize / float(nThreads))
if not memPerThread:
print 'Not enough memory, decrease nThreads or increase memSize'
sys.exit()
###check the data directory:
if not os.path.isdir(options.dataDir):
print 'Cannot find data directory {}'.format(options.dataDir)
sys.exit(2)
###check for the executable files:
#kmer_countx
if options.kLen > 25:
if os.system('which kmer_countx > /dev/null'):
kmerCount = './kmer_countx'
if not is_exe(kmerCount):
print 'kmer_countx not found. Make sure it is in your PATH or the'
print 'current directory, and that it is executable'
sys.exit(1)
else:
kmerCount = 'kmer_countx'
#kmer_count
else:
if os.system('which kmer_count > /dev/null'):
kmerCount = './kmer_count'
if not is_exe(kmerCount):
print 'kmer_count not found. Make sure it is in your PATH or the'
print 'current directory, and that it is executable'
sys.exit(1)
else:
kmerCount = 'kmer_count'
#kmer_merge
if os.system('which kmer_merge > /dev/null'):
filt = './kmer_merge'
if not is_exe(filt):
print 'kmer_merge not found. Make sure it is in your PATH or the'
print 'current directory, and that it is executable'
sys.exit(1)
else:
filt = 'kmer_merge'
if not options.sim:
if os.path.exists(os.path.join(options.dataDir, options.outFile)):
s = raw_input('{} is already in your data directory, overwrite it? Y/N '
.format(options.outFile))
if s == 'Y' or s == 'y':
print'{} is going to be overwritten'.format(options.outFile)
else:
print'No overwritting, exit'
sys.exit(2)
###Get sample list:
samples = []
for fileName in os.listdir(options.dataDir):
if os.path.isdir(os.path.join(options.dataDir, fileName)):
samples.append(fileName)
else:
if not fileName.startswith('.'):
sample = fileName.split(".")[0]
if sample in samples:
sample = fileName.split(".")[0]+fileName.split(".")[1]
if sample in samples:
print 'Error, redundant sample or file names. Aborting!'
sys.exit(3)
os.system("mkdir {}/{}".format(options.dataDir,sample))
os.system("mv {}/{} {}/{}/".format(options.dataDir,fileName,options.dataDir,sample))
samples.append(sample)
samples.sort()
print 'SPECIES LIST:'
for sample in samples:
print sample
###Prepare kmer_count jobs
jobList = []
for sample in samples:
outFile = '{}.pkdat.gz'.format(sample)
command = '{} -l {} -n {} -G {} -o {} -f '.format(kmerCount, options.kLen,
n, memPerThread, outFile)
command1 = ''
for inputFile in os.listdir(os.path.join(options.dataDir, sample)):
inputFile = os.path.join(options.dataDir, sample, inputFile)
handle = smartopen(inputFile)
firstChar = handle.read(1)
if firstChar == '@':
seqFormat = 'FQ'
elif firstChar == '>':
seqFormat = 'FA'
else:
print 'Error, file {} is not FA or FQ format. Aborting!'.\
format(inputFile)
sys.exit(3)
command1 += " -i '{}'".format(inputFile)
command += '{}{}> {}.wc'.format(seqFormat,command1,sample)
jobList.append(command)
jobList = jobList[::-1]
###Run jobs
pool = mp.Pool(nThreads)
jobs = []
nJobs = 0
batch = 0
count = 0
nBatches = len(jobList) / nThreads
if len(jobList) % nThreads:
nBatches += 1
while 1:
if nJobs == nThreads:
batch += 1
print '\n', time.strftime('%c')
print "running batch {}/{}".format(batch, nBatches)
for job in jobs:
pool.apply_async(runJob, args=[job, options.sim])
pool.close()
pool.join()
pool = mp.Pool(nThreads)
nJobs = 0
jobs = []
if jobList:
command = jobList.pop()
jobs.append(command)
#job = pool.apply_async(runJob, args=[command, options.sim])
nJobs += 1
else:
break
count += 1
if nJobs:
print '\n', time.strftime('%c')
print "running last batch"
for job in jobs:
pool.apply_async(runJob, args=[job, options.sim])
pool.close()
pool.join()
###Merge output wc files
if not options.sim:
if options.outFile.endswith('.gz'):
divFile = options.outFile.rstrip('.gz')+'.wc'
else:
divFile = options.outFile + '.wc'
handle = open(divFile, 'w')
handle.close()
for sample in samples:
kmerFile = sample + '.wc'
os.system('cat {} >> {}'.format(kmerFile, divFile))
os.remove(kmerFile)
###Run kmer_merge
if options.outFile.endswith('.gz'):
outFile = options.outFile
else:
outFile = options.outFile+'.gz'
if not options.sim:
handle = smartopen(outFile, 'w')
print >> handle, '#-k {}'.format(options.kLen)
print >> handle, '#-n {}'.format(n)
for i, sample in enumerate(samples):
print >> handle, '#sample{}: {}'.format(i + 1, sample)
handle.close()
command = "{} -k s -c -d '0' -a 'T,M,F'".format(filt)
command_sba = "{} -k s -c -d '0' -a A".format(filt)
cut = []
if options.withKmer:
cut.append('1')
for i, sample in enumerate(samples):
command += " '{}.pkdat.gz'".format(sample)
command_sba += " '{}.pkdat.gz'".format(sample)
cut.append(str((i + 1) * 2))
command += ' | cut -f {} | gzip >> {}'.format(','.join(cut), outFile)
command_sba += ' | cut -f 1 > test.kmer'
sba_sh = open("kmer_merge.sh",'w')
sba_sh.write(command_sba)
sba_sh.close()
print '\n', time.strftime('%c')
print command
if not options.sim:
os.system(command)
print time.strftime('%c')