-
Notifications
You must be signed in to change notification settings - Fork 3
/
RiboSeq_pipeline.py
476 lines (396 loc) · 16.2 KB
/
RiboSeq_pipeline.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
'''
RiboSeq Pipeline
Author: Thomas Goodman
Pipeline to process RiboSeq fastqs for data analysis Alignment + QC
Pipeline created using Chunky pipes
'''
## TODO: Clean up code a little
## TODO: Make usable for paired end samples
## Get versions of software used
## Fix picard
import matplotlib
matplotlib.use('Agg')
import os
import multiprocessing
import subprocess
import re
import time
from chunkypipes.components import Software, Parameter, Redirect, Pipe, BasePipeline
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
from collections import Counter
defaultThreads = multiprocessing.cpu_count()
def split_bid(fastq):
return fastq.split('/')[-1].split('.')[0]
def new_dir(outputDir, folder):
return os.path.join(outputDir, folder)
def description(self):
return ['Pipeline to process RiboSeq Data and do QC']
# Required paths to run commands
def configure(self):
return{
'cutadapt':{
'path': 'Full path to cutadapt executable'
},
'STAR':{
'path': 'Full path to STAR executable',
'genomeDir': 'Full path to STAR indexed genome directory',
'GTF_ref': 'Path to gtf annotation for genome'
},
'samtools':{
'path': 'Full path to samtools'
},
'bowtie2':{
'path': 'Full path to bowtie2',
'genome_ref': 'Path to index of genome for bowtie2'
},
'bedtools':{
'path': 'Full path to bedtools',
'hg19_start100': 'Path to sorted start100 annotation bed file',
'grch37_bed': 'Path to sorted grch37 bed file'
},
'read_distribution':{ # RSeq QC package
'path': 'Full path to read_distribution.py',
'hg19_bed': 'Path to hg19_refseq bed12 file'
},
'FastQC':{
'path' : 'Full path to FastQC software'
},
'featureCounts':{
'path' : 'Full path to featureCounts software'
},
'picard':{
'path' : 'Full path and command to run picard ex: java -jar /mnt/picard/dist/picard.jar',
'genomeFasta' : 'Full path to the genome fasta reference file',
'refFlat' : 'Full path to the refFlat.txt reference file'
}
}
# Required input for pipeline software
# Next time put all these paths to annotations in configure
def add_pipeline_args(self, parser):
parser.add_argument('--fastq:lib', required=True, nargs='*', help='Fastq input for pipeline:library name(prefix for files)')
parser.add_argument('--output', required=True, help='Where pipeline output should go')
parser.add_argument('--adapter', default='AGATCGGAAGAGCACACGTCT', help='Adapter sequence for trimming')
parser.add_argument('--threads', default=defaultThreads, help='Threads to be used for multi-threaded programs. Default is 8')
# chunky run RiboSeq_pipe.py --fastqs
# /mnt/cinder/thomas/RiboSeq/Lane5/AWS-3_S3_L005_R1_001.fastq.gz
# --output /mnt/cinder/thomas/RiboSeq/test --threads
# create variables from parser if wanted
fastqFiles = pipeline_args['fastq:lib']
outputDir = pipeline_args['output']
adapter = pipeline_args['adapter']
numThreads = pipeline_args['threads']
# Create output directory
subprocess.call(['mkdir', outputDir])
# Software
cutadapt = Software('cutadapt', pipeline_config['cutadapt']['path'])
star = Software('STAR', pipeline_config['STAR']['path'])
bedtools = Software('bedtools', pipeline_config['bedtools']['path'])
bowtie2 = Software('bowtie2', pipeline_config['bowtie2']['path'])
samtools = Software('samtools', pipeline_config['samtools']['path'])
samtools_sort = Software('samtools sort', pipeline_config['samtools']['path'])
read_distribution = Software('read_distribution.py',
pipeline_config['read_distribution']['path'])
featureCounts = Software('featureCounts', pipeline_config['featureCounts']['path'])
fastQC = Software('FastQC', pipeline_config['FastQC']['path'])
picard = Software('picard', pipeline_config['picard']['path'])
# Change these to just be done in python script?
# Common software tools
awk = Software('awk', 'awk')
sort = Software('sort', 'sort')
uniq = Software('uniq', 'uniq')
paste = Software('paste', 'paste')
cat = Software('cat', 'cat')
grep = Software('grep', 'grep')
# Directories and Files
pathToGenomeDir = pipeline_config['STAR']['genomeDir']
pathToGenome = pipeline_config['bowtie2']['genome_ref']
pathToGtf = pipeline_config['STAR']['GTF_ref']
pathTo_hg19_bed = pipeline_config['read_distribution']['hg19_bed']
pathTo_hg19_bed_start100 = pipeline_config['bedtools']['hg19_start100']
pathTo_grch37_bed = pipeline_config['bedtools']['grch37_bed']
pathTo_genomeFasta = pipeline_config['picard']['genomeFasta']
pathTo_ref_flat = pipeline_config['picard']['refFlat']
'''
remove adaptor and trim
adaptor sequence: AGATCGGAAGAGCACACGTCT
-m 25 discard any reads shorter than 25 nucleotides
keep only reads that had the adaptor sequence --discard-untrimmed
cutadapt -a AGATCGGAAGAGCACACGTCT -m 25 --discard-untrimmed {filename}.fastq.gz
> {filename}_trimmed.fastq.gz 2> {filename}_report.txt
Remove adapters
Only keep reads with adapters, otherwise artifact
Discard reads shorter than 25 bp
'''
# Keep track of Bids in pipeline
bid_list = []
for fastqlib in fastqFiles:
bid_list.append(fastqlib.split(':')[-1])
# Cutadapt
for fastqlib in fastqFiles:
fastq, bid = fastqlib.split(':')
newDir = new_dir(outputDir, bid)
# Make new directories to store data
subprocess.call(['mkdir', newDir])
# consider multi-threading by splitting in multiple files and then combining
cutadapt.run(
Parameter('--quality-base=33'),
Parameter('--minimum-length=25'),
Parameter('--discard-untrimmed'),
Parameter('--output={}/{}.trimmed.fastq.gz'.format(newDir, bid)),
# Parameter('-a', forward_adapter if forward_adapter else 'AGATCGGAAGAGCACACGTCT'),
Parameter('-a', adapter),
Parameter(fastq),
Redirect(stream=Redirect.STDOUT, dest=os.path.join(newDir,
'{}.cutadapt.summary.log'.format(bid)))
)
'''
Bowtie2
bowtie2 --seedlen=23 --un-fq=${filename}_filtered.fq -x $genome -U $file
-S | samtools view -Sb - > ${filename}.rts.bam
Remove snoRNA, rRNA, tRNA, keep only mRna for alignment
'''
for bid in bid_list:
newDir = new_dir(outputDir, bid)
bowtie2.run(
Parameter('--seedlen=23'),
Parameter('--threads', numThreads),
Parameter('--un-gz {}/{}_filtered.fq.gz'.format(newDir, bid)),
Parameter('-x', pathToGenome), # Path to rtsRNA_seqs files
Parameter('-U', '{}/{}.trimmed.fastq.gz'.format(newDir, bid)),
Parameter('-S'),
Parameter('{}/{}.rts.sam'.format(newDir, bid)),
Redirect(stream=Redirect.STDOUT, dest=os.path.join(newDir,
'{}.bowtie2.log'.format(bid))),
Redirect(stream=Redirect.STDERR, dest=os.path.join(newDir,
'{}.bowtie2.log2'.format(bid))),
shell=True # Look into changing
)
# This doesn't work
samtools.run(
Parameter('view'),
Parameter('-Sb'),
Parameter('{}/{}.rts.sam'.format(newDir, bid)),
Redirect(stream=Redirect.STDOUT, dest=os.path.join(newDir,
'{}.rts.bam'.format(bid))),
)
'''
Star
STAR --runThreadN 6 --sjdbGTFfile gtfFile --outSAMtype BAM Unsorted
--outFileNamePrefix {filename}_ --genomeDir /path/to/genome/index
--genomeFastaFiles --readFilesIn
{filename}_filtered.fq.gz --readFilesCommand zcat
Basically RNAseq at this point
Align the kept reads from bowtie to the genome
'''
# Only load the genome one time: genomeLoad = 'LoadAndKeep'.....Doesn't really work
for bid in bid_list:
newDir = new_dir(outputDir, bid)
# remove genome from memory on last run
# genomeLoad = 'LoadAndRemove'
star.run(
Parameter('--runThreadN', numThreads), # Change to command line parameter --threads
Parameter('--sjdbGTFfile', pathToGtf),
Parameter('--outSAMtype', 'BAM', 'Unsorted'),
Parameter('--outFileNamePrefix', '{}/{}_'.format(newDir, bid)),
Parameter('--genomeDir', pathToGenomeDir),
# Parameter('--genomeLoad', genomeLoad), broken
Parameter('--readFilesIn', '{}/{}_filtered.fq.gz'.format(newDir, bid)),
Parameter('--readFilesCommand zcat') # reads gzipped files
)
'''
Sort and extract uniquely mapped reads for QC and further analyses
samtools view -H $file > header.sam
samtools view $file | grep -w NH:i:1 | cat header.sam - | samtools view -bS - | samtools sort - ${filename}_uniq_sorted
rm header.sam
Using this file for the rest of the analysis
'''
for bid in bid_list:
newDir = new_dir(outputDir, bid)
samtools.run(
Parameter('view'),
Parameter('-H'),
Parameter('{}/{}_Aligned.out.bam'.format(newDir, bid)), # star outfile name
Redirect(stream=Redirect.STDOUT, dest=os.path.join(newDir,
'{}.header.sam'.format(bid)))
)
samtools.run(
Parameter('view'),
Parameter('{}/{}_Aligned.out.bam'.format(newDir, bid)), # star outfile name
Pipe(
grep.pipe(
Parameter('-w'),
Parameter('NH:i:1'),
Pipe(
cat.pipe(
Parameter(os.path.join(newDir, '{}.header.sam'.format(bid)), '-'),
Pipe(
samtools.pipe(
Parameter('view'),
Parameter('-bS', '-'),
Pipe(
samtools.pipe(
Parameter('sort'),
Parameter('-', '-o', '{}/{}.uniq_sorted.bam'.format(newDir, bid))
)
)
)
)
)
)
)
)
)
# subprocess.call(['rm', '{}/{}.header.sam'.format(newDir, bid)])
'''
rSeQC to evaluate percent reads mapped to each genomic features
read_distribution.py -r hg19_RefSeq.bed12 -i $file
'''
for bid in bid_list:
newDir = new_dir(outputDir, bid)
read_distribution.run(
Parameter('-r'),
Parameter(pathTo_hg19_bed),
Parameter('-i'),
Parameter('{}/{}.uniq_sorted.bam'.format(newDir, bid)),
Redirect(stream=Redirect.STDOUT, dest=os.path.join(newDir,
'{}.read_distribution.log'.format(bid))),
shell=True
)
'''
codon periodicity
annotation=/glusterfs/users/ashieh/annotations/hg19_ccds_exons_plus_start100.bed
bedtools intersect -a {annotation} -b {uniq.bam} -s -bed -wa -wb > intersect_start100
awk -v OFS='\t' '{print ($2-($14+100))}' ${filename}_intersect_start100.bed
| sort | uniq -c > ${filename}_relative_pos_aggregate.table
'''
# bedtools intersect -a {annotation} -b {uniq.bam} -s -bed -wa -wb > intersect_start100
for bid in bid_list:
newDir = new_dir(outputDir, bid)
bedtools.run(
Parameter('intersect'),
Parameter('-a {}'.format(pathTo_hg19_bed_start100)),
Parameter('-b {}/{}.uniq_sorted.bam'.format(newDir, bid)),
Parameter('-s'),
Parameter('-bed'),
Parameter('-wa'),
Parameter('-wb'),
Redirect(stream=Redirect.STDOUT, dest=os.path.join(newDir,
'{}.intersect_start100.bed'.format(bid))),
shell=True
)
start100_file = open('{}/{}.intersect_start100.bed'.format(newDir, bid), 'rb')
relativePos_file = open('{}/{}_relative_pos_aggregate.table'.format(newDir, bid), 'wb')
distanceList = []
for line in start100_file:
splitLine = line.split('\t')
# Really is relative start
if len(splitLine) >= 7:
distance = int(splitLine[7]) - (int(splitLine[1]) + 100)
distanceList.append(distance)
distanceList.sort()
distanceCounting = Counter(distanceList)
for key, value in distanceCounting.iteritems():
relativePos_file.write("{}\t{}\n".format(value,key))
# Create chart of relative_positions_aggregate to see codon periodicity
for bid in bid_list:
newDir = new_dir(outputDir, bid)
rpaFile = open('{dir}/{bid}_relative_pos_aggregate.table'.format(dir=newDir, bid=bid), 'rb')
myDict = {}
for i in range(-30,31):
myDict[i] = 0
for line in rpaFile:
Frequency, start = line.strip().split(' ')
if int(start) >= -30 and int(start) <= 30:
# print start
myDict[int(start)] = Frequency
# Change to log scaling?
freqs = []
starts = []
for i in range(-30, 31):
starts.append(i)
freqs.append(myDict[i])
# print freqs
fig, ax = plt.subplots()
# plt.set_title('{} codon periodicity'.format(bid))
plt.xlabel("-30 to 30 relative position")
plt.ylabel("Frequency")
plt.bar(starts, freqs)
fig.savefig('{dir}/{bid}_codon_periodicity_plot.png'.format(dir=newDir, bid=bid))
'''
Picard tools
java -jar picard.jar CollectMultipleMetrics
I=2017-221.uniq_sorted.bam
O= multiple_metrics
R=GRCh37.p13.genome.fa
java -jar picard.jar CollectGcBiasMetrics
I= .uniq
O=gc_bias_metrics.txt
CHART=gc_bias_metrics.pdf
S=summary_metrics.txt
R=reference_sequence.fasta
java -jar picard.jar CollectRnaSeqMetrics
I=input.bam
O=output.RNA_Metrics
REF_FLAT=ref_flat.txt
STRAND=FIRST_READ_TRANSCRIPTION_STRAND
java -jar picard.jar MarkDuplicates
I=input.bam
O=marked_duplicates.bam
M=marked_dup_metrics.txt
ASSUME_SORTED=true
'''
for bid in bid_list:
newDir = new_dir(outputDir, bid)
picard.run(
Parameter('CollectMultipleMetrics'),
Parameter('I={}'.format(bam)), # input
Parameter('O={}/{}.multiple_metrics'.format(newDir, bid)), # output
Parameter('R={}'.format(pathTo_genomeFasta)) # genomeReference
)
picard.run(
Parameter('CollectGcBiasMetrics'),
Parameter('I={}'.format(bam)), # input
Parameter('O={}/{}.gc_bias_metrics'.format(newDir, bid)), # output
Parameter('CHART={}/{}.gc_bias_metrics.pdf'.format(newDir, bid)), # chart
Parameter('S={}/{}.summary_metrics'.format(newDir, bid)), # summary metrics
Parameter('R={}'.format(pathTo_genomeFasta)) # genome reference
)
picard.run(
Parameter('CollectRnaSeqMetrics'),
Parameter('I={}'.format(bam)), # input
Parameter('O={}/{}.RNA_Metrics'.format(newDir, bid)), # output
Parameter('REF_FLAT={}'.format('{}'.format(pathTo_ref_flat))), # ref_flat
Parameter('STRAND=FIRST_READ_TRANSCRIPTION_STRAND') # strandedness
)
picard.run(
Parameter('MarkDuplicates'),
Parameter('I={}/{}.uniq_sorted.bam'.format(newDir, bid)), # input
Parameter('O={}/{}.marked_duplicates.bam'.format(newDir, bid)), # output
Parameter('M={}/{}.marked_dup_metrics.txt'.format(newDir, bid)),# marked dup metrics
Parameter('ASSUME_SORTED=true') # It is sorted
)
'''
subread: featureCounts
featureCounts -a /path_to_gtf/gencode.v19.annotation.gtf -o <bid>.featureCounts <bid>.uniq_sorted.bam
'''
for bid in bid_list:
newDir = new_dir(outputDir, bid)
featureCounts.run(
Parameter('-a', '{}'.format(pathToGtf)), # gtf
Parameter('-s', '1'), # strand-specific read counting
Parameter('-o', '{}/{}.featureCounts'.format(newDir, bid)), # output
Parameter('{}/{}.uniq_sorted.bam'.format(newDir, bid)) # input
)
'''
FastQC
fastqc --outdir=/path_to/<bid>/ /path_to_fastq/<bid>.fastq.gz
'''
for fastqlib in fastqFiles:
fastq, bid = fastqlib.split(':')
newDir = new_dir(outputDir, bid)
fastQC.run(
Parameter('--outdir={}'.format(newDir)), # output
Parameter('--t', numThreads),
Parameter(fastq) # input
)