Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion fcat/assessCompleteness.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ def mode2(ppFile, missingGr, coreDir, coreSet, queryID, outDir, fasDiff):
refspecFile = '%s/%s/%s/last_refspec.txt' % (outDir, coreSet, queryID)
for g in fcatFn.readFile(refspecFile):
groupRefspec[g.split('\t')[0]] = g.split('\t')[1]
# get the refspec sequence used as seed for each group (only written by fcat --matchedSeed / --windowFallback)
groupSeed = {}
seedInfoFile = '%s/%s/%s/seed_info.txt' % (outDir, coreSet, queryID)
if os.path.exists(seedInfoFile):
for g in fcatFn.readFile(seedInfoFile)[1:]:
tmp = g.split('\t')
if len(tmp) >= 3:
groupSeed[tmp[0]] = tmp[2]
# validate orthologs
for line in fcatFn.readFile(ppFile):
if len(line) > 0:
Expand All @@ -115,10 +123,18 @@ def mode2(ppFile, missingGr, coreDir, coreSet, queryID, outDir, fasDiff):
if os.path.exists(scoreFile):
meanRefspec = 0
fasRefspec = []
seedCutoff = None
for l in fcatFn.readFile(scoreFile):
if l.split('\t')[0] == groupRefspec[groupID].strip():
fasRefspec.append(float(l.split('\t')[1].strip()))
meanRefspec = round(statistics.mean(fasRefspec), 4)
# cutoff of the refspec sequence that was used as seed for this group
if groupID in groupSeed and len(l.split('\t')) > 2:
if l.split('\t')[2].strip().split('|')[-1] == groupSeed[groupID]:
seedCutoff = float(l.split('\t')[1].strip())
if not seedCutoff == None:
meanRefspec = round(seedCutoff, 4)
else:
meanRefspec = round(statistics.mean(fasRefspec), 4)
if meanFas >= (meanRefspec - fasDiff):
assessment = addToDict(assessment, groupID, line.split('\t')[2], 'similar', meanFas, meanRefspec)
geneCat['similar'].append(line+'\t0')
Expand Down
2 changes: 2 additions & 0 deletions fcat/fcat.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ def main():
optional.add_argument('--noDomain', help='Not output feature domain files', action='store_true', default=False)
optional.add_argument('--keep', help='Keep temporary phyloprofile data', action='store_true', default=False)
optional.add_argument('--bidirectional', help=argparse.SUPPRESS, action='store_true', default=False)
optional.add_argument('--matchedSeed', help='If a reference species has several sequences in a core group: keep the score window and accept a candidate whose best reverse BLAST hit is any of them; that sequence is used as seed (fDOG --matchedSeed) and for the mode 2 cutoff. Without this option fCAT switches the score window off for such groups (--scoreCutoff 100)', action='store_true', default=False)
optional.add_argument('--windowFallback', help='If no hit within the score window passes the reverse search, also check the other top hits (fDOG --windowFallback)', action='store_true', default=False)

args = parser.parse_args()

Expand Down
44 changes: 39 additions & 5 deletions fcat/searchOrtho.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def readRefspecFile(refspecFile):
groupRefspec[line.split('\t')[0]] = line.split('\t')[1]
return(groupRefspec)

