-
Notifications
You must be signed in to change notification settings - Fork 0
/
minerva_rna_pipe.py
executable file
·401 lines (320 loc) · 14 KB
/
minerva_rna_pipe.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/hpc/users/hagenj02/luigi_pipeline/vluigi/bin/python3.5
'''#!/home/jake/.virtualenvs/luigi/bin/python
for testing on local machine
'''
import luigi
import luigi.postgres
import psycopg2
import subprocess
import os
import pandas as pd
import datetime
from sqlalchemy import create_engine
from sqlalchemy.schema import CreateSchema
class parameters(luigi.Config):
'''
Class to contain all parameters. Most of these will need to be set from
luigi.cfg file in working directory, which is good because it acts as a log
for parameters of experiment
'''
fastqs = luigi.Parameter(default=None)
exp_dir = luigi.Parameter(default=os.getcwd())
genome_fasta = luigi.Parameter(default=None)
genome_gtf = luigi.Parameter(default=None)
star_genome_folder = luigi.Parameter(default=None)
read_length = luigi.IntParameter(default=100)
stranded = luigi.BoolParameter(default=False)
paried = luigi.IntParameter(default=0)
cores = luigi.IntParameter(default=6)
exp_name = luigi.Parameter(default=
datetime.date.today().strftime("%B%d,%Y"))
class fastqs(luigi.Task):
'''Takes fastqs from parameters (specified in python.cfg),
makes dictionary, and returns fastq based on sample
'''
sample = luigi.Parameter()
def run(self):
fastq_dict = {}
for line in parameters().fastqs.splitlines():
sample, path = line.split(":")
fastq_dict[sample] = path
return fastq_dict
def output(self):
fastq_dict = self.run()
fastq_file_path = fastq_dict[self.sample]
return luigi.LocalTarget(fastq_file_path)
class fastqc(luigi.Task):
'''Runs fastqc, quality control program on fastqs
'''
sample = luigi.Parameter()
def requires(self):
return fastqs(sample=self.sample)
def output_dir(self):
return '%s/fastqc' % parameters().exp_dir
def run(self):
try:
os.makedirs(self.output_dir())
except OSError:
pass
fastqc_command = ['fastqc', self.input().path,
'-o', self.output_dir()]
subprocess.call(fastqc_command)
def output(self):
file_name = os.path.basename(self.input().path).split(".")[0]
return luigi.LocalTarget('%s/%s_fastqc.html' %
(self.output_dir(), file_name))
class star_index(luigi.Task):
'''Index genome to be used with STAR aligner
- More options can be passed to STAR by adding paramters to the config file
'''
star_index = luigi.Parameter(default="", significant=False)
def run(self):
try:
os.makedirs(parameters().star_genome_folder)
except OSError:
pass
star_command = ['STAR',
'--runThreadN %d' % parameters().cores,
'--runMode genomeGenerate',
'--genomeDir %s' % parameters().star_genome_folder,
'--genomeFastaFiles %s' % parameters().genome_fasta,
'--sjdbGTFfile %s' % parameters().genome_gtf,
'--sjdbOverhang %d' % (parameters().read_length - 1),
]
for line in self.star_index.splitlines():
star_command.append(line)
subprocess.call(star_command)
if not os.path.isfile('%s/Genome' % parameters().star_genome_folder):
raise OSError("star_index/Genome file could not be created")
def output(self):
return luigi.LocalTarget('%s/Genome' % parameters().star_genome_folder)
class star_align(luigi.Task):
'''Align fastq to previously indexed genome
More options can be passed to STAR by adding parameters to the
config file
The config file already contains default aligner options
'''
sample = luigi.Parameter()
star_align = luigi.Parameter(default="", significant=False)
def requires(self):
return star_index(), fastqs(sample=self.sample)
def output_dir(self):
return '%s/%s/star' % (parameters().exp_dir, self.sample)
def run(self):
try:
os.makedirs(self.output_dir())
except OSError:
pass
star_command = ['STAR',
'--genomeDir %s' % parameters().star_genome_folder,
'--readFilesIn %s' % self.input()[1].path,
'--runThreadN %d' % parameters().cores,
'--outFileNamePrefix %s/%s.' %
(self.output_dir(), self.sample)
]
for line in self.star_align.splitlines():
star_command.append(line)
subprocess.call(star_command)
if not os.path.isfile('%s/%s.Aligned.sortedByCoord.out.bam' %
(self.output_dir(), self.sample)):
raise OSError("STAR could not create %s bam file" % self.sample)
def output(self):
return luigi.LocalTarget('%s/%s.Aligned.sortedByCoord.out.bam' %
(self.output_dir(), self.sample))
class gene_counter(luigi.Task):
'''Use featureCounts from the subread package to count alignments
that overlap a gene.
This also serves as a base class for counting on
different features besides genes
'''
sample = luigi.Parameter()
annotation = luigi.Parameter(default=parameters().genome_gtf)
bam_generator = luigi.TaskParameter(default=star_align)
feature_to_count = luigi.Parameter(default='exon')
grouper = luigi.Parameter(default='gene_id')
feature_level = luigi.Parameter(default="")
output_name = luigi.Parameter(default="gene")
def output_dir(self):
return '%s/%s/counts' % (parameters().exp_dir, self.sample)
def requires(self):
try:
return self.bam_generator(sample=self.sample), require()
except NameError:
return self.bam_generator(sample=self.sample)
def run(self):
try:
os.makedirs(self.output_dir())
except OSError:
pass
bam_file = self.bam_generator(sample=self.sample).output().path
featureCounts_command = ['featureCounts',
'-T', '%d' % parameters().cores,
'-t', '%s' % self.feature_to_count,
'-g', '%s' % self.grouper, self.feature_level,
'-o', '%s/%s.%s.counts' %
(self.output_dir(),
self.sample,
self.output_name),
'-a', self.annotation,
bam_file]
subprocess.call(featureCounts_command)
def output(self):
return luigi.LocalTarget('%s/%s_%s.counts' % (self.output_dir(), self.sample, self.output_name))
class exon_counter(gene_counter):
feature_to_count = 'exon'
feauture_level = '-f'
grouper = 'transcript_id'
output_name = 'exon'
class extract_exon_annotation(luigi.Task):
'''extract only exons from annotation file,
will be used to filter reads that align to an intron
'''
eisa_dir = '%s/eisa' % parameters().exp_dir
def run(self):
try:
os.makedirs(self.eisa_dir)
except OSError:
pass
with open('%s/exons.gtf' % self.eisa_dir, 'a') as f:
for line in open(parameters().genome_gtf):
if "##" in line:
print(line, end = "", file = f)
else:
cols = line.split()
if cols[2] == 'exon':
print(line, end = "", file = f)
def output(self):
return luigi.LocalTarget('%s/exons.gtf' % self.eisa_dir)
class filter_nonexon(luigi.Task):
'''Make bam file with reads that do not overlap any exons
Should end up with a bam file with reads that align to an intron region
or do not align at all
'''
sample = luigi.Parameter()
exon_gtf = luigi.Parameter(extract_exon_annotation().output().path)
def output_dir(self):
return '%s/eisa/%s' % (parameters().exp_dir, self.sample)
def requires(self):
return star_align(self.sample), extract_exon_annotation()
def run(self):
try:
os.makedirs(self.output_dir())
except OSError:
pass
bam_file = self.input()[0]
intersect_command = [
'bedtools', 'intersect', '-a', bam_file.path, '-b', self.exon_gtf, '-v'
]
with open('%s/%s.intron.bam' % (self.output_dir(), self.sample), 'w') as f:
subprocess.call(intersect_command, stdout=f)
def output(self):
return luigi.LocalTarget('%s/%s.intron.bam' % (self.output_dir(), self.sample))
class intron_counter(gene_counter):
bam_generator = filter_nonexon
feature_to_count = 'gene'
output_name = 'intron'
class extract_protein_coding_annotation(luigi.Task):
def run(self):
with open('%s/protein_coding.gtf' % parameters().exp_dir, 'a') as f:
for line in open(parameters().genome_gtf):
if "##" in line:
print(line, end = "", file = f)
else:
if 'protein_coding' in line:
print(line, end = "", file = f)
def output(self):
return luigi.LocalTarget('%s/protein_coding.gtf' % parameters().exp_dir)
class protein_coding_gene_counter(gene_counter):
annotation = extract_protein_coding_annotation().output().path
require = extract_protein_coding_annotation()
output_name = "gene_protein_code"
class protein_coding_gene_intron_counter(gene_counter):
require = extract_protein_coding_annotation
bam_generator = luigi.TaskParameter(filter_nonexon)
feature_to_count = 'gene'
output_name = "intron_protein_code"
annotation = extract_protein_coding_annotation().output().path
class multiQC_test(luigi.Task):
def run(self):
subprocess.call(['multiqc', parameters().exp_dir])
class all_counters(luigi.WrapperTask):
def requires(self):
print(fastqs(sample = "").run())
yield {sample:gene_counter(sample = sample) for sample in fastqs(sample = "").run()}
yield {sample:exon_counter(sample = sample) for sample in fastqs(sample = "").run()}
yield {sample:intron_counter(sample = sample) for sample in fastqs(sample = "").run()}
yield {sample:protein_coding_gene_counter(sample = sample) for sample in fastqs(sample = "").run()}
yield {sample:protein_coding_gene_intron_counter(sample = sample) for sample in fastqs(sample = "").run()}
class postgres_count_matrix(luigi.Task):
password = luigi.Parameter(significant=False)
host = luigi.Parameter(significant=False)
database = 'rna'
user = luigi.Parameter(default='rna', significant=False)
table = luigi.Parameter(default='gene_counts')
feature_counter = luigi.TaskParameter(default=gene_counter, significant=False)
def requires(self):
return {x:self.feature_counter(sample=x) for x in fastqs(sample='').run()}
def run(self):
engine = create_engine('postgresql://%s:%s@%s/%s' %
(self.user, self.password, self.host, self.database))
try:
engine.execute(CreateSchema(parameters().exp_name))
except: # should catch psycopg2.ProgrammingError, but doesnt work
pass
pandas_files = [
pd.read_table(self.input()[name].path,
skiprows=2,
index_col=0,
names=['Gene', 'Chr', 'Start', 'End',
'Strand', 'Length', name],
usecols=['Gene', name],
header=None)
for name in self.input()
]
count_table = pd.concat(pandas_files, axis=1).sort_index(axis=1)
count_table.to_csv("%s/%s.csv" % (parameters().exp_dir, self.table))
count_table.to_sql(self.table, con=engine, schema=parameters().exp_name)
# Taken from luigi source code, makes marker table and adds entry
self.output().create_marker_table()
connection = self.output().connect()
self.output().touch(connection)
connection.commit()
connection.close()
def output(self):
return luigi.postgres.PostgresTarget(host=self.host,
database=self.database,
user=self.user,
password=self.password,
table=self.table,
update_id=parameters().exp_name +
'_' + self.table)
class all_some_task(luigi.WrapperTask):
require = luigi.TaskParameter()
def requires(self):
yield {sample: self.require(sample=sample)
for sample in fastqs(sample="").run()}
class all_count_matrix(luigi.WrapperTask):
password = luigi.Parameter(significant=False)
host = luigi.Parameter(significant=False)
def requires(self):
yield postgres_count_matrix(
password=self.password,
host=self.host)
yield postgres_count_matrix(
table="exon_counts",
feature_counter=exon_counter,
password=self.password, host=self.host)
yield postgres_count_matrix(
table="protein_gene_counts",
feature_counter=protein_coding_gene_counter,
password=self.password, host=self.host)
yield postgres_count_matrix(
table="intron_counts",
feature_counter=intron_counter,
password=self.password, host=self.host)
yield postgres_count_matrix(
table="protein_intron_counts",
feature_counter=protein_coding_gene_intron_counter,
password=self.password, host=self.host)
if __name__ == '__main__':
luigi.run()