From cab18a40e34a4367b7c8b1f3c8ec467eea00302f Mon Sep 17 00:00:00 2001 From: Krishnasai <93254061+Krishnachowdary711@users.noreply.github.com> Date: Thu, 12 Nov 2026 12:13:33 +0100 Subject: [PATCH] new feature to choose seed protein and a candidate is accepted if its best reverse blast hit is any of the reference species's genes in the group and that match becomes the seed --- fdog/libs/hmm.py | 25 ++++ fdog/libs/orthosearch.py | 248 +++++++++++++++++++++++++++------------ fdog/libs/output.py | 28 +++++ fdog/libs/preparation.py | 18 +++ fdog/runMulti.py | 29 ++++- fdog/runSingle.py | 13 +- 6 files changed, 282 insertions(+), 79 deletions(-) diff --git a/fdog/libs/hmm.py b/fdog/libs/hmm.py index 120a2a4..9b34e57 100644 --- a/fdog/libs/hmm.py +++ b/fdog/libs/hmm.py @@ -128,3 +128,28 @@ def do_hmmsearch( % (hmm_file, search_fa, e)) hmm_hits = {k.decode() if isinstance(k, bytes) else k: v for k, v in hmm_hits.items()} return(hmm_hits) + + +def do_hmmsearch_with_reserve( + hmm_file, search_fa, evalHmmer = 0.00001, scoreCutoff = 10, + hitLimit = 10, hmm_score_type = 'domain', cpus = os.cpu_count(), debug = False): + """ Same search as do_hmmsearch, but return two dictionaries {hit: score}: + window_hits: the hits within the score window, i.e. score >= + max_score / 100 * (100 - scoreCutoff); these are exactly the + hits that do_hmmsearch returns + reserve_hits: the other hits among the hitLimit best hits; they are + checked only if no hit within the window is accepted + (used if --windowFallback is specified) + Both are sorted by decreasing score. The pHMM search is run only once. + """ + all_hits = do_hmmsearch( + hmm_file, search_fa, evalHmmer, 100, hitLimit, hmm_score_type, cpus, debug) + if len(all_hits) == 0: + return({}, {}) + best_score = max(all_hits.values()) + cutoff = best_score/100*(100-scoreCutoff) + if best_score < 0: + cutoff = best_score/100*(100+scoreCutoff) + window_hits = {k: v for k, v in all_hits.items() if v >= cutoff} + reserve_hits = {k: v for k, v in all_hits.items() if v < cutoff} + return(window_hits, reserve_hits) diff --git a/fdog/libs/orthosearch.py b/fdog/libs/orthosearch.py index 6bb7a0c..4388b58 100644 --- a/fdog/libs/orthosearch.py +++ b/fdog/libs/orthosearch.py @@ -30,6 +30,76 @@ import fdog.libs.output as output_fn +##### REVERSE SEARCH OF ONE pHMM HIT (step 4 of HaMStR) ##### +def reverse_check(hmm_hit, seed_ids, seqName, search_taxon, outpath, + refspec_db, refspec_seqs, search_seqs, + evalBlast, lowComplexityFilter, checkCoorthologsRefOff, + aligner, cpus, debug, silentOff, noCleanup): + """ Reverse BLAST search of one pHMM hit against the reference proteome + seed_ids: reference sequences that may serve as seed, in order of preference. + Default (one seed): [first reference sequence of the core group]. + With --matchedSeed: all reference sequences of the core group. + Return the seed ID the hit was accepted with, or None if it was rejected: + (4a) the best BLAST hit is one of the seeds -> accepted with that seed + (4b) otherwise (co-ortholog test): accepted with the first seed for which the + best BLAST hit is closer to the seed than to the hmm hit + """ + hmm_hit_fa = '%s/hmm_%s_%s_%s.fa' % ( + outpath, seqName, search_taxon, hmm_hit) + with open(hmm_hit_fa, 'w') as hmm_fa_out: + hmm_fa_out.write('>%s_%s\n%s' % (search_taxon, hmm_hit, search_seqs.fetch(hmm_hit))) + blast_xml = blast_fn.do_blastsearch( + hmm_hit_fa, refspec_db, evalBlast = evalBlast, lowComplexityFilter = lowComplexityFilter, cpus = cpus) + blast_out = blast_fn.parse_blast_xml(blast_xml) + output_fn.print_debug(debug, 'BLAST hits', blast_out) + if noCleanup == False: + os.remove(hmm_hit_fa) + if not len(blast_out['hits'].keys()) > 0: + return(None) + best_blast_hit = list(blast_out['hits'].keys())[0] + if best_blast_hit == hmm_hit and len(blast_out['hits'].keys()) > 1: + best_blast_hit = list(blast_out['hits'].keys())[0] + ### (4a) if refspec_seq_id == best blast hit + if best_blast_hit in seed_ids: + output_fn.print_stdout( + silentOff, + '%s accepted (best blast hit is ref)' % (blast_out['query'])) + return(best_blast_hit) + ### (4b) else, check for co-ortholog ref + if checkCoorthologsRefOff == False: + for seed_id in seed_ids: + aln_fa = '%s/blast_%s_%s_%s_%s_%s.fa' % ( + outpath, seqName, seed_id, search_taxon, + hmm_hit, best_blast_hit) + with open(aln_fa, 'w') as aln_fa_out: + aln_fa_out.write( + '>%s\n%s\n>%s_%s\n%s\n>%s\n%s' % ( + seed_id, refspec_seqs.fetch(seed_id), + search_taxon, hmm_hit, search_seqs.fetch(hmm_hit), + best_blast_hit, refspec_seqs.fetch(best_blast_hit) + ) + ) + fasta_fn.remove_dup(aln_fa) + aln_seq = align_fn.do_align(aligner, aln_fa) + output_fn.print_debug( + debug, 'Alignment for checking co-ortholog ref', aln_seq) + br_dist = align_fn.calc_Kimura_dist(aln_seq, best_blast_hit, seed_id, debug) + bh_dist = align_fn.calc_Kimura_dist(aln_seq, best_blast_hit, f'{search_taxon}_{hmm_hit}', debug) + output_fn.print_debug( + debug, 'Check if distance blast_vs_ref < blast_vs_hmm', + 'd_br = %s; d_bh = %s' % (br_dist, bh_dist)) + if noCleanup == False: + os.remove(aln_fa) + if br_dist == bh_dist == 0 or br_dist < bh_dist: + output_fn.print_stdout( + silentOff, + '%s accepted (best blast hit is co-ortholog to ref)' + % (blast_out['query']) + ) + return(seed_id) + return(None) + + ##### FUNCTION FOR HMM-BASED ORTHOLOG SEARCH (HaMStR) ##### def hamstr(args): (seqName, hmmpath, corepath, searchpath, outpath, @@ -37,13 +107,23 @@ def hamstr(args): evalHmmer, hitLimit, hmmScoreType, scoreCutoff, evalBlast, lowComplexityFilter, checkCoorthologsRefOff, rbh, rep, - aligner, cpus, debug, silentOff, noCleanup) = args + aligner, cpus, debug, silentOff, noCleanup, + seed_ids, windowFallback) = args """ Ortholog search algorithm for a hmm core group agains a search taxon Implemented based on HaMStR https://doi.org/10.1186/1471-2148-9-157 + seed_ids: reference sequences that may serve as seed ([seed_id] by default, + all reference sequences of the core group with --matchedSeed) + windowFallback: if no hit within the score window is accepted, check the + other hits among the hitLimit best hits (--windowFallback) + Return (ortho_final, seed_info). seed_info records which seed was used for + the reported ortholog(s), its position in seed_ids, and whether it was + found only by the fallback. """ ### (0) Dict for storing candidate and final orthologs (key=id, value=seq) ortho_candi = {} + candi_seed = {} # seed each accepted candidate was accepted with (key=id, value=seed id) ortho_final = {} + seed_info = {'search_taxon': search_taxon, 'seed': None, 'seed_rank': None, 'fallback': False} ### (00) Parse input files hmm_file = '%s/%s/hmm_dir/%s.hmm' % (hmmpath, seqName, seqName) refspec_db = '%s/%s/%s' % (corepath, refspec, refspec) @@ -59,7 +139,7 @@ def hamstr(args): silentOff, '\n### Ortholog search ###' + '\nSeed: %s\nRefspec: %s\n' % (seqName, refspec) - + 'Ref_seqID: %s\n' % seed_id + + 'Ref_seqID: %s\n' % ','.join(seed_ids) + 'Search taxon: %s' % search_taxon) output_fn.print_debug( debug, 'Parameters', @@ -68,11 +148,17 @@ def hamstr(args): + 'BLAST evalue cutoff: %s\n' % evalBlast + 'Blast low complexity filter: %s\n' % lowComplexityFilter + 'Turn off check for co-orthologs ref: %s\n' % checkCoorthologsRefOff + + 'Window fallback: %s\n' % windowFallback + 'Aligner: %s' % aligner) ### (1) Do hmmsearch for query hmm against search taxon fasta - hmm_hits = hmm_fn.do_hmmsearch( + if windowFallback == True: + (hmm_hits, reserve_hits) = hmm_fn.do_hmmsearch_with_reserve( + hmm_file, search_fa, evalHmmer, scoreCutoff, hitLimit, hmmScoreType, cpus, debug) + else: + hmm_hits = hmm_fn.do_hmmsearch( hmm_file, search_fa, evalHmmer, scoreCutoff, hitLimit, hmmScoreType, cpus, debug) + reserve_hits = {} output_fn.print_debug(debug, 'Sorted HMM hits', hmm_hits) ### (2) Read fasta file of refspec and search taxon refspec_seqs = fasta_fn.read_fasta(refspec_fa) @@ -82,75 +168,50 @@ def hamstr(args): output_fn.print_stdout( silentOff, 'WARNING: No HMM hit found!') else: - for hmm_hit in hmm_hits: - hmm_hit_fa = '%s/hmm_%s_%s_%s.fa' % ( - outpath, seqName, search_taxon, hmm_hit) - with open(hmm_hit_fa, 'w') as hmm_fa_out: - hmm_fa_out.write('>%s_%s\n%s' % (search_taxon, hmm_hit, search_seqs.fetch(hmm_hit))) - blast_xml = blast_fn.do_blastsearch( - hmm_hit_fa, refspec_db, evalBlast = evalBlast, lowComplexityFilter = lowComplexityFilter, cpus = cpus) - blast_out = blast_fn.parse_blast_xml(blast_xml) - output_fn.print_debug(debug, 'BLAST hits', blast_out) - if noCleanup == False: - os.remove(hmm_hit_fa) - ### (4) check reciprocity - ### (4a) if refspec_seq_id == best blast hit - if len(blast_out['hits'].keys()) > 0: - best_blast_hit = list(blast_out['hits'].keys())[0] - if best_blast_hit == hmm_hit and len(blast_out['hits'].keys()) > 1: - best_blast_hit = list(blast_out['hits'].keys())[0] - if seed_id == best_blast_hit: - output_fn.print_stdout( - silentOff, - '%s accepted (best blast hit is ref)' % (blast_out['query'])) + ### (4) check reciprocity; first for the hits within the score window, + ### then (only with --windowFallback, and only if nothing was accepted) + ### for the other hits among the hitLimit best hits + for phase, phase_hits in enumerate([hmm_hits, reserve_hits]): + if phase == 1: + if len(ortho_candi) > 0 or len(phase_hits) == 0: + break + seed_info['fallback'] = True + output_fn.print_stdout( + silentOff, + 'No hit within the score window accepted. Checking %s further hit(s)...' + % len(phase_hits)) + output_fn.print_debug(debug, 'Further HMM hits (fallback)', phase_hits) + for hmm_hit in phase_hits: + accepted_seed = reverse_check( + hmm_hit, seed_ids, seqName, search_taxon, outpath, + refspec_db, refspec_seqs, search_seqs, + evalBlast, lowComplexityFilter, checkCoorthologsRefOff, + aligner, cpus, debug, silentOff, noCleanup) + if accepted_seed: ortho_candi[hmm_hit] = search_seqs.fetch(hmm_hit) - continue - else: - ### (4b) else, check for co-ortholog ref - if checkCoorthologsRefOff == False: - aln_fa = '%s/blast_%s_%s_%s_%s_%s.fa' % ( - outpath, seqName, seed_id, search_taxon, - hmm_hit, best_blast_hit) - with open(aln_fa, 'w') as aln_fa_out: - aln_fa_out.write( - '>%s\n%s\n>%s_%s\n%s\n>%s\n%s' % ( - seed_id, refspec_seqs.fetch(seed_id), - search_taxon, hmm_hit, search_seqs.fetch(hmm_hit), - best_blast_hit, refspec_seqs.fetch(best_blast_hit) - ) - ) - fasta_fn.remove_dup(aln_fa) - aln_seq = align_fn.do_align(aligner, aln_fa) - output_fn.print_debug( - debug, 'Alignment for checking co-ortholog ref', aln_seq) - br_dist = align_fn.calc_Kimura_dist(aln_seq, best_blast_hit, seed_id, debug) - bh_dist = align_fn.calc_Kimura_dist(aln_seq, best_blast_hit, f'{search_taxon}_{hmm_hit}', debug) - output_fn.print_debug( - debug, 'Check if distance blast_vs_ref < blast_vs_hmm', - 'd_br = %s; d_bh = %s' % (br_dist, bh_dist)) - if noCleanup == False: - os.remove(aln_fa) - if br_dist == bh_dist == 0 or br_dist < bh_dist: - output_fn.print_stdout( - silentOff, - '%s accepted (best blast hit is co-ortholog to ref)' - % (blast_out['query']) - ) - ortho_candi[hmm_hit] = search_seqs.fetch(hmm_hit) - continue + candi_seed[hmm_hit] = accepted_seed + # remove seed protein from candidate list + # (a reference sequence that was accepted with itself as seed) + if search_taxon == refspec: + for cand in list(ortho_candi.keys()): + if cand == candi_seed[cand]: + ortho_candi.pop(cand) + candi_seed.pop(cand) - # remove seed protein from candidate list - if search_taxon == refspec: - if seed_id in ortho_candi: - ortho_candi.pop(seed_id) ### (5) check co-ortholog if more than 1 HMM hits are accepted if len(ortho_candi) == 0: + seed_info['fallback'] = False output_fn.print_stdout( silentOff, 'WARNING: Reciprocity not fulfulled! No ortholog found!') else: output_fn.print_debug( debug, 'Candidates for checking co-orthologs', ortho_candi.keys()) best_ortho = list(ortho_candi.keys())[0] + # the seed for step 5 (and for the output) is the seed that the + # best ortholog was accepted with + seed_used = candi_seed[best_ortho] + seed_info['seed'] = seed_used + seed_info['seed_rank'] = seed_ids.index(seed_used) + 1 ortho_final = fasta_fn.add_seq_to_dict( ortho_final, '%s|%s|%s|1' % (seqName, search_taxon, best_ortho), ortho_candi[best_ortho]) @@ -161,7 +222,7 @@ def hamstr(args): outpath, seqName, search_taxon) with open(aln_co_fa, 'w') as aln_co_fa_out: aln_co_fa_out.write(('>%s\n%s\n') % - (seed_id, refspec_seqs.fetch(seed_id))) + (seed_used, refspec_seqs.fetch(seed_used))) for cand in ortho_candi: aln_co_fa_out.write(('>%s\n%s\n') % (f'{search_taxon}_{cand}', ortho_candi[cand])) @@ -171,7 +232,7 @@ def hamstr(args): if noCleanup == False: os.remove(aln_co_fa) best_dist = align_fn.calc_Kimura_dist( - aln_co_seq, seed_id, f'{search_taxon}_{best_ortho}', debug) + aln_co_seq, seed_used, f'{search_taxon}_{best_ortho}', debug) for cand in ortho_candi: if not cand == best_ortho: candi_dist = align_fn.calc_Kimura_dist( @@ -183,7 +244,7 @@ def hamstr(args): 'd_best = %s; d_other = %s' % (best_dist, candi_dist)) if candi_dist < best_dist: - if not cand == seed_id: + if not cand == seed_used: ortho_final = fasta_fn.add_seq_to_dict( ortho_final, '%s|%s|%s|0' \ @@ -193,11 +254,19 @@ def hamstr(args): silentOff, '=> %s orthologs found: %s' % (len(ortho_final), list(ortho_final.keys()))) - return(ortho_final) + return(ortho_final, seed_info) -def run_hamstr(args): - """ Perform ortholog search based on hamstr approach """ +def run_hamstr(args, matchedSeed = False, windowFallback = False, return_seed_info = False): + """ Perform ortholog search based on hamstr approach + matchedSeed: accept a candidate whose best reverse BLAST hit is any of the + reference sequences in the core group; that sequence is then the seed + (only relevant if the reference species has several sequences in the + core group, i.e. with --reuseCore) + windowFallback: if no hit within the score window is accepted, check the + other hits among the hitLimit best hits + return_seed_info: also return a list with one seed_info dict per search taxon + """ (seqName, refspec, pathArgs, orthoArgs, otherArgs) = args (outpath, hmmpath, corepath, searchpath, annopath) = pathArgs @@ -209,6 +278,10 @@ def run_hamstr(args): ### get ref seqID core_fa = '%s/%s/%s.fa' % (hmmpath, seqName, seqName) seed_id = prepare_fn.get_seed_id_from_fa(core_fa, refspec) + if matchedSeed == True: + seed_ids = prepare_fn.get_seed_ids_from_fa(core_fa, refspec) + else: + seed_ids = [seed_id] ### get search taxa from user defined list (as a file or directly a list) if not searchTaxa == '': @@ -228,7 +301,8 @@ def run_hamstr(args): evalHmmer, hitLimit, hmmScoreType, scoreCutoff, evalBlast, lowComplexityFilter, checkCoorthologsRefOff, rbh, rep, - aligner, cpus, debug, silentOff, noCleanup + aligner, cpus, debug, silentOff, noCleanup, + seed_ids, windowFallback ]) else: ignored_taxa.append(search_taxon) @@ -248,11 +322,13 @@ def run_hamstr(args): evalHmmer, hitLimit, hmmScoreType, scoreCutoff, evalBlast, lowComplexityFilter, checkCoorthologsRefOff, rbh, rep, - aligner, cpus, debug, silentOff, noCleanup + aligner, cpus, debug, silentOff, noCleanup, + seed_ids, windowFallback ]) ### do ortholog search hamstr_out = {} + seed_infos = [] if len(hamstr_jobs) > 0: if cpus > len(hamstr_jobs): cpus = len(hamstr_jobs) @@ -260,23 +336,45 @@ def run_hamstr(args): silentOff, 'Ortholog search for %s taxa...' % len(hamstr_jobs)) if debug == True or silentOff == True or len(hamstr_jobs) == 1: for job in hamstr_jobs: - tmp_out = hamstr(job) + (tmp_out, tmp_info) = hamstr(job) hamstr_out = {**hamstr_out, **tmp_out} + seed_infos.append(tmp_info) else: pool = mp.Pool(cpus) for _ in tqdm( pool.imap_unordered(hamstr, hamstr_jobs), total=len(hamstr_jobs)): - if len(_) > 0: - hamstr_out = {**hamstr_out, **_} + (tmp_out, tmp_info) = _ + if len(tmp_out) > 0: + hamstr_out = {**hamstr_out, **tmp_out} + seed_infos.append(tmp_info) ### Get seed seq + # the seed written into the output (and used by FAS as the reference) is the + # seed that the orthologs were accepted with. By default this is always the + # first reference sequence. If different search taxa were accepted with + # different seeds (only possible with --matchedSeed), the first reference + # sequence is written and a warning is printed. + used_seeds = [] + for info in seed_infos: + if info['seed'] and not info['seed'] in used_seeds: + used_seeds.append(info['seed']) + out_seed = seed_id + if len(used_seeds) == 1: + out_seed = used_seeds[0] + elif len(used_seeds) > 1: + print( + 'WARNING: Search taxa of %s were accepted with different reference sequences (%s). ' + 'The output lists %s as seed sequence. See %s.seed_info.tsv for the seed of each taxon.' + % (seqName, ','.join(used_seeds), seed_id, seqName)) refspec_fa = '%s/%s/%s.fa' % (corepath, refspec, refspec) refspec_seqs = fasta_fn.read_fasta(refspec_fa) - seed_id_mod = '%s|%s|%s|1' % (seqName, refspec, seed_id) - seed_seq = refspec_seqs.fetch(seed_id) + seed_id_mod = '%s|%s|%s|1' % (seqName, refspec, out_seed) + seed_seq = refspec_seqs.fetch(out_seed) ### return + if return_seed_info == True: + return({**{seed_id_mod:seed_seq}, **hamstr_out}, seed_infos) return({**{seed_id_mod:seed_seq}, **hamstr_out}) diff --git a/fdog/libs/output.py b/fdog/libs/output.py index 1cf9bef..7efaa20 100644 --- a/fdog/libs/output.py +++ b/fdog/libs/output.py @@ -80,6 +80,34 @@ def write_hamstr(hamstr_result, outpath, seqName, force, append): 'Found %s ortholog(s)!\nOutput file: %s' % (ortho_count, outfile)) +def write_seed_info(seed_infos, outpath, seqName, refspec, append): + """ Write which reference sequence was used as seed for each search taxon + into seqName.seed_info.tsv (written if --matchedSeed or --windowFallback is used) + Columns: geneID, searchTaxon, refspec, seedID, seedRank, fallback + seedRank: position of the seed among the reference sequences in the core + group fasta file (1 = first); NA if no ortholog was found + fallback: 1 if the ortholog was found only among the hits below the + score window (--windowFallback), otherwise 0 + """ + outfile = os.path.abspath('%s/%s.seed_info.tsv' % (outpath, seqName)) + old_lines = [] + if append == True and os.path.exists(outfile): + with open(outfile) as f: + old_lines = [l for l in f.read().split('\n')[1:] if len(l) > 0] + new_taxa = [info['search_taxon'] for info in seed_infos] + with open(outfile, 'w') as out_file: + out_file.write('geneID\tsearchTaxon\trefspec\tseedID\tseedRank\tfallback\n') + for line in old_lines: + if not line.split('\t')[1] in new_taxa: + out_file.write(line + '\n') + for info in seed_infos: + seed = info['seed'] if info['seed'] else 'NA' + rank = info['seed_rank'] if info['seed_rank'] else 'NA' + out_file.write('%s\t%s\t%s\t%s\t%s\t%s\n' % ( + seqName, info['search_taxon'], refspec, seed, rank, int(info['fallback']))) + return(outfile) + + def hamstr_2_profile(fa_file): """ Convert extended.fa file into phyloprofile file """ if os.path.exists(fa_file): diff --git a/fdog/libs/preparation.py b/fdog/libs/preparation.py index bc86189..7d7fdd6 100644 --- a/fdog/libs/preparation.py +++ b/fdog/libs/preparation.py @@ -165,6 +165,24 @@ def get_seed_id_from_fa(core_fa, refspec): return(seed_id) +def get_seed_ids_from_fa(core_fa, refspec): + """ Get the IDs of ALL sequences of the reference species in the core + ortholog fasta file, in the order in which they appear in the file + (used if --matchedSeed is specified together with --reuseCore). + The order of the file is the order in which the genes are tried as seed + in the co-ortholog test (step 4b), i.e. the preferred gene comes first. + """ + seed_ids = [] + for s in SeqIO.parse(open(core_fa), 'fasta'): + tmp = s.id.split('|') + if len(tmp) > 2 and tmp[1] == refspec: + seed_ids.append(tmp[-1]) + if len(seed_ids) == 0: + # fall back to the matching rule of get_seed_id_from_fa + seed_ids = [get_seed_id_from_fa(core_fa, refspec)] + return(seed_ids) + + def identify_seed_id(seqFile, refspec, corepath, debug, silentOff): """ Identify seed ID in reference protein set using BLAST If the header of the seed fasta seq if found in the refspec proteome, then diff --git a/fdog/runMulti.py b/fdog/runMulti.py index 8d90d31..5d6fff4 100644 --- a/fdog/runMulti.py +++ b/fdog/runMulti.py @@ -113,7 +113,7 @@ def compile_core(core_options, other_options, seeds, inFol, cpus, outpath, silen def search_ortholog(options, seeds, inFol, outpath): - (orthoArgs, otherArgs, pathArgs, refspec) = options + (orthoArgs, otherArgs, pathArgs, refspec, matchedSeed, windowFallback) = options (searchTaxa, cpus, debug, silentOff, noCleanup, force, append) = otherArgs ortho_runtime = [] n = 1 @@ -123,8 +123,12 @@ def search_ortholog(options, seeds, inFol, outpath): seqName = get_seed_name(seed) print('... %s (%s / %s)' % (seqName, n, len(seeds))) if not os.path.exists('%s/%s.extended.fa' % (outpath, seqName)) or force == True: - hamstr_out = ortho_fn.run_hamstr([seqName, refspec, pathArgs, orthoArgs, otherArgs]) + (hamstr_out, seed_infos) = ortho_fn.run_hamstr( + [seqName, refspec, pathArgs, orthoArgs, otherArgs], + matchedSeed = matchedSeed, windowFallback = windowFallback, return_seed_info = True) output_fn.write_hamstr(hamstr_out, outpath, seqName, force, append) + if matchedSeed or windowFallback: + output_fn.write_seed_info(seed_infos, outpath, seqName, refspec, append) end = time.time() ortho_runtime.append('%s\t%s' % (seqName, '{:5.3f}s'.format(end - begin))) n += 1 @@ -150,6 +154,19 @@ def join_outputs(outpath, jobName, seeds, keep, silentOff): os.remove(outpath + '/' + seqName + '.fa') if os.path.exists(os.getcwd() + '/' + seqName + '.fa'): os.remove(os.getcwd() + '/' + seqName + '.fa') + # merge single seed_info.tsv files (written with --matchedSeed or --windowFallback) + seed_info_files = ['%s/%s.seed_info.tsv' % (outpath, get_seed_name(seed)) for seed in seeds] + seed_info_files = [f for f in seed_info_files if os.path.exists(f)] + if len(seed_info_files) > 0: + with open('%s/%s.seed_info.tsv' % (outpath, jobName), 'w') as seed_info_out: + seed_info_out.write('geneID\tsearchTaxon\trefspec\tseedID\tseedRank\tfallback\n') + for f in seed_info_files: + with open(f) as fd: + seed_info_out.write(''.join(fd.readlines()[1:])) + if not os.path.exists('%s/%s' % (single_output_fol, os.path.basename(f))): + shutil.move(f, single_output_fol) + else: + os.remove(f) if keep == True: try: print('Compressing single outputs...') @@ -226,6 +243,10 @@ def main(): action='store', choices=['domain','sequence'], default='domain') ortho_options.add_argument('--scoreCutoff', help='Define the percent range of the hmms core of the best hit up to which a candidate of the hmmsearch will be subjected for further evaluation. Default: 10', action='store', default=10, type=int) + ortho_options.add_argument('--matchedSeed', help='Use with --reuseCore when the reference species has several sequences in the core group: a candidate is accepted if its best reverse BLAST hit is any of them, and that sequence is used as seed for the co-ortholog checks and in the output. The co-ortholog check (4b) tries the sequences in the order of the core group fasta file', + action='store_true', default=False) + ortho_options.add_argument('--windowFallback', help='If no hit within the score window (--scoreCutoff) passes the reverse search, also check the other hits among the --hitLimit best hits', + action='store_true', default=False) fas_options = parser.add_argument_group('FAS options') fas_options.add_argument('--coreFilter', @@ -301,6 +322,8 @@ def main(): hitLimit = args.hitLimit hmmScoreType = args.hmmScoreType scoreCutoff = args.scoreCutoff + matchedSeed = args.matchedSeed + windowFallback = args.windowFallback # fas arguments fasOff = args.fasOff @@ -427,7 +450,7 @@ def main(): orthoArgs = [checkCoorthologsRefOff, rbh, rep, evalBlast, lowComplexityFilter, evalHmmer, hitLimit, hmmScoreType, scoreCutoff, aligner] otherArgs = [searchTaxa, cpus, debug, silentOff, noCleanup, force, append] - ortho_options = [orthoArgs, otherArgs, pathArgs, refspec] + ortho_options = [orthoArgs, otherArgs, pathArgs, refspec, matchedSeed, windowFallback] ortho_runtime = search_ortholog(ortho_options, seeds, inFol, outpath) end = time.time() multi_ortho_time = '{:5.3f}'.format(end-start) diff --git a/fdog/runSingle.py b/fdog/runSingle.py index 2867835..82f4f4e 100644 --- a/fdog/runSingle.py +++ b/fdog/runSingle.py @@ -99,6 +99,10 @@ def main(): action='store', choices=['domain','sequence'], default='domain') ortho_options.add_argument('--scoreCutoff', help='Define the percent range of the hmms core of the best hit up to which a candidate of the hmmsearch will be subjected for further evaluation. Default: 10', action='store', default=10, type=int) + ortho_options.add_argument('--matchedSeed', help='Use with --reuseCore when the reference species has several sequences in the core group: a candidate is accepted if its best reverse BLAST hit is any of them, and that sequence is used as seed for the co-ortholog checks and in the output. The co-ortholog check (4b) tries the sequences in the order of the core group fasta file', + action='store_true', default=False) + ortho_options.add_argument('--windowFallback', help='If no hit within the score window (--scoreCutoff) passes the reverse search, also check the other hits among the --hitLimit best hits', + action='store_true', default=False) fas_options = parser.add_argument_group('FAS options') fas_options.add_argument('--coreFilter', @@ -173,6 +177,8 @@ def main(): hitLimit = args.hitLimit hmmScoreType = args.hmmScoreType scoreCutoff = args.scoreCutoff + matchedSeed = args.matchedSeed + windowFallback = args.windowFallback # fas arguments fasOff = args.fasOff @@ -285,9 +291,14 @@ def main(): orthoArgs = [checkCoorthologsRefOff, rbh, rep, evalBlast, lowComplexityFilter, evalHmmer, hitLimit, hmmScoreType, scoreCutoff, aligner] otherArgs = [searchTaxa, cpus, debug, silentOff, noCleanup, force, append] - hamstr_out = ortho_fn.run_hamstr([seqName, refspec, pathArgs, orthoArgs, otherArgs]) + (hamstr_out, seed_infos) = ortho_fn.run_hamstr( + [seqName, refspec, pathArgs, orthoArgs, otherArgs], + matchedSeed = matchedSeed, windowFallback = windowFallback, return_seed_info = True) output_fn.write_hamstr(hamstr_out, outpath, seqName, force, append) outputFiles = [f'{seqName}.extended.fa'] + if matchedSeed or windowFallback: + output_fn.write_seed_info(seed_infos, outpath, seqName, refspec, append) + outputFiles.append(f'{seqName}.seed_info.tsv') end = time.time() print('==> Ortholog search finished in ' + '{:5.3f}s'.format(end - start))