-
Notifications
You must be signed in to change notification settings - Fork 2
/
tophat_cufflinks.py
254 lines (189 loc) · 8.18 KB
/
tophat_cufflinks.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
# -*- coding: utf-8 -*-
__author__ = "Andrew Sczesnak"
__email__ = "andrew.sczesnak@med.nyu.edu"
__date__ = "10/26/2011"
__version__ = 0.0
from optparse import OptionParser, OptionGroup
import sys
import os
from wrappers import tophat
from wrappers import cufflinks
from wrappers import samtools
from wrappers import igvtools
import _common
import tempfile
def mkwig(input_bam, output_wig, filter_less_than = 1):
st = samtools.SAMTools()
pileup = st.pileup(input_bam)
out_fp = open(output_wig, "w")
this_chrom = None
for line in pileup:
line_split = line.strip().split()
if int (line_split[3]) < filter_less_than: continue
if line_split[0] != this_chrom:
out_fp.write("variableStep chrom=%s\n" % (line_split[0],))
this_chrom = line_split[0]
out_fp.write("%s\t%s\n" % (line_split[1], line_split[3]))
out_fp.flush()
def parse_options(arguments):
global options, args
parser = OptionParser(usage="%prog [options] <assembly> <tophat.params>",
version="%prog " + str(__version__))
parser.add_option("-o",
dest="output_dir",
metavar="[./th_cl_out]",
default="./th_cl_out",
help="write output files to this directory")
parser.add_option("-p",
dest="num_threads",
type="int",
metavar="[1]",
default=1,
help="number of threads used during analysis")
parser.add_option("-r",
dest="reference",
metavar="reference.gtf",
default=None,
help="reference annotation (i.e. refGene) GTF file")
tophat_opts = OptionGroup(parser, "TopHat Options")
tophat_opts.add_option("--skip-tophat",
dest="skip_tophat",
action="store_true",
default=False,
help="skip execution of TopHat")
tophat_opts.add_option("-c",
dest="num_procs",
type="int",
metavar="[1]",
default=1,
help="number of concurrent TopHat processes")
parser.add_option_group(tophat_opts)
cuff_opts = OptionGroup(parser, "Cufflinks Options")
cuff_opts.add_option("--skip-cufflinks",
dest="skip_cufflinks",
action="store_true",
default=False,
help="skip execution of Cufflinks")
cuff_opts.add_option("-M",
dest="mask",
metavar="mask.gtf",
default=None,
help="ignore all alignment within transcripts in this file")
parser.add_option_group(cuff_opts)
tdf_opts = OptionGroup(parser, "TDF Options")
tdf_opts.add_option("--skip-tdf",
dest="skip_tdf",
action="store_true",
default=False,
help="do not make TDF files for each sample")
tdf_opts.add_option("--filter-less-than",
dest="filter_less_than",
action="store",
type="int",
metavar="[1]",
default=1,
help="Filter positions with less than this coverage")
parser.add_option_group(tdf_opts)
options, args = parser.parse_args(arguments)
if len(args) <> 2:
print "Error: Incorrect number of arguments"
parser.print_help()
sys.exit(0)
if not (options.skip_tophat and options.skip_cufflinks) and not options.reference:
print "Error: Reference GTF required"
parser.print_help()
sys.exit(0)
labels = []
runs = []
for line in open(args[1], "r"):
if line.startswith("#"): continue
line_split = line.strip().split("\t")
if len(line_split) <> 6:
print "Error: Invalid line in params file"
print "\t", line
parser.print_help()
sys.exit(0)
labels.append(line_split[0])
runs.append(dict(zip(["label", "inner_dist", "inner_dist_sd",
"seed_len", "left_reads", "right_reads"],
line_split)))
options.labels = labels
options.runs = runs
options.bowtie_index = _common.bowtie_index[args[0]]
def main():
parse_options(sys.argv[1:])
if not os.path.exists(options.output_dir):
os.mkdir(options.output_dir)
# tophat runs
if not options.skip_tophat:
jobs = []
for label, run_params in zip(options.labels, options.runs):
run_out_dir = os.path.join(options.output_dir, label)
run_input_fastq = [run_params["left_reads"]]
extra_params = ["--seed-length", run_params["seed_len"]]
if run_params["right_reads"] != "None":
run_input_fastq.append(run_params["right_reads"])
extra_params.extend(["--mate-inner-dist", run_params["inner_dist"],
"--mate-std-dev", run_params["inner_dist_sd"]])
jobs.append((run_input_fastq,
["--GTF", options.reference,
"--no-novel-juncs",
"--no-novel-indels",
"--no-coverage-search"] + \
extra_params,
run_out_dir))
th = tophat.TopHat(options.bowtie_index,
options.output_dir,
options.num_threads)
th.run_multiple(jobs, options.num_procs)
# cufflinks runs
if not options.skip_cufflinks:
for label, run_params in zip(options.labels, options.runs):
input_bam = os.path.join(options.output_dir, label, "accepted_hits.bam")
if not os.path.exists(input_bam):
print "Error: Could not find %s" % (input_bam,)
continue
run_out_dir = os.path.join(options.output_dir, "FPKM_" + label)
cl = cufflinks.Cufflinks(run_out_dir, options.num_threads)
cl.run(input_bam, ["--min-frags-per-transfrag", "0",
"-G", options.reference] + \
([] if not options.mask else ["-M", options.mask]))
# make TDF files
if not options.skip_tdf:
# collect sample data
sample_info = []
for label, run_params in zip(options.labels, options.runs):
input_bam = os.path.join(options.output_dir, label, "accepted_hits.bam")
if not os.path.exists(input_bam):
print "Error: Could not find %s" % (input_bam,)
continue
wig_file = tempfile.NamedTemporaryFile("w", suffix=".wig", delete=False)
tdf_out = os.path.join(options.output_dir, label, label + ".tdf")
sample_info.append((input_bam, wig_file, tdf_out))
# make wigs
st = samtools.SAMTools()
pid_list = []
for input_bam, wig_file, tdf_out in sample_info:
pid = _common.fork_and_run(st.wig,
input_bam,
wig_file,
options.filter_less_than)
pid_list.append(pid)
_common.wait_for_slot(pid_list, options.num_threads)
_common.wait_for_slot(pid_list, 0, True)
# make tdfs
igv = igvtools.IGVTools()
pid_list = []
for input_bam, wig_file, tdf_out in sample_info:
pid = _common.fork_and_run(igv.tile,
wig_file.name,
tdf_out,
args[0])
pid_list.append(pid)
_common.wait_for_slot(pid_list, options.num_threads)
_common.wait_for_slot(pid_list, 0, True)
# remove tempfiles
for _, wig_file, _ in sample_info:
os.unlink(wig_file.name)
if __name__ == "__main__":
main()