-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseqalign.py
More file actions
executable file
·303 lines (273 loc) · 8.33 KB
/
seqalign.py
File metadata and controls
executable file
·303 lines (273 loc) · 8.33 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
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
#!/usr/bin/env python3.9
"""
seqalign - 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 multiprocessing
import os
import subprocess
import sys
from pathlib import Path, PurePath
import inquirer
STAR_BIN_LOCATION_DEFAULT = Path("/opt/STAR-2.5.0c/bin/Linux_x86_64/STAR")
WORKING_DIR_DEFAULT = Path("/data/working_directory/projects/")
ORIGINAL_FILES_DIR_DEFAULT = Path("/data/originalFiles/")
GENOMES_LOCATION_DEFAULT = Path("/data/referenceFiles/genome/")
genomes_location = GENOMES_LOCATION_DEFAULT
def parse_args() -> None:
"""Parse arguments provided by the user on runtime."""
parser = argparse.ArgumentParser(description="Get genomes from Ensembl.")
parser.add_argument(
"-g",
"--genomeDir",
type=str,
metavar="/data/referenceFiles/genome/" "boops_boops/star/",
help="directory with STAR genome index",
)
parser.add_argument(
"-o",
"--outdir",
type=str,
metavar="/data/working_directory/PROJECT/",
help="directory with the outputs of your project",
)
args = parser.parse_args()
if args.genomeDir is not None:
global genomeDir
genomeDir = Path(args.genomeDir)
def check_genome_index(genome_location: Path) -> None:
"""Check if there's a STAR index inside a directory."""
index_exists = os.path.isdir(genomes_location + "star")
if not index_exists:
print("No STAR genome index could be found.")
print("Refer to SOP DAM002 for index creation instructions.")
sys.exit()
def align_single(r1) -> None:
global star_bin_location
global genomeDir
global outDir
outDir_single = f"{outDir}/{r1.stem}/"
Path(outDir_single).mkdir(parents=True, exist_ok=True)
p = subprocess.Popen(
[
str(PurePath(star_bin_location)),
"--genomeDir",
str(PurePath(genomeDir)),
"--readFilesIn",
r1.name,
"--outFilterType",
"BySJout",
"--outFilterMultimapNmax",
"1",
"--alignSJoverhangMin",
"8",
"--alignSJDBoverhangMin",
"5",
"--outFilterMismatchNmax",
"1",
"--alignIntronMax",
"100000",
"--outSAMmultNmax",
"1",
"--outSAMattributes",
"All",
"--outSAMstrandField",
"intronMotif",
"--outSAMtype",
"BAM",
"Unsorted",
"SortedByCoordinate",
"--quantMode",
"TranscriptomeSAM",
"GeneCounts",
"--outSAMunmapped",
"Within",
"--outReadsUnmapped",
"Fastx",
"--twopassMode",
"Basic",
"--outSJfilterCountTotalMin",
"8",
"5",
"5",
"5",
"--outSJfilterCountUniqueMin",
"-1",
"-1",
"-1",
"-1",
"--outWigType",
"wiggle",
"read1_5p",
"--limitBAMsortRAM",
"10000000000",
"--outFileNamePrefix",
outDir_single,
],
cwd=sequences_dir,
)
p.communicate()
def align_pair(r1, r2):
global star_bin_location
global genomeDir
global outDir
r1 = PurePath(r1[0]).name
r2 = PurePath(r2[0]).name
outDir_pair = f"{outDir}/{r1.stem}/"
Path(outDir_pair).mkdir(parents=True, exist_ok=True)
p = subprocess.Popen(
[
str(PurePath(star_bin_location)),
"--genomeDir",
str(PurePath(genomeDir)),
"--readFilesIn",
r1,
r2,
"--outFilterType",
"BySJout",
"--outFilterMultimapNmax",
"1",
"--alignSJoverhangMin",
"8",
"--alignSJDBoverhangMin",
"5",
"--outFilterMismatchNmax",
"1",
"--alignIntronMax",
"100000",
"--outSAMmultNmax",
"1",
"--outSAMattributes",
"All",
"--outSAMstrandField",
"intronMotif",
"--outSAMtype",
"BAM",
"Unsorted",
"SortedByCoordinate",
"--quantMode",
"TranscriptomeSAM",
"GeneCounts",
"--outSAMunmapped",
"Within",
"--outReadsUnmapped",
"Fastx",
"--twopassMode",
"Basic",
"--outSJfilterCountTotalMin",
"8",
"5",
"5",
"5",
"--outSJfilterCountUniqueMin",
"-1",
"-1",
"-1",
"-1",
"--outWigType",
"wiggle",
"read1_5p",
"--limitBAMsortRAM",
"10000000000",
"--outFileNamePrefix",
outDir_pair,
],
cwd=sequences_dir,
)
p.communicate()
def find_genomes() -> dict:
"""Find indexed genomes in the system."""
available_genomes = genomes_location.iterdir()
organisms = {}
for species_path in available_genomes:
slug = species_path.name
organisms[slug] = species_path
return organisms
def query_genomeDir() -> Path:
"""Query indexed genomeDir form filesystem."""
available_genomes = find_genomes()
print(
"Use your up/down arrows to find the species you want, and select "
"it with ENTER."
)
questions = [
inquirer.List("species", message="Species", choices=available_genomes.keys())
]
answer = inquirer.prompt(questions)["species"]
chosen_dir = genomes_location / answer / "star"
return chosen_dir
def query_sequences_dir() -> str:
"""Query the user for the directory which contains the filtered
sequences"""
project_paths = working_dir.glob("*/fastfilter")
print(
"Use your up/down arrows to find the folder you want, and select "
"it with ENTER."
)
questions = [
inquirer.List(
"project_path", message="Sequences folder", choices=list(project_paths)
)
]
answer = inquirer.prompt(questions)["project_path"]
return Path(answer)
def query_outDir() -> str:
"""Query the user interactively for the folder to save the reports to."""
project_paths = working_dir.iterdir()
print(
"Use your up/down arrows to find the folder you want, and select "
"it with ENTER."
)
questions = [
inquirer.List(
"project_path", message="Output folder", choices=list(project_paths)
)
]
answer = inquirer.prompt(questions)["project_path"]
return answer
def get_fasta_pairs() -> list:
global sequences_dir
all_r1 = list(sequences_dir.glob("*_R1_*_FILTERED.fastq"))
all_r2 = list(sequences_dir.glob("*_R2_*_FILTERED.fastq"))
if len(list(all_r1)) != len(list(all_r2)):
raise Exception("Unmatching paired ends found! Process aborted.")
paired_ends = list(zip(all_r1, all_r2))
return paired_ends
def main() -> None:
"""Main function"""
global working_dir
global star_bin_location
global sequences_dir
global genomeDir
global outDir
working_dir = WORKING_DIR_DEFAULT
star_bin_location = STAR_BIN_LOCATION_DEFAULT
parse_args()
if "sequences_dir" not in globals():
sequences_dir = Path(query_sequences_dir())
if "genomeDir" not in globals():
genomeDir = Path(query_genomeDir())
if "outDir" not in globals():
outDir = Path(query_outDir())
outDir = outDir / "STAR_results"
paired_ends = get_fasta_pairs()
pool = multiprocessing.Pool()
if len(paired_ends) == 2:
print("Detected single-end reading.")
align_pair(paired_ends[0], paired_ends[1])
elif len(paired_ends) == 0:
print("Detected single-end readings.")
fastq_files = list(sequences_dir.glob("*_FILTERED.fastq"))
pool.map(align_single, fastq_files)
else:
print("Detected paired-end readings.")
pool.starmap(align_pair, paired_ends)
if __name__ == "__main__":
main()