drep 4.0.2__tar.gz → 4.0.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {drep-4.0.2 → drep-4.0.4}/PKG-INFO +1 -1
- {drep-4.0.2 → drep-4.0.4}/README.md +2 -2
- drep-4.0.4/drep/VERSION +1 -0
- {drep-4.0.2 → drep-4.0.4}/drep/argumentParser.py +12 -0
- {drep-4.0.2 → drep-4.0.4}/drep/d_bonus.py +9 -0
- {drep-4.0.2 → drep-4.0.4}/drep/d_cluster/compare_utils.py +16 -3
- {drep-4.0.2 → drep-4.0.4}/drep/d_cluster/controller.py +16 -0
- {drep-4.0.2 → drep-4.0.4}/drep/d_cluster/external.py +61 -0
- {drep-4.0.2 → drep-4.0.4}/drep/d_cluster/union_find.py +46 -6
- {drep-4.0.2 → drep-4.0.4}/drep/d_cluster/utils.py +79 -0
- {drep-4.0.2 → drep-4.0.4}/drep/d_filter.py +49 -8
- {drep-4.0.2 → drep-4.0.4}/drep.egg-info/PKG-INFO +1 -1
- drep-4.0.2/drep/VERSION +0 -1
- {drep-4.0.2 → drep-4.0.4}/bin/dRep +0 -0
- {drep-4.0.2 → drep-4.0.4}/drep/WorkDirectory.py +0 -0
- {drep-4.0.2 → drep-4.0.4}/drep/__init__.py +0 -0
- {drep-4.0.2 → drep-4.0.4}/drep/controller.py +0 -0
- {drep-4.0.2 → drep-4.0.4}/drep/d_adjust.py +0 -0
- {drep-4.0.2 → drep-4.0.4}/drep/d_analyze.py +0 -0
- {drep-4.0.2 → drep-4.0.4}/drep/d_choose.py +0 -0
- {drep-4.0.2 → drep-4.0.4}/drep/d_cluster/__init__.py +0 -0
- {drep-4.0.2 → drep-4.0.4}/drep/d_cluster/cluster_utils.py +0 -0
- {drep-4.0.2 → drep-4.0.4}/drep/d_cluster/greedy_clustering.py +0 -0
- {drep-4.0.2 → drep-4.0.4}/drep/d_cluster/parsers.py +0 -0
- {drep-4.0.2 → drep-4.0.4}/drep/d_evaluate.py +0 -0
- {drep-4.0.2 → drep-4.0.4}/drep/d_workflows.py +0 -0
- {drep-4.0.2 → drep-4.0.4}/drep.egg-info/SOURCES.txt +0 -0
- {drep-4.0.2 → drep-4.0.4}/drep.egg-info/dependency_links.txt +0 -0
- {drep-4.0.2 → drep-4.0.4}/drep.egg-info/not-zip-safe +0 -0
- {drep-4.0.2 → drep-4.0.4}/drep.egg-info/requires.txt +0 -0
- {drep-4.0.2 → drep-4.0.4}/drep.egg-info/top_level.txt +0 -0
- {drep-4.0.2 → drep-4.0.4}/helper_scripts/ScaffoldLevel_dRep.py +0 -0
- {drep-4.0.2 → drep-4.0.4}/helper_scripts/parse_stb.py +0 -0
- {drep-4.0.2 → drep-4.0.4}/pyproject.toml +0 -0
- {drep-4.0.2 → drep-4.0.4}/setup.cfg +0 -0
- {drep-4.0.2 → drep-4.0.4}/setup.py +0 -0
- {drep-4.0.2 → drep-4.0.4}/tests/test_suite.py +0 -0
|
@@ -51,8 +51,8 @@ $ dRep check_dependencies
|
|
|
51
51
|
|
|
52
52
|
## Dependencies
|
|
53
53
|
### Near Essential
|
|
54
|
-
* [skani](https://github.com/bluenote-1577/skani) - Makes primary clusters and performs the default secondary comparison (v0.2+
|
|
55
|
-
* [CheckM](http://ecogenomics.github.io/CheckM/) - Determines contamination and completeness of genomes (v1.0.7 confirmed works). Only needed for `dereplicate`; skip it with `--genomeInfo` (raw CheckM2 or CheckM1 output can be passed directly) or `--ignoreGenomeQuality`
|
|
54
|
+
* [skani](https://github.com/bluenote-1577/skani) - Makes primary clusters and performs the default secondary comparison (v0.2.2+ needed; earlier versions lack `--diagonal`)
|
|
55
|
+
* [CheckM](http://ecogenomics.github.io/CheckM/) - Determines contamination and completeness of genomes (v1.0.7 confirmed works; CheckM 1.4+ also needs `gplace`, installed with `pip install gtdb-gplace`, for the default `lineage_wf`). Only needed for `dereplicate`; skip it with `--genomeInfo` (raw CheckM2 or CheckM1 output can be passed directly) or `--ignoreGenomeQuality`
|
|
56
56
|
|
|
57
57
|
### Optional
|
|
58
58
|
|
drep-4.0.4/drep/VERSION
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
4.0.4
|
|
@@ -147,6 +147,18 @@ def parse_args(args):
|
|
|
147
147
|
"partition closely; lower it only if you have very fragmented genomes "
|
|
148
148
|
"and understand the chaining risk.",
|
|
149
149
|
default=15, type=float)
|
|
150
|
+
Clustflags.add_argument("--primary_skani_min_af_both",
|
|
151
|
+
help="Require --primary_skani_min_af in both directions of a pair, "
|
|
152
|
+
"not just one, for it to form a primary-clustering edge "
|
|
153
|
+
"(--primary_algorithm skani only). Recommended for plasmids and "
|
|
154
|
+
"other small genomes: a small genome that is mostly a shared "
|
|
155
|
+
"element (e.g. a transposon) aligns ~100%% to every larger genome "
|
|
156
|
+
"carrying it, and single linkage then chains unrelated genomes "
|
|
157
|
+
"into one huge primary cluster. Secondary clustering only groups "
|
|
158
|
+
"pairs that align in both directions, so setting "
|
|
159
|
+
"--primary_skani_min_af to at most 100 x --cov_thresh does not "
|
|
160
|
+
"change which genomes can end up in the same secondary cluster.",
|
|
161
|
+
action='store_true', default=False)
|
|
150
162
|
Clustflags.add_argument("-ms", "--MASH_sketch", help="MASH sketch size", default=1000)
|
|
151
163
|
Clustflags.add_argument("--SkipMash", help="Skip primary clustering entirely and run secondary\
|
|
152
164
|
clustering on all genomes at once. (Named for when primary clustering was\
|
|
@@ -102,6 +102,15 @@ def check_dependencies(print_out=False):
|
|
|
102
102
|
loc, works = find_program(dep)
|
|
103
103
|
works_message = {True:'all good', False:'!!! ERROR !!!'}[works]
|
|
104
104
|
message = '{0:.<40} {1:15} (location = {2})'.format(dep, works_message, loc)
|
|
105
|
+
if dep == 'skani' and works:
|
|
106
|
+
import drep.d_cluster.external as ext
|
|
107
|
+
text = ext._skani_version(loc)
|
|
108
|
+
version = ext._parse_skani_version(text)
|
|
109
|
+
need = '.'.join(str(x) for x in ext.SKANI_MIN_VERSION)
|
|
110
|
+
if version is not None and version < ext.SKANI_MIN_VERSION:
|
|
111
|
+
message += ' !!! {0} is too old; dRep needs v{1}+ !!!'.format(text, need)
|
|
112
|
+
else:
|
|
113
|
+
message += ' ({0})'.format(text)
|
|
105
114
|
if print_out:
|
|
106
115
|
print(message)
|
|
107
116
|
else:
|
|
@@ -194,6 +194,8 @@ def primary_cluster_skani_sparse(Bdb, data_folder, **kwargs):
|
|
|
194
194
|
# region become edges and single linkage chains them into one huge cluster.
|
|
195
195
|
# See run_skani_triangle_sparse for the measurements behind the default.
|
|
196
196
|
min_af = kwargs.get('primary_skani_min_af', 15)
|
|
197
|
+
# Require min_af in both directions rather than either (issue #311)
|
|
198
|
+
min_af_both = kwargs.get('primary_skani_min_af_both', False)
|
|
197
199
|
|
|
198
200
|
# Secondary applies its own coverage filter at cov_thresh, so the single pass
|
|
199
201
|
# has to emit anything secondary might still care about. Ask skani for the
|
|
@@ -206,14 +208,25 @@ def primary_cluster_skani_sparse(Bdb, data_folder, **kwargs):
|
|
|
206
208
|
|
|
207
209
|
logging.info(f" Running sparse skani primary clustering on {len(genome_list):,} genomes "
|
|
208
210
|
f"(ANI threshold {ani_threshold:.1f}%, screen {screen:.1f}%, "
|
|
209
|
-
f"min-af {min_af}
|
|
211
|
+
f"min-af {min_af}%{' in both directions' if min_af_both else ''}, "
|
|
212
|
+
f"emitting min-af {emit_min_af:.1f}%)")
|
|
210
213
|
sparse_file = drep.d_cluster.external.run_skani_triangle_sparse(
|
|
211
214
|
genome_list, skani_folder, screen, min_af=emit_min_af, **kwargs)
|
|
212
215
|
|
|
213
216
|
all_genomes = list(Bdb['genome'].unique())
|
|
214
|
-
|
|
217
|
+
self_hits = set()
|
|
218
|
+
edges = drep.d_cluster.union_find.load_skani_sparse_edges(sparse_file, self_hits=self_hits)
|
|
219
|
+
# A genome skani skipped has no edges, so it becomes a primary cluster of
|
|
220
|
+
# its own (and an extra representative after dereplication). Say so.
|
|
221
|
+
# skani writes a self row (--diagonal) for every genome it read.
|
|
222
|
+
drep.d_cluster.utils.check_genomes_in_results(
|
|
223
|
+
all_genomes, self_hits, 'primary skani',
|
|
224
|
+
reason="skani skips files it can't read as FASTA/FASTQ, and genomes whose "
|
|
225
|
+
"contigs are all shorter than 500 bp. ",
|
|
226
|
+
consequence="dRep keeps going and puts each of them in a primary cluster of "
|
|
227
|
+
"its own, so each will be its own representative. ")
|
|
215
228
|
Cdb, stats = drep.d_cluster.union_find.cluster_edges(
|
|
216
|
-
edges, P_ani, all_genomes, cov_threshold=min_af / 100.0)
|
|
229
|
+
edges, P_ani, all_genomes, cov_threshold=min_af / 100.0, cov_both=min_af_both)
|
|
217
230
|
|
|
218
231
|
logging.info(f" Sparse skani primary clustering: {stats['edges_kept']:,} edges above "
|
|
219
232
|
f"threshold, {stats['primary_clusters']:,} primary clusters "
|
|
@@ -104,6 +104,11 @@ class GenomeClusterController(object):
|
|
|
104
104
|
else:
|
|
105
105
|
logging.info("Running pair-wise MASH clustering")
|
|
106
106
|
Mdb, Cdb, cluster_ret = drep.d_cluster.compare_utils.all_vs_all_primary(self.Bdb, self.wd.get_dir('MASH'), **self.kwargs)
|
|
107
|
+
missing = drep.d_cluster.utils.check_genomes_in_results(
|
|
108
|
+
self.Bdb['genome'], Cdb['genome'], 'primary clustering',
|
|
109
|
+
consequence="dRep keeps going and puts each of them in a primary "
|
|
110
|
+
"cluster of its own. ")
|
|
111
|
+
Cdb = drep.d_cluster.utils.add_missing_as_own_clusters(Cdb, missing, 'primary')
|
|
107
112
|
|
|
108
113
|
if self.debug:
|
|
109
114
|
logging.debug("Debug mode on - saving Mdb ASAP")
|
|
@@ -176,6 +181,17 @@ class GenomeClusterController(object):
|
|
|
176
181
|
Cdb = drep.d_cluster.utils._gen_nomani_cdb(self.MCdb, data_folder=self.wd.get_dir('data'), **self.kwargs)
|
|
177
182
|
Ndb = pd.DataFrame({'Blank': []})
|
|
178
183
|
|
|
184
|
+
# A genome the secondary algorithm couldn't read is left out of Ndb, and
|
|
185
|
+
# so out of Cdb, i.e. it would silently vanish from the output. Keep it,
|
|
186
|
+
# as a secondary cluster of its own, and say so.
|
|
187
|
+
missing = drep.d_cluster.utils.check_genomes_in_results(
|
|
188
|
+
self.Bdb['genome'], Cdb['genome'], 'secondary clustering ({0})'.format(algorithm),
|
|
189
|
+
reason="{0} returned no comparisons for them. ".format(algorithm),
|
|
190
|
+
consequence="dRep keeps going and puts each of them in a secondary "
|
|
191
|
+
"cluster of its own, so each will be its own representative. ")
|
|
192
|
+
Cdb = drep.d_cluster.utils.add_missing_as_own_clusters(
|
|
193
|
+
Cdb, missing, 'secondary', MCdb=self.MCdb)
|
|
194
|
+
|
|
179
195
|
logging.info(
|
|
180
196
|
"Step 4. Return output")
|
|
181
197
|
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import glob
|
|
2
2
|
import logging
|
|
3
3
|
import os
|
|
4
|
+
import re
|
|
4
5
|
import shutil
|
|
6
|
+
import subprocess
|
|
5
7
|
import sys
|
|
6
8
|
|
|
7
9
|
import numpy as np
|
|
@@ -148,6 +150,11 @@ def run_pairwise_skani(genome_list, outdir, **kwargs):
|
|
|
148
150
|
logdir = False
|
|
149
151
|
drep.thread_cmds([cmd], shell=False, logdir=logdir, t=1)
|
|
150
152
|
|
|
153
|
+
# skani writes nothing if it couldn't read any genome in this group;
|
|
154
|
+
# return no rows so the missing genomes are reported, not a FileNotFoundError
|
|
155
|
+
if not os.path.isfile(out_base):
|
|
156
|
+
return pd.DataFrame()
|
|
157
|
+
|
|
151
158
|
# Load results
|
|
152
159
|
fdb = load_skani(out_base)
|
|
153
160
|
|
|
@@ -318,6 +325,7 @@ def run_skani_triangle_sparse(genome_list, outdir, screen, min_af=15, **kwargs):
|
|
|
318
325
|
glist = _make_glist(genome_list, glist)
|
|
319
326
|
|
|
320
327
|
exe_loc = drep.get_exe('skani')
|
|
328
|
+
check_skani_version(exe_loc)
|
|
321
329
|
out_file = os.path.join(outdir, 'skani_sparse_{0}.tsv'.format(code))
|
|
322
330
|
# min_af matters far more than it looks, because MASH similarity and skani ANI
|
|
323
331
|
# measure different things. MASH compares k-mers across the whole genome, so
|
|
@@ -334,6 +342,11 @@ def run_skani_triangle_sparse(genome_list, outdir, screen, min_af=15, **kwargs):
|
|
|
334
342
|
'-l', glist, '-s', str(screen), '--min-af', str(min_af)]
|
|
335
343
|
if extra_cmd != "":
|
|
336
344
|
cmd += extra_cmd.split(' ')
|
|
345
|
+
# --diagonal adds a self-comparison row for every genome skani managed to
|
|
346
|
+
# read. Without it, a genome skani skipped is indistinguishable from one with
|
|
347
|
+
# no edges (see primary_cluster_skani_sparse).
|
|
348
|
+
if '--diagonal' not in cmd:
|
|
349
|
+
cmd.append('--diagonal')
|
|
337
350
|
|
|
338
351
|
logging.debug(' '.join(cmd) + ' ' + code)
|
|
339
352
|
|
|
@@ -343,9 +356,57 @@ def run_skani_triangle_sparse(genome_list, outdir, screen, min_af=15, **kwargs):
|
|
|
343
356
|
logdir = False
|
|
344
357
|
drep.thread_cmds([cmd], shell=False, logdir=logdir, t=1)
|
|
345
358
|
|
|
359
|
+
# skani writes nothing if it can't read any of the genomes, or if it's too
|
|
360
|
+
# old to know --diagonal; don't let that surface as a FileNotFoundError.
|
|
361
|
+
if not os.path.isfile(out_file):
|
|
362
|
+
msg = ("skani triangle --sparse wrote no output ({0}). Either skani could not "
|
|
363
|
+
"read any of the {1} genomes as FASTA/FASTQ, or skani is older than "
|
|
364
|
+
"v0.2.2 and doesn't support --diagonal. Run with --debug to keep "
|
|
365
|
+
"skani's log in log/cmd_logs/.").format(_skani_version(exe_loc), len(genome_list))
|
|
366
|
+
logging.error(msg)
|
|
367
|
+
raise ValueError(msg)
|
|
368
|
+
|
|
346
369
|
return out_file
|
|
347
370
|
|
|
348
371
|
|
|
372
|
+
SKANI_MIN_VERSION = (0, 2, 2)
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
def _parse_skani_version(text):
|
|
376
|
+
'''
|
|
377
|
+
Return the (major, minor, patch) version in `skani --version` output, or None.
|
|
378
|
+
'''
|
|
379
|
+
m = re.search(r'(\d+)\.(\d+)\.(\d+)', text or '')
|
|
380
|
+
return tuple(int(x) for x in m.groups()) if m else None
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
def check_skani_version(exe_loc):
|
|
384
|
+
'''
|
|
385
|
+
Stop with a clear error if skani is older than SKANI_MIN_VERSION (needed for
|
|
386
|
+
--diagonal). If the version can't be read, warn and carry on.
|
|
387
|
+
'''
|
|
388
|
+
text = _skani_version(exe_loc)
|
|
389
|
+
version = _parse_skani_version(text)
|
|
390
|
+
need = '.'.join(str(x) for x in SKANI_MIN_VERSION)
|
|
391
|
+
if version is None:
|
|
392
|
+
logging.warning("Couldn't read the skani version ({0}); dRep needs skani v{1} "
|
|
393
|
+
"or newer.".format(text, need))
|
|
394
|
+
return
|
|
395
|
+
if version < SKANI_MIN_VERSION:
|
|
396
|
+
msg = ("{0} at {1} is too old: dRep needs skani v{2} or newer (for "
|
|
397
|
+
"--diagonal). Please update skani.").format(text, exe_loc, need)
|
|
398
|
+
logging.error(msg)
|
|
399
|
+
raise ValueError(msg)
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
def _skani_version(exe_loc):
|
|
403
|
+
try:
|
|
404
|
+
out = subprocess.run([exe_loc, '--version'], capture_output=True, text=True, timeout=60)
|
|
405
|
+
return out.stdout.strip() or 'unknown skani version'
|
|
406
|
+
except Exception:
|
|
407
|
+
return 'unknown skani version'
|
|
408
|
+
|
|
409
|
+
|
|
349
410
|
def _make_glist(genomes, floc):
|
|
350
411
|
o = open(floc, 'w')
|
|
351
412
|
for g in genomes:
|
|
@@ -334,7 +334,7 @@ SKANI_SPARSE_COLUMNS = ['Ref_file', 'Query_file', 'ANI',
|
|
|
334
334
|
'Align_fraction_ref', 'Align_fraction_query']
|
|
335
335
|
|
|
336
336
|
|
|
337
|
-
def load_skani_sparse_edges(sparse_files):
|
|
337
|
+
def load_skani_sparse_edges(sparse_files, self_hits=None):
|
|
338
338
|
"""
|
|
339
339
|
Load `skani triangle --sparse` output into a symmetric edge table.
|
|
340
340
|
|
|
@@ -346,6 +346,12 @@ def load_skani_sparse_edges(sparse_files):
|
|
|
346
346
|
alignment_coverage as the aligned fraction of the genome named in the first
|
|
347
347
|
column, and the two directions have different coverages.
|
|
348
348
|
|
|
349
|
+
Args:
|
|
350
|
+
sparse_files: path or list of paths to sparse skani output.
|
|
351
|
+
self_hits: optional set. Names of genomes with a self-comparison row
|
|
352
|
+
(written by `skani triangle --diagonal` for every genome skani
|
|
353
|
+
read) are added to it.
|
|
354
|
+
|
|
349
355
|
Returns:
|
|
350
356
|
DataFrame with ['genome1', 'genome2', 'ani', 'alignment_coverage'],
|
|
351
357
|
where ani and alignment_coverage are 0-1 fractions.
|
|
@@ -366,6 +372,13 @@ def load_skani_sparse_edges(sparse_files):
|
|
|
366
372
|
for x in d['Ref_file'].values])
|
|
367
373
|
b = np.array([drep.d_cluster.utils._get_genome_name_from_fasta(x)
|
|
368
374
|
for x in d['Query_file'].values])
|
|
375
|
+
# drop self comparisons before doubling; they are added back
|
|
376
|
+
# explicitly where needed
|
|
377
|
+
is_self = a == b
|
|
378
|
+
if self_hits is not None:
|
|
379
|
+
self_hits.update(a[is_self])
|
|
380
|
+
keep = ~is_self
|
|
381
|
+
a, b, d = a[keep], b[keep], d[keep]
|
|
369
382
|
ani = (d['ANI'].values / 100).astype(np.float32)
|
|
370
383
|
af_a = (d['Align_fraction_ref'].values / 100).astype(np.float32)
|
|
371
384
|
af_b = (d['Align_fraction_query'].values / 100).astype(np.float32)
|
|
@@ -381,12 +394,28 @@ def load_skani_sparse_edges(sparse_files):
|
|
|
381
394
|
if not frames:
|
|
382
395
|
return pd.DataFrame(columns=['genome1', 'genome2', 'ani', 'alignment_coverage'])
|
|
383
396
|
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
397
|
+
return pd.concat(frames, ignore_index=True)
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
def _reverse_coverage(edges):
|
|
401
|
+
"""
|
|
402
|
+
For each row of an edge table, the alignment_coverage of the same pair in
|
|
403
|
+
the other direction (genome2's aligned fraction), or NaN if that row is
|
|
404
|
+
missing.
|
|
405
|
+
"""
|
|
406
|
+
g1 = edges['genome1'].values
|
|
407
|
+
g2 = edges['genome2'].values
|
|
408
|
+
names = pd.Index(pd.unique(np.concatenate([g1, g2])))
|
|
409
|
+
n = len(names)
|
|
410
|
+
a = names.get_indexer(g1).astype(np.int64)
|
|
411
|
+
b = names.get_indexer(g2).astype(np.int64)
|
|
412
|
+
|
|
413
|
+
cov = pd.Series(edges['alignment_coverage'].values, index=a * n + b)
|
|
414
|
+
cov = cov[~cov.index.duplicated(keep='last')]
|
|
415
|
+
return cov.reindex(b * n + a).values
|
|
387
416
|
|
|
388
417
|
|
|
389
|
-
def cluster_edges(edges, ani_threshold, all_genomes, cov_threshold=0.0):
|
|
418
|
+
def cluster_edges(edges, ani_threshold, all_genomes, cov_threshold=0.0, cov_both=False):
|
|
390
419
|
"""
|
|
391
420
|
Union-find clustering of an in-memory edge table (see load_skani_sparse_edges).
|
|
392
421
|
|
|
@@ -397,6 +426,13 @@ def cluster_edges(edges, ani_threshold, all_genomes, cov_threshold=0.0):
|
|
|
397
426
|
cov_threshold: minimum alignment_coverage (0-1) for a pair to be an edge.
|
|
398
427
|
See run_skani_triangle_sparse for why this matters -- without it,
|
|
399
428
|
genomes sharing a small conserved region chain together.
|
|
429
|
+
cov_both: require cov_threshold in both directions rather than either.
|
|
430
|
+
With either, a small genome that is mostly one shared element (e.g.
|
|
431
|
+
a plasmid that is mostly a transposon) aligns ~100% to every larger
|
|
432
|
+
genome carrying that element and chains them all together (issue
|
|
433
|
+
#311). Secondary clustering only joins pairs that align in both
|
|
434
|
+
directions anyway, so this splits primary clusters without changing
|
|
435
|
+
which genomes secondary clustering can put together.
|
|
400
436
|
|
|
401
437
|
Returns:
|
|
402
438
|
(Cdb, stats)
|
|
@@ -407,7 +443,11 @@ def cluster_edges(edges, ani_threshold, all_genomes, cov_threshold=0.0):
|
|
|
407
443
|
|
|
408
444
|
keep = edges['ani'].values >= ani_threshold
|
|
409
445
|
if cov_threshold > 0:
|
|
410
|
-
|
|
446
|
+
cov = edges['alignment_coverage'].values
|
|
447
|
+
if cov_both:
|
|
448
|
+
# A missing other direction gives NaN, which fails the comparison
|
|
449
|
+
cov = np.minimum(cov, _reverse_coverage(edges))
|
|
450
|
+
keep &= cov >= cov_threshold
|
|
411
451
|
|
|
412
452
|
g1 = edges['genome1'].values[keep]
|
|
413
453
|
g2 = edges['genome2'].values[keep]
|
|
@@ -30,6 +30,85 @@ import drep.d_cluster.external
|
|
|
30
30
|
|
|
31
31
|
|
|
32
32
|
|
|
33
|
+
def check_genomes_in_results(genomes, found, stage, reason='', consequence=''):
|
|
34
|
+
'''
|
|
35
|
+
Log an error naming any input genome that is missing from a clustering step's results.
|
|
36
|
+
|
|
37
|
+
The comparison programs usually just leave out a genome they couldn't read
|
|
38
|
+
(skani, for example, logs "not a valid fasta/fastq file; skipping" and exits
|
|
39
|
+
0, and dRep discards its stderr). This doesn't stop the run: the caller
|
|
40
|
+
decides what happens to the missing genomes and says so in `consequence`,
|
|
41
|
+
so the problem is visible in the log instead of silent.
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
genomes: names of every genome that should be in the results
|
|
45
|
+
found: names of the genomes that are in the results
|
|
46
|
+
stage: which step this is, for the error message
|
|
47
|
+
reason: optional extra explanation for the error message
|
|
48
|
+
consequence: what dRep does with the missing genomes, for the error message
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
list: sorted names of the missing genomes (empty if none)
|
|
52
|
+
'''
|
|
53
|
+
genomes = set(genomes)
|
|
54
|
+
missing = sorted(genomes - set(found))
|
|
55
|
+
if not missing:
|
|
56
|
+
return missing
|
|
57
|
+
|
|
58
|
+
shown = ', '.join(missing[:20])
|
|
59
|
+
if len(missing) > 20:
|
|
60
|
+
shown += ', ... ({0} more)'.format(len(missing) - 20)
|
|
61
|
+
msg = ("{0} of {1} genomes are missing from the {2} results: {3}. {4}{5}"
|
|
62
|
+
"Check that these files are valid FASTA genomes the comparison program can "
|
|
63
|
+
"handle (run with --debug to keep its log in log/cmd_logs/).").format(
|
|
64
|
+
len(missing), len(genomes), stage, shown, reason, consequence)
|
|
65
|
+
logging.error(msg)
|
|
66
|
+
return missing
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def add_missing_as_own_clusters(Cdb, missing, level, MCdb=None):
|
|
70
|
+
'''
|
|
71
|
+
Put each missing genome back into Cdb as a cluster of its own.
|
|
72
|
+
|
|
73
|
+
Args:
|
|
74
|
+
Cdb: primary (level='primary') or secondary (level='secondary') Cdb
|
|
75
|
+
missing: genome names to add
|
|
76
|
+
level: 'primary' or 'secondary'
|
|
77
|
+
MCdb: primary Cdb; needed for level='secondary' to find each genome's
|
|
78
|
+
primary cluster
|
|
79
|
+
|
|
80
|
+
Returns:
|
|
81
|
+
DataFrame: Cdb with one new row per missing genome
|
|
82
|
+
'''
|
|
83
|
+
if len(missing) == 0:
|
|
84
|
+
return Cdb
|
|
85
|
+
|
|
86
|
+
rows = []
|
|
87
|
+
if level == 'primary':
|
|
88
|
+
nxt = (int(pd.to_numeric(Cdb['primary_cluster']).max()) + 1) if len(Cdb) > 0 else 1
|
|
89
|
+
for i, g in enumerate(missing):
|
|
90
|
+
rows.append({'genome': g, 'primary_cluster': nxt + i})
|
|
91
|
+
else:
|
|
92
|
+
pc_of = MCdb.set_index('genome')['primary_cluster'].to_dict()
|
|
93
|
+
# Highest secondary number already used in each primary cluster
|
|
94
|
+
used = {}
|
|
95
|
+
for sc in Cdb['secondary_cluster'] if len(Cdb) > 0 else []:
|
|
96
|
+
pc, _, n = str(sc).rpartition('_')
|
|
97
|
+
used[pc] = max(used.get(pc, 0), int(n))
|
|
98
|
+
template = Cdb.iloc[0].to_dict() if len(Cdb) > 0 else {}
|
|
99
|
+
for g in missing:
|
|
100
|
+
pc = pc_of[g]
|
|
101
|
+
n = used.get(str(pc), 0) + 1
|
|
102
|
+
used[str(pc)] = n
|
|
103
|
+
row = {k: v for k, v in template.items()
|
|
104
|
+
if k in ('threshold', 'cluster_method', 'comparison_algorithm')}
|
|
105
|
+
row.update({'genome': g, 'primary_cluster': pc,
|
|
106
|
+
'secondary_cluster': '{0}_{1}'.format(pc, n)})
|
|
107
|
+
rows.append(row)
|
|
108
|
+
|
|
109
|
+
return pd.concat([Cdb, pd.DataFrame(rows)], ignore_index=True)
|
|
110
|
+
|
|
111
|
+
|
|
33
112
|
def _cluster_Ndb(Ndb, id='primary_cluster', **kwargs):
|
|
34
113
|
'''
|
|
35
114
|
Cluster Ndb on id
|
|
@@ -10,6 +10,7 @@ import glob
|
|
|
10
10
|
import gzip
|
|
11
11
|
import pandas as pd
|
|
12
12
|
import os
|
|
13
|
+
import subprocess
|
|
13
14
|
import sys
|
|
14
15
|
|
|
15
16
|
import drep
|
|
@@ -595,7 +596,7 @@ def calc_n50(loc):
|
|
|
595
596
|
|
|
596
597
|
with open_func(loc, mode) as handle:
|
|
597
598
|
seq_length = 0
|
|
598
|
-
for line in handle:
|
|
599
|
+
for line in _skip_leading_comments(handle):
|
|
599
600
|
if line.startswith('>'):
|
|
600
601
|
if not is_header:
|
|
601
602
|
lengths.append(seq_length)
|
|
@@ -696,14 +697,18 @@ def run_checkM(genome_folder_whole, checkm_outf_whole, **kwargs):
|
|
|
696
697
|
dbs = []
|
|
697
698
|
for genome_folder, checkm_outf in _iterate_checkm_groups(genome_folder_whole, checkm_outf_whole, checkm_group_size):
|
|
698
699
|
|
|
699
|
-
# Run checkM initial
|
|
700
|
+
# Run checkM initial. Its own results table isn't used (the `qa` run
|
|
701
|
+
# below makes the one dRep reads), so don't ask for it: CheckM 1.4
|
|
702
|
+
# removed -f and --tab_table from the workflow commands.
|
|
700
703
|
if checkm_method == 'taxonomy_wf':
|
|
701
|
-
cmd = [check_exe,checkm_method,'domain','Bacteria',genome_folder,checkm_outf,
|
|
702
|
-
|
|
704
|
+
cmd = [check_exe,checkm_method,'domain','Bacteria',genome_folder,checkm_outf,
|
|
705
|
+
'-t',str(t),'-g','-x','faa']
|
|
703
706
|
else:
|
|
704
|
-
cmd = [check_exe,checkm_method,genome_folder,checkm_outf,
|
|
705
|
-
|
|
706
|
-
|
|
707
|
+
cmd = [check_exe,checkm_method,genome_folder,checkm_outf,
|
|
708
|
+
'-t',str(t),'-g','-x','faa']
|
|
709
|
+
# CheckM 1.4 also removed --pplacer_threads (it no longer uses pplacer)
|
|
710
|
+
if _checkm_has_option(check_exe, checkm_method, '--pplacer_threads'):
|
|
711
|
+
cmd += ['--pplacer_threads', str(min(64, int(t)))]
|
|
707
712
|
|
|
708
713
|
logging.debug("Running CheckM with command: {0}".format(' '.join(cmd)))
|
|
709
714
|
|
|
@@ -742,6 +747,25 @@ def run_checkM(genome_folder_whole, checkm_outf_whole, **kwargs):
|
|
|
742
747
|
|
|
743
748
|
return pd.concat(dbs).reset_index(drop=True)
|
|
744
749
|
|
|
750
|
+
def _checkm_has_option(check_exe, command, option):
|
|
751
|
+
'''
|
|
752
|
+
Whether `checkm <command> -h` lists `option`. CheckM's options differ
|
|
753
|
+
between versions (1.4 dropped several), so check instead of assuming.
|
|
754
|
+
'''
|
|
755
|
+
key = (check_exe, command)
|
|
756
|
+
if key not in _CHECKM_HELP:
|
|
757
|
+
try:
|
|
758
|
+
out = subprocess.run([check_exe, command, '-h'], capture_output=True,
|
|
759
|
+
text=True, timeout=120)
|
|
760
|
+
_CHECKM_HELP[key] = out.stdout + out.stderr
|
|
761
|
+
except Exception:
|
|
762
|
+
_CHECKM_HELP[key] = ''
|
|
763
|
+
return option in _CHECKM_HELP[key]
|
|
764
|
+
|
|
765
|
+
|
|
766
|
+
_CHECKM_HELP = {}
|
|
767
|
+
|
|
768
|
+
|
|
745
769
|
def _checkm_get_exe():
|
|
746
770
|
loc, works = drep.d_bonus.find_program('checkm')
|
|
747
771
|
if loc == None:
|
|
@@ -805,7 +829,7 @@ def calc_fasta_length(fasta_loc):
|
|
|
805
829
|
mode = 'r'
|
|
806
830
|
|
|
807
831
|
with open_func(fasta_loc, mode) as handle:
|
|
808
|
-
for line in handle:
|
|
832
|
+
for line in _skip_leading_comments(handle):
|
|
809
833
|
if line.startswith('>'):
|
|
810
834
|
pass
|
|
811
835
|
else:
|
|
@@ -813,6 +837,23 @@ def calc_fasta_length(fasta_loc):
|
|
|
813
837
|
|
|
814
838
|
return total
|
|
815
839
|
|
|
840
|
+
|
|
841
|
+
def _skip_leading_comments(handle):
|
|
842
|
+
'''
|
|
843
|
+
Yield the lines of a FASTA file, leaving out ';' comment lines and blank
|
|
844
|
+
lines before the first record (the original Pearson FASTA comment form,
|
|
845
|
+
also used for FAIR-bioHeaders metadata). Without this they are counted as
|
|
846
|
+
sequence. Lines after the first record are passed through unchanged.
|
|
847
|
+
'''
|
|
848
|
+
lines = iter(handle)
|
|
849
|
+
for line in lines:
|
|
850
|
+
if line.startswith(';') or not line.strip():
|
|
851
|
+
continue
|
|
852
|
+
yield line
|
|
853
|
+
break
|
|
854
|
+
yield from lines
|
|
855
|
+
|
|
856
|
+
|
|
816
857
|
def chdb_to_genomeInfo(chdb):
|
|
817
858
|
'''
|
|
818
859
|
Convert the output of checkM (chdb) into genomeInfo
|
drep-4.0.2/drep/VERSION
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
4.0.2
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|