-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataprep.py
More file actions
executable file
·266 lines (226 loc) · 7.42 KB
/
dataprep.py
File metadata and controls
executable file
·266 lines (226 loc) · 7.42 KB
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
#!/usr/bin/env python3.9
"""
dataprep.py - version 1.0a1
Author: Gil Oliveira <gpo@ciencias.ulisboa.pt>
PI: Margarida Gama-Carvalho <mhcarvalho@ciencias.ulisboa.pt>
RNA Systems Biology Lab
BioISI - Biosystems and Integrative Sciences Institute
Department of Chemistry and Biochemistry
Faculty of Sciences, University of Lisbon
(C) 2022-2023
"""
import argparse
import gzip
import shutil
import subprocess
import sys
from multiprocessing import Pool
from pathlib import Path
import inquirer
ORIGINAL_FILES_DIR = Path("/data/original_files/")
WORKING_DIR = Path("/data/working_directory/projects/")
CUTADAPT_BIN = Path("/opt/anaconda2/bin/cutadapt")
FASTQC_BIN = Path("/opt/FastQC/fastqc")
ADAPTER_DEFAULT = "AGATCGGAAGAGCGTCGTGTA"
ADAPTER_R1_DEFAULT = "GATCGGAAGAGCACACGTCTGA"
ADAPTER_R2_DEFAULT = "AGATCGGAAGAGCGTCGTGTA"
TRIM_ENDS_DEFAULT = 10
def cutadapt_paired_end(r1_path, r2_path):
global trim_ends
r1 = r1_path.name
r2 = r2_path.name
log_path = (r1_path.parents[1] / "cutadapt") / (
r1_path.stem + "_cutadapt_stdout.txt"
)
with open(log_path, "w") as f:
instance = subprocess.Popen(
[
CUTADAPT_BIN,
"-a",
adapter_r1,
"-A",
adapter_r2,
"-u",
str(trim_ends),
"-U",
str(trim_ends),
"-o",
r1_path.stem + "_cutadapt.fastq",
"-p",
r2_path.stem + "_cutadapt.fastq",
r1,
r2,
],
cwd=raw_fastq_dir,
stdout=f,
)
instance.wait()
def cutadapt_single_end(fastq_path):
global trim_ends
fastq_name = fastq_path.name
log_path = (fastq_path.parents[1] / "cutadapt") / (
fastq_path.stem + "_cutadapt_stdout.txt"
)
with open(log_path, "w") as f:
instance = subprocess.Popen(
[
CUTADAPT_BIN,
"-a",
adapter,
"-u",
str(trim_ends),
"-o",
fastq_path.stem + "_cutadapt.fastq",
fastq_name,
],
cwd=raw_fastq_dir,
stdout=f,
)
instance.wait()
def cutadapt_all_files():
trim_ends_response = input(
"How many nucleotides should be cut off each end? (default: 0): "
)
adapter_response = input(
f"Insert adapter sequence (default {ADAPTER_DEFAULT}): "
)
global trim_ends
global adapter
if trim_ends_response == "":
trim_ends = 0
else:
trim_ends = int(trim_ends_response)
if adapter_response == "":
adapter = ADAPTER_DEFAULT
else:
adapter = adapter_response
print("Trimming adapters with cutadapt. This may take a while...")
global cutadapt_dir
global raw_fastq_dir
cutadapt_dir.mkdir(parents=True, exist_ok=True)
r1_files = raw_fastq_dir.glob("*R1*.fastq")
r2_files = raw_fastq_dir.glob("*R2*.fastq")
pool = Pool()
if len(list(r1_files)) + len(list(r2_files)) > 2:
# Paired-end files
paired_ends = zip(r1_files, r2_files)
pool.starmap(cutadapt_paired_end, paired_ends)
else:
fastq_files = raw_fastq_dir.glob("*.fastq")
pool.map(cutadapt_single_end, fastq_files)
cutadapt_files = raw_fastq_dir.glob("*_cutadapt.fastq")
for file in cutadapt_files:
shutil.move(file, cutadapt_dir)
def fastqc_file(file):
parent_folder = file.parent
instance = subprocess.Popen(
[
FASTQC_BIN,
file,
"--outdir=" + str(parent_folder / "fastqc_reports"),
],
cwd=file.parents[0],
)
instance.wait()
def fastqc_all_files(fastq_dir):
fastqc_dir = Path(fastq_dir / "fastqc_reports")
Path(fastqc_dir / "html").mkdir(parents=True, exist_ok=True)
fastq_files = fastq_dir.glob("*.fastq")
pool = Pool()
pool.map(fastqc_file, fastq_files)
for file in fastqc_dir.glob("*.html"):
shutil.move(file, fastqc_dir / "html")
def parse_arguments():
"""Parse arguments supplied on runtime and create global variables to
store those values. Any unsupplied parameters will be set to the default
hardcoded values (variables which end in "DEFAULT")
"""
parser = argparse.ArgumentParser(
description="Prep the data for RNA-Seq analysis."
)
parser.add_argument(
"-i",
"--input",
type=str,
metavar="/data/originalFiles/PROJECT/",
help="Directory where the original files are.",
)
args = parser.parse_args()
global project_original_files
project_original_files = args.input
def decompress_file(path):
stem = Path(path).stem
output = project_working_dir / "raw_data_fastq" / stem
with gzip.open(path, "rb") as f_gz, open(output, "wb") as f_ungz:
print("Unzipping", f_gz, "to", f_ungz)
content = f_gz.read()
f_ungz.write(content)
def decompress_data(path):
global project_working_dir
Path(project_working_dir / "raw_data_fastq").mkdir(
parents=True, exist_ok=True
)
files = path.glob("*.gz")
print("Decompressing files...")
pool = Pool()
pool.map(decompress_file, files)
print(
"Done! Decompressed FASTQ files saved in "
f"{project_working_dir}/raw_data_fastq/"
)
def select_project():
projects = [x for x in original_files.iterdir() if x.is_dir()]
print(
"Use your up/down arrows to find the folder you want, and select "
"it with ENTER."
)
questions = [
inquirer.List("project_path", message="Folder:", choices=projects)
]
project_path = Path(inquirer.prompt(questions)["project_path"])
return project_path
def main() -> None:
global original_files
global working_dir
global adapter
global adapter_r1
global adapter_r2
global trim_ends
global project_original_files
original_files = ORIGINAL_FILES_DIR
working_dir = WORKING_DIR
adapter = ADAPTER_DEFAULT
adapter_r1 = ADAPTER_R1_DEFAULT
adapter_r2 = ADAPTER_R2_DEFAULT
trim_ends = TRIM_ENDS_DEFAULT
parse_arguments()
if project_original_files is None:
project_original_files = select_project()
slug = project_original_files.name
project_working_dir = working_dir / slug
yn = input(f"You've selected project {slug}. Is that correct? (Y/n): ")
if yn.lower() not in ("y", "yes", "s", "sim", ""):
sys.exit()
else:
project_original_files = Path(project_original_files)
project_working_dir = working_dir / project_original_files.name
print(f"Using original files in {project_original_files}.")
raw_fastq_dir = project_working_dir / "raw_data_fastq"
cutadapt_dir = project_working_dir / "cutadapt"
yn = input(
"Do you want to uncompress the files into "
f"{project_working_dir}? (Y/n): "
)
if yn.lower() in ("y", "yes", "s", "sim", ""):
decompress_data(project_original_files)
yn = input("Run FastQC on raw data? (Y/n): ")
if yn.lower() in ("y", "yes", "s", "sim", ""):
fastqc_all_files(raw_fastq_dir)
yn = input("Run cutadapt? (Y/n): ")
if yn.lower() in ("y", "yes", "s", "sim", ""):
cutadapt_all_files()
yn = input("Run FastQC on trimmed files? (Y/n): ")
if yn.lower() in ("y", "yes", "s", "sim", ""):
fastqc_all_files(cutadapt_dir)
if __name__ == "__main__":
main()