forked from dib-lab/MMETSP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrim_qc.py
221 lines (205 loc) · 7.01 KB
/
trim_qc.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
import os
import os.path
from os.path import basename
from urllib import urlopen
from urlparse import urlparse
import subprocess
from subprocess import Popen, PIPE
import urllib
import shutil
import glob
# custom Lisa module
import clusterfunc
#1. Get data from spreadsheet
def get_data(thefile):
count=0
url_data={}
with open(thefile,"rU") as inputfile:
headerline=next(inputfile).split(',')
#print headerline
position_name=headerline.index("ScientificName")
position_reads=headerline.index("Run")
position_ftp=headerline.index("download_path")
for line in inputfile:
line_data=line.split(',')
name="_".join(line_data[position_name].split())
read_type=line_data[position_reads]
ftp=line_data[position_ftp]
name_read_tuple=(name,read_type)
print name_read_tuple
#check to see if Scientific Name and run exist
if name_read_tuple in url_data.keys():
#check to see if ftp exists
if ftp in url_data[name_read_tuple]:
print "url already exists:", ftp
else:
url_data[name_read_tuple].append(ftp)
else:
url_data[name_read_tuple] = [ftp]
return url_data
# run trimmomatic
def run_trimmomatic_TruSeq(trimdir,file1,file2,sra):
<<<<<<< .merge_file_bYoSrk
bash_filename=trimdir+sra+".trim.TruSeq.sh"
# need a better check for whether this process has been run:
#if os.path.isfile(bash_filename):
# print "trim file already written",bash_filename
# else:
j="""#!/bin/bash
java -Xmx10g -jar /home/ubuntu/Trimmomatic-0.33/trimmomatic-0.33.jar PE \\
=======
bash_filename=trimdir+sra+".trim.TruSeq.sh"
clusterfunc.check_dir(trimdir+"qsub_files/")
listoffile = os.listdir(trimdir+"qsub_files/")
#print listoffile
trim_file = trimdir+"qsub_files/""trim."+sra+".log"
#print trim_file
matching = [s for s in listoffile if "trim."+sra+".log" in s]
matching_string = "TrimmomaticPE: Completed successfully"
if os.path.isfile(trim_file):
with open(trim_file) as f:
content = f.readlines()
if len(matching)!=0:
trim_complete = [m for m in content if matching_string in m]
if len(trim_complete)!=0:
print "Already trimmed:",matching
else:
j="""
java -jar /mnt/home/ljcohen/bin/Trimmomatic-0.33/trimmomatic-0.33.jar PE \\
-baseout {}.trim.fq \\
{} {} \\
ILLUMINACLIP:/mnt/home/ljcohen/bin/Trimmomatic-0.33/adapters/combined.fa:2:40:15 \\
SLIDINGWINDOW:4:2 \\
LEADING:2 \\
TRAILING:2 \\
MINLEN:25 &> trim.{}.log
""".format(sra,file1,file2,sra)
orphan_string=make_orphans(trimdir,sra)
commands = [j,orphan_string]
process_name="trim"
module_name_list=""
filename=sra
clusterfunc.qsub_file(trimdir,process_name,module_name_list,filename,commands)
else:
j="""
java -jar /mnt/home/ljcohen/bin/Trimmomatic-0.33/trimmomatic-0.33.jar PE \\
>>>>>>> .merge_file_xj8fMp
-baseout {}.trim.fq \\
{} {} \\
ILLUMINACLIP:/mnt/home/ljcohen/bin/Trimmomatic-0.33/adapters/combined.fa:2:40:15 \\
SLIDINGWINDOW:4:2 \\
LEADING:2 \\
TRAILING:2 \\
MINLEN:25 &> trim.{}.log
""".format(sra,file1,file2,sra)
orphan_string=make_orphans(trimdir,sra)
commands = [j,orphan_string]
process_name="trim"
module_name_list=""
filename=sra
clusterfunc.qsub_file(trimdir,process_name,module_name_list,filename,commands)
def make_orphans(trimdir,sra):
#if os.path.isfile(trimdir+"orphans.fq.gz"):
#if os.stat(trimdir+"orphans.fq.gz").st_size != 0:
# print "orphans file exists:",trimdir+"orphans.fq.gz"
#else:
# print "orphans file exists but is empty:",trimdir+"orphans.fq.gz"
#else:
file1 = trimdir+"qsub_files/"+sra+".trim_1U.fq"
file2 = trimdir+"qsub_files/"+sra+".trim_2U.fq"
orphanlist=file1 + " " + file2
orphan_string="gzip -9c "+orphanlist+" > "+trimdir+"orphans.fq.gz"
print orphan_string
#s=subprocess.Popen(orphan_string,shell=True)
#s.wait()
return orphan_string
def move_files(trimdir,sra):
tmp_trimdir = trimdir + "qsub_files/"
file1 = tmp_trimdir+sra+".trim_1P.fq"
file2 = tmp_trimdir+sra+".trim_2P.fq"
print file1
print file2
if os.path.isfile(file1):
if os.path.isfile(file2):
mv_string1 = "cp "+file1+" "+trimdir
mv_string2 = "cp "+file2+" "+trimdir
#s=subprocess.Popen(mv_string1,shell=True)
#s.wait()
#t=subprocess.Popen(mv_string2,shell=True)
#t.wait()
#if os.path.isfile(trimdir+sra+".trim_1P.fq"):
# if os.path.isfile(trimdir+sra+".trim_2P.fq"):
# print "Files all here:",os.listdir(trimdir)
return mv_string1,mv_string2
def run_move_files(trimdir,sra):
orphan_string=make_orphans(trimdir,sra)
mv_string1,mv_string2 = move_files(trimdir,sra)
commands = [orphan_string,mv_string1,mv_string2]
process_name="move"
module_name_list=""
filename=sra
clusterfunc.qsub_file(trimdir,process_name,module_name_list,filename,commands)
<<<<<<< .merge_file_bYoSrk
=======
def check_files(trimdir,sra):
file1 = trimdir+sra+".trim_1P.fq"
file2 = trimdir+sra+".trim_2P.fq"
if os.path.isfile(file1):
if os.path.isfile(file2):
print "Files all here:",os.listdir(trimdir)
else:
print "Still waiting:",trimdir
>>>>>>> .merge_file_xj8fMp
def execute(url_data,datadir):
for item in url_data.keys():
organism=item[0]
org_seq_dir=datadir+organism+"/"
url_list=url=url_data[item]
for url in url_list:
sra=basename(urlparse(url).path)
newdir=org_seq_dir+sra+"/"
trimdir=newdir+"trim/"
<<<<<<< .merge_file_bYoSrk
#interleavedir=newdir+"interleave/"
=======
>>>>>>> .merge_file_xj8fMp
clusterfunc.check_dir(trimdir)
#interleavedir=newdir+"interleave/"
#clusterfunc.check_dir(interleavedir)
file1=newdir+sra+"_1.fastq"
file2=newdir+sra+"_2.fastq"
if os.path.isfile(file1) and os.path.isfile(file2):
print file1
print file2
<<<<<<< .merge_file_bYoSrk
#fastqc_report(datadir,fastqcdir)
### need to fix so the following steps run themselves:
run_trimmomatic_TruSeq(trimdir,file1,file2,sra)
#interleave_reads(trimdir,sra,interleavedir)
#run_jellyfish(trimdir,sra)
#make_orphans(trimdir)
else:
print "Files do not exist:",file1,file2
#run fastqc on all files
#fastqc_report(trimdir,fastqcdir)
datafiles=["MMETSP_SRA_Run_Info_subset2.csv",
"MMETSP_SRA_Run_Info_subset_d.csv","MMETSP_SRA_Run_Info_subset_a.csv",
"MMETSP_SRA_Run_Info_subset_b.csv"]
basedir="/mnt_redo/mmetsp/"
clusterfunc.check_dir(basedir)
for datafile in datafiles:
url_data=get_data(datafile)
print url_data
execute(url_data,basedir)
=======
#run_trimmomatic_TruSeq(trimdir,file1,file2,sra)
run_move_files(trimdir,sra)
#check_files(trimdir,sra)
#else:
# print "Files do not exist:",file1,file2
datafile="MMETSP_SRA_Run_Info_subset_msu7.csv"
datadir="/mnt/scratch/ljcohen/mmetsp/"
url_data=get_data(datafile)
print url_data
execute(url_data,datadir)
>>>>>>> .merge_file_xj8fMp