Skip to content

Commit 4403477

Browse files
committed
unaligned queries script and report
1 parent f2fc29a commit 4403477

2 files changed

Lines changed: 30 additions & 28 deletions

File tree

mtsv/commands/snakefiles/binning_snek

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ from mtsv.parsing import (
77
format_cml_params,
88
file_type, outfile_type,
99
make_table)
10-
from mtsv.utils import bin_path, track_file_params, get_database_params
10+
from mtsv.utils import (
11+
bin_path, script_path,
12+
track_file_params, get_database_params)
1113

1214

1315
shell.prefix("set -euo pipefail;")
@@ -60,8 +62,10 @@ try:
6062
except FileNotFoundError:
6163
SAMPLES = config['fastq']
6264
SAMPLES = [os.path.basename(s) for s in SAMPLES]
65+
SAMPLE_NAMES = [os.path.splitext(s)[0] for s in SAMPLES]
6366
UNALIGNED_QUERIES = [os.path.join(config['binning_outpath'],
64-
"unaligned_queries_{}.fasta".format(s)) for s in SAMPLES]
67+
"unaligned_queries_{}.fasta".format(s)) for s in SAMPLE_NAMES]
68+
6569

6670
rule binning_all:
6771
input: REPORT
@@ -73,7 +77,8 @@ rule binning_report:
7377
output:
7478
rep=REPORT
7579
params:
76-
header = SAMPLES
80+
header = SAMPLE_NAMES,
81+
merge_file = config['merge_file']
7782
message:
7883
"""
7984
Running binning report.
@@ -102,21 +107,29 @@ rule binning_report:
102107
"Unique Unaligned Queries"])
103108

104109
file_str = "\n".join(UNALIGNED_QUERIES)
110+
total_hits = summary_data['total_hits']
111+
total_unique_hits = summary_data['total_unique_hits']
112+
total_queries = summary_data['total_queries']
113+
total_unique_queries = summary_data['total_unique_queries']
114+
total_unaligned_queries = summary_data['total_unaligned_queries']
115+
total_unique_unaligned_queries = summary_data[
116+
'total_unique_unaligned_queries']
117+
105118
track_file_params('merge_file', config['merge_file'], config)
106119

107120
report("""
108121
Binning Report
109122
================================
110123
Hits are reported in:\n
111-
{config['merge_file']}\n
112-
There were **{summary_data['total_hits']}** total hits
113-
and **{summary_data['total_unique_hits']}** unique hits out
114-
of **{summary_data['total_queries']}** total queries and
115-
**{summary_data['total_unique_queries']}** unique queries.\n
116-
There were **{summary_data['total_unaligned_queries']}** total
117-
queries and **{summary_data['total_unique_unaligned_queries']}**
124+
**{params.merge_file}**\n
125+
There were **{total_hits}** total hits
126+
and **{total_unique_hits}** unique hits out
127+
of **{total_queries}** total queries and
128+
**{total_unique_queries}** unique queries.\n
129+
There were **{total_unaligned_queries}** total
130+
queries and **{total_unique_unaligned_queries}**
118131
unique queries with no hits.\n
119-
Analigned query sequences are reported in:\n
132+
Unaligned query sequences are reported in:\n
120133
{file_str}.\n
121134
Query Stats:\n
122135
{table}
@@ -132,13 +145,15 @@ rule unaligned_queries:
132145
clps = config['merge_file'],
133146
fasta = config['fasta']
134147
params:
135-
samples = SAMPLES
148+
samples = SAMPLE_NAMES,
149+
outpath = config['binning_outpath']
136150
threads: config['threads']
137151
log: config['log_file']
138152
message:
139153
"""
140154
Finding unaligned queries.
141-
Writing unaligned queries to {output}.
155+
Writing unaligned queries to {output}.
156+
Logging to {log}.
142157
Snakemake scheduler assuming {threads} thread(s)
143158
"""
144159
script: SCRIPT['unaligned_queries']

mtsv/scripts/MTSv_unaligned_queries.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,10 @@
77
from itertools import chain
88
from multiprocessing import Pool
99
from Bio import SeqIO
10-
from mtsv.utils import config_logging, error, line_generator#, fasta_generator
10+
from mtsv.utils import config_logging, error, line_generator, fasta_generator
1111
from mtsv.parsing import file_type, outpath_type, parse_query_id, outfile_type
1212

1313

14-
def fasta_generator(file_name, n_records):
15-
with open(file_name, 'r') as handle:
16-
records = {}
17-
for record in SeqIO.parse(handle, 'fasta'):
18-
records[record.id] = record
19-
if len(records) == n_records:
20-
yield records
21-
records = {}
22-
if len(records):
23-
yield records
24-
return
25-
26-
2714
class Result:
2815
def __init__(self, all_queries, unaligned_queries):
2916
self._all_queries = all_queries
@@ -288,7 +275,7 @@ def parse_args():
288275
config_logging(snakemake.log[0], "INFO")
289276
LOGGER = logging.getLogger(__name__)
290277
get_unaligned_queries(snakemake.input[0],
291-
snakemake.input[1], snakemake.output[0],
278+
snakemake.input[1], snakemake.params[1],
292279
snakemake.params[0], snakemake.threads)
293280
except NameError:
294281
ARGS = parse_args()

0 commit comments

Comments
 (0)