-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathYAPDemuxedMateAssemble.py
254 lines (192 loc) · 7.05 KB
/
YAPDemuxedMateAssemble.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
########################################################################################
## This file is a part of YAP package of scripts. https://github.com/shpakoo/YAP
## Distributed under the MIT license: http://www.opensource.org/licenses/mit-license.php
## Copyright (c) 2011-2013 Sebastian Szpakowski
########################################################################################
#################################################
## Pipeline for assemblying Mates
#################################################
import sys, subprocess
from optparse import *
from StepsLibrary import *
from StepsLibrary_EXP import *
from collections import defaultdict
_author="Sebastian Szpakowski"
_date="2012/12/17"
_version="Version 2"
#################################################
## Classes
##
class InfoParser:
def __init__(self, dirname):
self.sampleinfo=defaultdict(list)
for mate in (1,2):
for file in glob.glob("%s/*.mate%s" % (dirname, mate)):
filename = file.strip().split("/")[-1]
id = filename.split(".")[-2]
self.sampleinfo[id].append("%s/%s" % (dirname, filename))
def getSamples(self):
return self.sampleinfo.keys()
def getFiles(self, id):
return self.sampleinfo[id][0:2]
#################################################
## Functions
##
def countLines(fname):
#http://stackoverflow.com/questions/845058/how-to-get-line-count-cheaply-in-python
p = subprocess.Popen(['wc', '-l', fname], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
result, err = p.communicate()
if p.returncode != 0:
raise IOError(err)
return int(result.strip().split()[0])
#################################################
## Arguments
##
parser = OptionParser()
parser.add_option("-i", "--infodir", dest="dir_info",
help="a directory with demultiplexed mate1 and mate2", metavar="dir")
parser.add_option("-r", "--reference", dest="reference", default="human",
help="reference genome of the host (to filter out contaminants)", metavar="human")
parser.add_option("-P", "--PROJECT", dest="project", default="",
help="project code", metavar="#")
parser.add_option("-E", "--EMAIL", dest="email", default="",
help="e-mail address", metavar="@")
parser.add_option("-I", "--imported", dest="importflag", default=False, action="store_true",
help="Unless specified, this script will only import the data. Useful for running the script first on the archive1 server.")
parser.add_option("-H", "--head", dest="head", default=0, type="int",
help="For dry runs, import only # of lines from the input files")
parser.add_option("-S", "--sequences",
dest="minSeqs",type="int", default=1000,
help="Make sure the input files have at least n sequences [default: %default]")
parser.add_option("-x", "--independent", dest="matesindependent", action = "store_true", default=False,
help="""If Specified, mates will be treated independently, otherwise complete pairs will be used. [%default]""", metavar="#")
group = OptionGroup(parser, "Technical", "\"behind-the-scene\" options")
group.add_option("-T", "--threads",
dest="maxThreads",type="int", default=4,
help="maximum number of threads to spawn. [default: %default]")
group.add_option("-N", "--nodes",
dest="maxNodes",type="int", default=125,
help="maximum number of grid-nodes to use. [default: %default]")
group.add_option("-F", "--filesOpen",
dest="maxFiles",type="int", default=50,
help="maximum number of files opened at a given time. [default: %default]")
group.add_option("-C", "--NODESIZE", dest="nodesize", default=30,
help="maximum number of grid node's CPUs to use", metavar="#")
parser.add_option_group(group)
(options, args) = parser.parse_args()
#################################################
## Begin
##
info = InfoParser(options.dir_info)
QC=list()
CLEAN= list()
ASSEMBLIES = list()
COVERAGES = list()
READS = list()
init(options.project, options.email, maxnodes = options.maxNodes)
if options.reference =="mouse":
ref = "/usr/local/projects/KFLAB/sszpakow/annotation/GENOMES/MM10/mm10"
else:
ref = "/usr/local/projects/KFLAB/sszpakow/annotation/GENOMES/HG19/hg19"
for sample in info.getSamples():
#print sample
M1, M2 = info.getFiles(sample)
#print M1, M2
if countLines(M1)/4 >= options.minSeqs and countLines(M2)/4 >= options.minSeqs:
INS = {"mate1": ["%s~%s" % (M1, sample)], "mate2": ["%s~%s" % (M2, sample)]}
if options.head == 0:
x = FileImport(INS)
else:
x = FileMiniImport(INS, {"lines": options.head})
if options.importflag or options.head>0:
Q = getQ(M1)
ARGS = {
"-h": "20",
"-m": "",
"-v": ""
}
sqa1 = SQA(ARGS, [x])
QC.append(sqa1)
# -> 200 000 sequences per file (assuming fastq)
ARGS = {
"types": "mate1,mate2",
"chunk": "800000"
}
chunks = FileSplit(ARGS, [x])
stats1 = fastx_quality_stats(dict(), {"-Q": "%s" %(Q) } , [x])
ARGS = {
"-x" : ref,
"--nondeterministic": "",
"--phred%s" % (Q): "" ,
"--no-unal": "",
"--no-hd": "",
"--no-head": "",
"--no-sq": "",
"--very-fast" : "",
"--no-discordant": "",
"-p": "4"
}
S1 = Bowtie2(dict(), ARGS, [chunks])
ARGS = {
"type": "sam",
"newtype": "filter",
"awk" : " NF>2 {print $1}",
"uniq" : "",
"sort" : "",
"postprocess" : " tr -d \"@\" | sort | uniq "
}
S2 = AwkCommand({}, ARGS, [S1] )
S3 = ContaminantRemoval(dict(), dict(), [S2])
ARGS = {
"-h": "20"
}
S4 = SQAtrim(ARGS, [S3])
ARGS = {
"-l": "30"
}
S4a = SQAlenfil(ARGS, [S4])
ARGS = {
"-Q": Q
}
S5 = fastq2fasta(dict(), ARGS, [S4a])
if options.matesindependent:
S6 = MateMerge(dict(), dict(), [S5], prefix=sample)
else:
S6 = mateInterweave(dict(), dict(), [S5])
S7 = FileMerger("fasta", [S6], prefix=sample)
CLEAN.append(S7)
else:
print "skipping:\n\t%s\n\t%s" % (M1, M2)
if options.importflag or options.head>0:
#############################################################################
### assemble using all samples
###
SALL = FileMerger("fasta", CLEAN, prefix = "ALL")
INS = {"properties": ["/usr/local/packages/clc-ngs-cell/license.properties"]}
if options.matesindependent:
ARGS = {
"-p": "no",
"--cpus": "4",
"-v" : "",
"--no-progress": ""
}
else:
ARGS = {
"-p": "fb ss 180 500",
"--cpus": "4",
"-v" : "",
"--no-progress": ""
}
S8 = CLC_Assemble(INS, ARGS, [SALL])
args = {
"types": "fasta",
"old": "contig",
"new": "ALL"
}
S9 = SED_replace({}, args, [S8])
OutputStep("CLEAN", "fasta", CLEAN)
OutputStep("ASSEMBLED", "fasta", [S9])
#################################################
## Finish
#################################################?