def prepareJob(coreDir, coreSet, queryID, refspecList, outDir, coreTaxa_dir, annoDir, annoQuery, force, cpus):
def prepareJob(coreDir, coreSet, queryID, refspecList, outDir, coreTaxa_dir, annoDir, annoQuery, force, cpus, matchedSeed = False, windowFallback = False):
fdogJobs = []
ignored = []
groupRefspec = {}
Expand All @@ -148,7 +148,7 @@ def prepareJob(coreDir, coreSet, queryID, refspecList, outDir, coreTaxa_dir, ann
else:
outPath = '%s/fcatOutput/%s/%s/fdogOutput/%s' % (outDir, coreSet, queryID, refspec)
if not os.path.exists('%s/%s/%s.phyloprofile' % (outPath, groupID, groupID)) or force:
fdogJobs.append([groupFa, groupID, refspec, outPath, coreTaxa_dir, hmmPath, searchTaxa_path, force])
fdogJobs.append([groupFa, groupID, refspec, outPath, coreTaxa_dir, hmmPath, searchTaxa_path, force, matchedSeed, windowFallback])
groupRefspec[groupID] = refspec
else:
sys.exit('No core group found at %s' % (coreDir + '/core_orthologs/' + coreSet))
Expand All @@ -165,19 +165,46 @@ def countRefCore(refSpec, hmmPath, coreGroup):


def runFdog(args):
(seqFile, jobName, refSpec, outPath, coreTaxa_path, hmmPath, searchTaxa_path, force) = args
(seqFile, jobName, refSpec, outPath, coreTaxa_path, hmmPath, searchTaxa_path, force, matchedSeed, windowFallback) = args
fdog = 'fdog.run --seqFile %s --jobName %s --refspec %s --outpath %s --corepath %s --hmmpath %s --searchpath %s --fasOff --reuseCore --cpu 1' % (seqFile, jobName, refSpec, outPath, coreTaxa_path, hmmPath, searchTaxa_path)
if force:
fdog += ' --force'
if countRefCore(refSpec, hmmPath, jobName) > 1:
fdog += ' --scoreCutoff 100' # set a low score cutoff to find more orthologs
if matchedSeed:
# keep fDOG's score window; accept a candidate if its best reverse BLAST hit
# is any of the refspec sequences of this group, and use that one as seed
fdog += ' --matchedSeed'
else:
fdog += ' --scoreCutoff 100' # set a low score cutoff to find more orthologs
if windowFallback:
# check hits below the score window only if no hit within the window is accepted
fdog += ' --windowFallback'
fdog += ' > /dev/null 2>&1'
try:
subprocess.run([fdog], shell=True, check=True)
except Exception as e:
print(f'\033[91mProblem occurred while running fDOG for \'{jobName}\' core group:\033[0m\n{e}\nCommand used:\n{fdog}')


def writeSeedInfo(outDir, coreSet, queryID, groupRefspec):
""" Collect the <groupID>.seed_info.tsv files written by fDOG (--matchedSeed / --windowFallback)
into fcatOutput/<coreSet>/<queryID>/seed_info.txt
Columns: groupID, refspec, seedID (refspec sequence used as seed), seedRank (position of the seed
among the refspec sequences of the group), fallback (1 = ortholog found only below the score window)
"""
seedInfoFile = '%s/fcatOutput/%s/%s/seed_info.txt' % (outDir, coreSet, queryID)
with open(seedInfoFile, 'w') as out:
out.write('groupID\trefspec\tseedID\tseedRank\tfallback\n')
for groupID in sorted(groupRefspec):
refspec = groupRefspec[groupID]
infoFile = '%s/fcatOutput/%s/%s/fdogOutput/%s/%s.seed_info.tsv' % (outDir, coreSet, queryID, refspec, groupID)
if os.path.exists(infoFile):
for line in fcatFn.readFile(infoFile)[1:]:
tmp = line.split('\t')
if len(tmp) == 6 and tmp[1] == queryID and not tmp[3] == 'NA':
out.write('%s\t%s\t%s\t%s\t%s\n' % (groupID, refspec, tmp[3], tmp[4], tmp[5]))
return(seedInfoFile)

def outputMode(outDir, coreSet, queryID, force, approach):
phyloprofileDir = '%s/fcatOutput/%s/%s/phyloprofileOutput' % (outDir, coreSet, queryID)
Path(phyloprofileDir).mkdir(parents=True, exist_ok=True)
Expand Down Expand Up @@ -422,6 +449,8 @@ def searchOrtho(args):
cpus = mp.cpu_count()-1
force = args.force
keep = args.keep
matchedSeed = getattr(args, 'matchedSeed', False)
windowFallback = getattr(args, 'windowFallback', False)

currDir = os.getcwd()

Expand All @@ -444,7 +473,7 @@ def searchOrtho(args):
print('Preparing...')
groupRefspec = {}
if status == 0:
(fdogJobs, ignored, groupRefspec) = prepareJob(coreDir, coreSet, queryID, refspecList, outDir, coreTaxa_dir, annoDir, annoQuery, force, cpus)
(fdogJobs, ignored, groupRefspec) = prepareJob(coreDir, coreSet, queryID, refspecList, outDir, coreTaxa_dir, annoDir, annoQuery, force, cpus, matchedSeed, windowFallback)
print('Searching orthologs...')
os.chdir('%s/fcatOutput/%s/%s' % (outDir, coreSet, queryID))
fdogOut = []
Expand All @@ -470,6 +499,9 @@ def searchOrtho(args):
for g in groupRefspec:
refspecFile.write('%s\t%s\n' % (g, groupRefspec[g]))
refspecFile.close()
# write the refspec sequence used as seed for each group (and whether it was found by the fallback)
if (matchedSeed or windowFallback) and len(groupRefspec) > 0:
writeSeedInfo(outDir, coreSet, queryID, groupRefspec)

# if not status == 2:
# Calculating FAS scores between query orthologs and all sequences in each core group
Expand Down Expand Up @@ -520,6 +552,8 @@ def main():
optional.add_argument('--cpus', help='Number of CPUs used for annotation. Default = 4', action='store', default=4, type=int)
optional.add_argument('--force', help='Force overwrite existing data', action='store_true', default=False)
optional.add_argument('--keep', help='Keep temporary phyloprofile data', action='store_true', default=False)
optional.add_argument('--matchedSeed', help='If a reference species has several sequences in a core group: keep the score window and accept a candidate whose best reverse BLAST hit is any of them; that sequence is used as seed (fDOG --matchedSeed). Without this option fCAT switches the score window off for such groups (--scoreCutoff 100)', action='store_true', default=False)
optional.add_argument('--windowFallback', help='If no hit within the score window passes the reverse search, also check the other top hits (fDOG --windowFallback)', action='store_true', default=False)
args = parser.parse_args()

start = time.time()
Expand Down