Skip to content

Commit 540b03e

Browse files
authored
Merge pull request #151 from SynBioDex/150-clustering-not-running
Updated run_uclust to select arguments based on whether search or usearch is used and stop indexing if cluster step fails.
2 parents 3c325f9 + 8c43a68 commit 540b03e

1 file changed

Lines changed: 22 additions & 5 deletions

File tree

flask/cluster.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from xml.etree import ElementTree
2+
import os
23
import subprocess
34
from configManager import ConfigManager
45
from logger import Logger
@@ -46,10 +47,26 @@ def write_fasta(sequences):
4647
f.write(f">{sequence['subject']}\n{sequence['sequence']}\n")
4748

4849
def run_uclust():
49-
args = [usearch_binary_filename, '-cluster_fast', sequences_filename, '-id', uclust_identity, '-sort', 'length', '-uc', uclust_results_filename]
50-
# result = subprocess.run(args, capture_output=True, text=True) # Python3.7
51-
result = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
52-
logger_.log(result.stdout, True)
50+
if whichSearch == 'vsearch':
51+
args = [usearch_binary_filename, '--cluster_fast', sequences_filename, '--id', uclust_identity, '--uc',
52+
uclust_results_filename]
53+
else:
54+
args = [usearch_binary_filename, '-cluster_fast', sequences_filename, '-id', uclust_identity, '-sort', 'length',
55+
'-uc', uclust_results_filename]
56+
logger_.log(f'******** Running {whichSearch} clustering with identity {uclust_identity} ********', True)
57+
proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
58+
for line in proc.stderr:
59+
line = line.strip()
60+
if line:
61+
logger_.log(line, True)
62+
proc.wait()
63+
if proc.returncode != 0:
64+
logger_.log(f'******** ERROR: {whichSearch} clustering failed with exit code {proc.returncode} ********', True)
65+
raise RuntimeError(f'clustering ({whichSearch}) failed (exit {proc.returncode}) — see indexing log for details')
66+
if not os.path.exists(uclust_results_filename) or os.path.getsize(uclust_results_filename) == 0:
67+
raise RuntimeError(f'{whichSearch} clustering produced no output — {uclust_results_filename} is missing or empty')
68+
logger_.log('******** Clustering completed successfully ********', True)
69+
5370

5471
def analyze_uclust():
5572
total_parts = 0
@@ -82,7 +99,7 @@ def uclust2uris(fileName):
8299

83100
def uclust2clusters():
84101
cluster2parts = {}
85-
102+
86103
with open(uclust_results_filename, 'r') as f:
87104
for line in f:
88105
parts = line.split()

0 commit comments

Comments
 (0)