-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.py
315 lines (292 loc) · 14.3 KB
/
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
"""
This program facilitates data collection,
environment set-up, and bash sub-processing
for RNA sequencing.
"""
import os
import shutil
import pandas as pd
import numpy as np
import subprocess
# TODO - add resume option, so that no need to re-run the entire process, especially the copy part
sub_directories = ["species", "clean", "counts",
"fastq", "map", "miscelaneous", "reference"]
table = pd.read_csv("./input/input.csv")
directory_ids = list(set(table["folder_id"].to_numpy()))
myID = table["myID"][0]
start_sh_list = []
def make_directories():
"""
Creates directory structure to store genome data
@param species_name: the species to sequence
"""
print("### creating directories ###")
# TODO - table["species"] can be used to write readme files for human to read in each directory
species = table["species"]
root_dir = "./data/"
if not (os.path.isdir(root_dir)):
os.mkdir(root_dir)
for id in directory_ids:
if not (os.path.isdir(root_dir + id)):
print(f"creating directory " + id)
os.mkdir(root_dir + id)
for dir in sub_directories:
if not (os.path.isdir(root_dir + id + '/' + dir)):
print(f"creating {root_dir + id + '/' + dir}")
os.mkdir(root_dir + id + "/" + dir + "/")
# we can skip this part for now
# for i in range(len(table)):
# os.mkdir(root_directory +
# table.at[i, "folder_id"] + "/species/" + table.at[i, "sample_name"])
def transfer_data():
"""
As of now, extracts data from Phytozome and
moves it to specific species directory
"""
print("### extracting data ###")
# copy genome reference, fasta and gff3 respectively
for i in range(len(table)):
if not os.path.isfile("./data/" + table.at[i, "folder_id"] + "/reference/" + table.at[i, "folder_id"] + "_genome.fa"):
print(f'copying {table.at[i, "genome_fa"]} to {"./data/" + table.at[i,"folder_id"] + "/reference/" + table.at[i, "folder_id"] + "_genome.fa"}')
shutil.copyfile(table.at[i, "genome_fa"], "./data/" + table.at[i,
"folder_id"] + "/reference/" + table.at[i, "folder_id"] + "_genome.fa")
# copy gff3
if not os.path.isfile("./data/" + table.at[i, "folder_id"] + "/reference/" + table.at[i, "folder_id"] + ".gff3"):
print(f'copying {table.at[i, "gene_gff3"]} to {"./data/" + table.at[i,"folder_id"] + "/reference/" + table.at[i, "folder_id"] + ".gff3"}')
shutil.copyfile(table.at[i, "gene_gff3"], "./data/" + table.at[i,
"folder_id"] + "/reference/" + table.at[i, "folder_id"] + ".gff3")
# copy fastq files
print(f'copying {table.at[i, "fastq_dir"] + table.at[i, "sample_name"] + ".R1.fastq"}')
shutil.copyfile(table.at[i, "fastq_dir"] + table.at[i, "sample_name"] + ".R1.fastq",
"./data/" + table.at[i, "folder_id"] + "/fastq/" + table.at[i, "sample_name"] + ".R1.fastq")
shutil.copyfile(table.at[i, "fastq_dir"] + table.at[i, "sample_name"] + ".R2.fastq",
"./data/" + table.at[i, "folder_id"] + "/fastq/" + table.at[i, "sample_name"] + ".R2.fastq")
# copy get_trim_counts
if not os.path.isfile("./data/" + table.at[i, "folder_id"] + "get_trim_sum.py"):
print(f'copying get_trim_sum.py to ./data/{table.at[i, "folder_id"]}')
shutil.copyfile("./helper_scripts/get_trim_sum.py", "./data/" + table.at[i, "folder_id"] + "/get_trim_sum.py")
# copy merge_counts
if not os.path.isfile("./data/" + table.at[i, "folder_id"] + "/merge_counts.py"):
print(f'copying merge_counts.py to ./data/{table.at[i, "folder_id"]}')
shutil.copyfile("./helper_scripts/merge_counts.py", "./data/" + table.at[i, "folder_id"] + "/merge_counts.py")
# copy fpkm
if not os.path.isfile("./data/" + table.at[i, "folder_id"] + "/fpkm_quick.R"):
print(f'copying fpkm_quick.R to ./data/{table.at[i, "folder_id"]}')
shutil.copyfile("./helper_scripts/fpkm_quick.R", "./data/" + table.at[i, "folder_id"] + "/fpkm_quick.R")
def generate_scripts():
print("### generating scripts ###")
for element in directory_ids:
"""
makes a script to prepare for array mapping
"""
element = element.strip()
start_sh_list.append(f"cd ./data/{element}")
start_sh_list.append(f"sbatch start.sh")
start_sh_list.append("cd ../..")
with open("./data/" + element + "/start.sh", "wt") as script:
script.write("#!/bin/sh\n")
script.write("#SBATCH -J " + element + "_script\n")
script.write("#SBATCH --partition batch\n")
script.write("#SBATCH --nodes=1\n")
script.write("#SBATCH --ntasks-per-node=12\n")
script.write("#SBATCH --time=8:00:00\n")
script.write("#SBATCH --mem=30gb\n")
script.write(f"#SBATCH --mail-user={myID}@uga.edu\n")
script.write("#SBATCH --mail-type=BEGIN,END,FAIL\n")
script.write("date\n\n")
script.write("cd $SLURM_SUBMIT_DIR\n")
script.write("module load gffread\n")
script.write("gffread reference/" + element +
".gff3 -T -o reference/" + element + "_gene.gtf\n")
script.write("ml STAR\n")
script.write(f"""STAR \
--runThreadN $SLURM_NTASKS_PER_NODE \
--runMode genomeGenerate \
--genomeSAindexNbases 13 \
--genomeDir ./reference \
--genomeFastaFiles ./reference/{element}_genome.fa \
--sjdbGTFfile ./reference/{element}_gene.gtf
sbatch map.sh""")
# handle array size and generate file.list
sample_list = table[table["folder_id"]
== element].sample_name.to_list()
requested_array_size = len(sample_list)
with open("./data/" + element + "/file.list", "wt") as file_list:
file_list.write("\n".join(sample_list))
with open("./data/" + element + "/map.sh", "wt") as mapping_script:
"""
makes array mappign script
"""
# writing header
mapping_script.write(f"""#!/bin/sh
#SBATCH -J {element}_array_mapping
#SBATCH --partition batch
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=12
#SBATCH --array=1-{requested_array_size}
#SBATCH --time=8:00:00
#SBATCH --mem=30gb
#SBATCH --mail-user={myID}@uga.edu
#SBATCH --mail-type=BEGIN,END,FAIL
date""")
# TODO - reduce the unecessary reassignment of variables, on the python level or bash level?
mapping_script.write("""
### File path and environment setup ###
masterfolder=$SLURM_SUBMIT_DIR
cd ${masterfolder}
cleanfolder=${masterfolder}/clean
mapfolder=${masterfolder}/map
countfolder=${masterfolder}/counts
readsfolder=${masterfolder}/fastq
genomefolder=${masterfolder}/reference
echo $readsfolder
array_configure_file=file.list
sampleFile=`head -n $SLURM_ARRAY_TASK_ID ${array_configure_file} | tail -n1`
trimmo_path=/apps/eb/Trimmomatic/0.39-Java-11
rRNA_ref_path=/work/cjtlab/Database/rRNA_ref
### module load ###
# TODO - do we want to add version here?
module load STAR
module load Trimmomatic
module load Subread
""")
# clean and trim reads
mapping_script.write(f"""
### This is the clean and trim section ###
cd ${{cleanfolder}}
java -jar $EBROOTTRIMMOMATIC/trimmomatic-0.39.jar PE -threads $SLURM_NTASKS_PER_NODE -phred33 \
${{readsfolder}}/${{sampleFile}}.R1.fastq ${{readsfolder}}/${{sampleFile}}.R2.fastq \
${{sampleFile}}_trimP_1.fq.gz ${{sampleFile}}_trimS_1.fq.gz ${{sampleFile}}_trimP_2.fq.gz ${{sampleFile}}_trimS_2.fq.gz \
ILLUMINACLIP:${{trimmo_path}}/adapters/TruSeq3-PE.fa:2:30:10 LEADING:5 TRAILING:5 SLIDINGWINDOW:4:15 MINLEN:36 TOPHRED33 &>${{sampleFile}}_trim.log
""")
# rRNA removal part
mapping_script.write('''
STAR --runThreadN $SLURM_NTASKS_PER_NODE --genomeDir ${rRNA_ref_path} \
--readFilesIn ${sampleFile}_trimP_1.fq.gz ${sampleFile}_trimP_2.fq.gz \
--readFilesCommand gunzip -c --outReadsUnmapped Fastx \
--outFileNamePrefix ${sampleFile}_STAR_ \
--outFilterMismatchNmax 999 \
--outFilterMismatchNoverLmax 0.4 \
--outFilterScoreMinOverLread 0.4 \
--outFilterMatchNminOverLread 0.4 \
--alignMatesGapMax 20 --alignIntronMax 20
STAR --runThreadN $SLURM_NTASKS_PER_NODE --genomeDir ${rRNA_ref_path} \
--readFilesIn ${sampleFile}_trimS_1.fq.gz \
--readFilesCommand gunzip -c --outReadsUnmapped Fastx \
--outFileNamePrefix ${sampleFile}_STAR_S1_ \
--outFilterMismatchNmax 999 \
--outFilterMismatchNoverLmax 0.4 \
--outFilterScoreMinOverLread 0.4 \
--outFilterMatchNminOverLread 0.4 \
--alignMatesGapMax 20 --alignIntronMax 20
STAR --runThreadN $SLURM_NTASKS_PER_NODE --genomeDir ${rRNA_ref_path} \
--readFilesIn ${sampleFile}_trimS_2.fq.gz \
--readFilesCommand gunzip -c --outReadsUnmapped Fastx \
--outFileNamePrefix ${sampleFile}_STAR_S2_ \
--outFilterMismatchNmax 999 \
--outFilterMismatchNoverLmax 0.4 \
--outFilterScoreMinOverLread 0.4 \
--outFilterMatchNminOverLread 0.4 \
--alignMatesGapMax 20 --alignIntronMax 20
printf "Trimming : " >>${sampleFile}_Final.log.txt
grep "^Input Read" ${sampleFile}_trim.log>>${sampleFile}_Final.log.txt
printf "Mapping rRNA : " >>${sampleFile}_Final.log.txt
grep "Number of input read" ${sampleFile}_STAR_Log.final.out >>${sampleFile}_Final.log.txt
printf "Mapping rRNA : " >>${sampleFile}_Final.log.txt
grep "Uniquely mapped reads number" ${sampleFile}_STAR_Log.final.out >>${sampleFile}_Final.log.txt
printf "Mapping rRNA : " >>${sampleFile}_Final.log.txt
grep "Number of reads mapped to multiple loci" ${sampleFile}_STAR_Log.final.out >>${sampleFile}_Final.log.txt
printf "HPT : " >>${sampleFile}_Final.log.txt
grep "MARKER" ${sampleFile}_STAR_Aligned.out.sam |grep -v "^@"|grep "MARKER_HPT"|cut -f 1|sort|uniq|wc -l>>${sampleFile}_Final.log.txt
printf "NPT : " >>${sampleFile}_Final.log.txt
grep "MARKER" ${sampleFile}_STAR_Aligned.out.sam |grep -v "^@"|grep "MARKER_NPT"|cut -f 1|sort|uniq|wc -l>>${sampleFile}_Final.log.txt
printf "IRP : " >>${sampleFile}_Final.log.txt
grep "MARKER" ${sampleFile}_STAR_Aligned.out.sam |grep -v "^@"|grep "MARKER_IRP"|cut -f 1|sort|uniq|wc -l>>${sampleFile}_Final.log.txt
sed '1~4 s|\s.\+$|/1|g' < ${sampleFile}_STAR_Unmapped.out.mate1 | paste - - - - | sort -k1,1 -S 10G -T ./ -V | tr '\\t' '\\n' >${sampleFile}_clean_1.fq
sed '1~4 s|\s.\+$|/2|g' < ${sampleFile}_STAR_Unmapped.out.mate2 | paste - - - - | sort -k1,1 -S 10G -T ./ -V | tr '\\t' '\\n' >${sampleFile}_clean_2.fq
sed -i '1~4 s|$|/1|g' ${sampleFile}_STAR_S1_Unmapped.out.mate1
sed -i '1~4 s|$|/1|g' ${sampleFile}_STAR_S2_Unmapped.out.mate1
mv ${sampleFile}_STAR_S1_Unmapped.out.mate1 ${sampleFile}_clean_S1.fq
sed '1~4 s|/1$|/2|g' < ${sampleFile}_STAR_S2_Unmapped.out.mate1 >${sampleFile}_clean_S2.fq
sed '1~4 s|/1$|/2|g' < ${sampleFile}_clean_S1.fq | \
awk 'NR%4==1{print}NR%4==2{printf "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNN\\n"}NR%4==3{print}NR%4==0{printf "##############################\\n"} ' > ${sampleFile}_clean_E2.fq
cat ${sampleFile}_STAR_S2_Unmapped.out.mate1 | \
awk 'NR%4==1{print}NR%4==2{printf "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNN\\n"}NR%4==3{print}NR%4==0{printf "##############################\\n"} ' > ${sampleFile}_clean_E1.fq
cat ${sampleFile}_clean_S1.fq >> ${sampleFile}_clean_1.fq
cat ${sampleFile}_clean_E2.fq >> ${sampleFile}_clean_2.fq
cat ${sampleFile}_clean_E1.fq >> ${sampleFile}_clean_1.fq
cat ${sampleFile}_clean_S2.fq >> ${sampleFile}_clean_2.fq
rm ${sampleFile}_STAR_SJ.out.tab
rm ${sampleFile}_STAR_Log.progress.out
rm ${sampleFile}_STAR_Log.out
rm ${sampleFile}_STAR_Log.final.out
rm ${sampleFile}_STAR_Aligned.out.sam
rm ${sampleFile}_STAR_S1_SJ.out.tab
rm ${sampleFile}_STAR_S1_Log.progress.out
rm ${sampleFile}_STAR_S1_Log.out
rm ${sampleFile}_STAR_S1_Log.final.out
rm ${sampleFile}_STAR_S1_Aligned.out.sam
rm ${sampleFile}_STAR_S2_SJ.out.tab
rm ${sampleFile}_STAR_S2_Log.progress.out
rm ${sampleFile}_STAR_S2_Log.out
rm ${sampleFile}_STAR_S2_Log.final.out
rm ${sampleFile}_STAR_S2_Aligned.out.sam
rm ${sampleFile}_clean_S1.fq
rm ${sampleFile}_clean_S2.fq
rm ${sampleFile}_clean_E1.fq
rm ${sampleFile}_clean_E2.fq
rm ${sampleFile}_STAR_S2_Unmapped.out.mate1
rm ${sampleFile}_STAR_Unmapped.out.mate1
rm ${sampleFile}_STAR_Unmapped.out.mate2
rm ${sampleFile}_trimP_1.fq.gz
rm ${sampleFile}_trimS_1.fq.gz
rm ${sampleFile}_trimP_2.fq.gz
rm ${sampleFile}_trimS_2.fq.gz
rm ${sampleFile}_trim.log
gzip -f ${sampleFile}_clean_1.fq
gzip -f ${sampleFile}_clean_2.fq
''')
mapping_script.write(f"""
### Mapping with STAR ###
cd ${{mapfolder}}
### STAR mapping ###
STAR --runThreadN $SLURM_NTASKS_PER_NODE \
--genomeDir ${{genomefolder}} --readFilesIn \
${{cleanfolder}}/${{sampleFile}}_clean_1.fq.gz ${{cleanfolder}}/${{sampleFile}}_clean_2.fq.gz --readFilesCommand gunzip -c \
--outSAMtype BAM SortedByCoordinate \
--outFileNamePrefix ${{sampleFile}}_ \
--alignMatesGapMax 20000 \
--alignIntronMax 10000 \
--outFilterScoreMinOverLread 0.1 \
--outFilterMatchNminOverLread 0.1
### Count by Subread ###
cd ${{countfolder}}
featureCounts -Q 2 -s 0 -T $SLURM_NTASKS_PER_NODE -p -C \
-a ${{genomefolder}}/{element}_gene.gtf \
-o ${{sampleFile}}_counts.txt ${{mapfolder}}/${{sampleFile}}_Aligned.sortedByCoord.out.bam
## get trim log
cd ..
python get_trim_sum.py ./clean/
## Get map log
grep "" ${{masterfolder}}/map/*Log.final.out > ${{masterfolder}}/all_mapping_logs.txt
# merge counts
python merge_counts.py ./counts counts/{element}_all_counts.tab
date
""")
def submit_scripts():
"""
Submit all scripts to the cluster
"""
print("### Submitting scripts to the cluster ###")
with open(f"submit_all_scripts.sh", "w") as submit_script:
submit_script.write("\n".join(start_sh_list))
bashCommand = f"bash ./submit_all_scripts.sh"
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
print(f"subprocess output: {output}")
print(f"subprocess error: {error}")
# TODO - print out progress
make_directories()
transfer_data()
generate_scripts()
submit_scripts()