-
Notifications
You must be signed in to change notification settings - Fork 0
/
skesa.py
140 lines (103 loc) · 3.38 KB
/
skesa.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
#!/usr/bin/env python3
"""
Purpose
-------
This module is intended execute Skesa on paired-end FastQ files.
Expected input
--------------
The following variables are expected whether using NextFlow or the
:py:func:`main` executor.
- ``sample_id`` : Sample Identification string.
- e.g.: ``'SampleA'``
- ``fastq_pair`` : Pair of FastQ file paths.
- e.g.: ``'SampleA_1.fastq.gz SampleA_2.fastq.gz'``
Generated output
----------------
- ``${sample_id}_*.assembly.fasta`` : Main output of skesawith the assembly
- e.g.: ``sample_1_skesa.fasta``
Code documentation
------------------
"""
__version__ = "1.0.1"
__build__ = "16012018"
__template__ = "skesa-nf"
import os
import re
import subprocess
from subprocess import PIPE
from assemblerflow_utils.assemblerflow_base import get_logger, MainWrapper
logger = get_logger(__file__)
def __get_version_skesa():
try:
cli = ["skesa", "--version"]
p = subprocess.Popen(cli, stdout=PIPE, stderr=PIPE)
_, err = p.communicate()
try:
version = re.search("v((\\..*))-", err.decode("utf8")).group(1)
except AttributeError:
version = "undefined"
except Exception as e:
logger.debug(e)
version = "undefined"
return {
"program": "SPAdes",
"version": version,
}
if __file__.endswith(".command.sh"):
SAMPLE_ID = '$sample_id'
FASTQ_PAIR = '$fastq_pair'.split()
logger.debug("Running {} with parameters:".format(
os.path.basename(__file__)))
logger.debug("SAMPLE_ID: {}".format(SAMPLE_ID))
logger.debug("FASTQ_PAIR: {}".format(FASTQ_PAIR))
@MainWrapper
def main(sample_id, fastq_pair):
"""Main executor of the skesa template.
Parameters
----------
sample_id : str
Sample Identification string.
fastq_pair : list
Two element list containing the paired FastQ files.
"""
logger.info("Starting skesa")
# Determine output file
if "_trim." in fastq_pair[0]:
sample_id += "_trim"
version = __get_version_skesa()["version"]
output_file = "{}_skesa{}.fasta".format(sample_id, version.replace(".", ""))
cli = [
"skesa",
"--fastq",
"{},{}".format(fastq_pair[0], fastq_pair[1]),
"--gz",
"--use_paired_ends",
"--cores",
"${task.cpus}"
]
logger.debug("Running Skesa subprocess with command: {}".format(cli))
with open(output_file, "w") as fh:
p = subprocess.Popen(cli, stdout=fh, stderr=PIPE)
stdout, stderr = p.communicate()
# Attempt to decode STDERR output from bytes. If unsuccessful, coerce to
# string
try:
stderr = stderr.decode("utf8")
stdout = stdout.decode("utf8")
except (UnicodeDecodeError, AttributeError):
stderr = str(stderr)
stdout = str(stdout)
logger.info("Finished Skesa subprocess with STDOUT:\\n"
"======================================\\n{}".format(stdout))
logger.info("Fished Skesa subprocesswith STDERR:\\n"
"======================================\\n{}".format(stderr))
logger.info("Finished Skesa with return code: {}".format(
p.returncode))
with open(".status", "w") as fh:
if p.returncode != 0:
fh.write("error")
raise SystemExit(p.returncode)
else:
fh.write("pass")
if __name__ == '__main__':
main(SAMPLE_ID, FASTQ_PAIR)