# Path: checkm/util/seqUtils.py
# def readFasta(fastaFile, trimHeader=True):
#     '''Read sequences from FASTA file.'''
#     try:
#         if fastaFile.endswith('.gz'):
#             openFile = gzip.open
#         else:
#             openFile = open
# 
#         seqs = {}
#         for line in openFile(fastaFile):
#             # skip blank lines
#             if not line.strip():
#                 continue
# 
#             if line[0] == '>':
#                 if trimHeader:
#                     seqId = line[1:].split(None, 1)[0]
#                 else:
#                     seqId = line[1:].rstrip()
#                 seqs[seqId] = []
#             else:
#                 seqs[seqId].append(line[0:-1])
# 
#         for seqId, seq in seqs.items():
#             seqs[seqId] = ''.join(seq)
#     except:
#         logger = logging.getLogger('timestamp')
#         logger.error("Failed to process sequence file: {}".format(fastaFile))
#         sys.exit(1)
# 
#     return seqs
# 
# Path: checkm/util/taxonomyUtils.py
# def appendTaxonomyRanks(taxonomy, ranks=7):
# def readTaxonomy(taxonomyFile):
# def __parseTaxon(taxon):
# def LCA(taxonomy1, taxonomy2):


import os
import sys
import logging
from collections import defaultdict
from checkm.util.seqUtils import readFasta
from checkm.util.taxonomyUtils import ranksByLabel

        return familyIdToScaffoldIds

    def precomputeGenomeFamilyScaffolds(self, genomeIds):
        """Cache scaffold of PFAM and TIGRFAM genes in genomes."""

        # This function is intended to speed up functions, such as geneDistTable(),
        # that are called multiple times (typically during simulations)
        self.cachedGenomeFamilyScaffolds = {}
        for genomeId in genomeIds:
            self.cachedGenomeFamilyScaffolds[genomeId] = self.__genomeIdToClusterScaffold(genomeId)

        return self.cachedGenomeFamilyScaffolds

    def familyIdToGeneId(self, filename, clusterIdIndex):
        """Determine gene ids associated with PFAMs or TIGRFAMs."""
        familyIdToGeneId = defaultdict(set)
        with open(filename) as f:
            f.readline()
            for line in f:
                lineSplit = line.split('\t')

                geneId = lineSplit[0]
                familyId = lineSplit[clusterIdIndex]
                familyIdToGeneId[familyId].update([geneId])

        return familyIdToGeneId

    def __genomeSeqLens(self, genomeId):
        """Determine length of contigs/scaffolds comprising genome."""
        genomeFile = os.path.join(self.genomeDir, genomeId, genomeId + '.fna')

# Path: checkm/util/seqUtils.py
# def readFasta(fastaFile, trimHeader=True):
#     '''Read sequences from FASTA file.'''
#     try:
#         if fastaFile.endswith('.gz'):
#             openFile = gzip.open
#         else:
#             openFile = open
# 
#         seqs = {}
#         for line in openFile(fastaFile):
#             # skip blank lines
#             if not line.strip():
#                 continue
# 
#             if line[0] == '>':
#                 if trimHeader:
#                     seqId = line[1:].split(None, 1)[0]
#                 else:
#                     seqId = line[1:].rstrip()
#                 seqs[seqId] = []
#             else:
#                 seqs[seqId].append(line[0:-1])
# 
#         for seqId, seq in seqs.items():
#             seqs[seqId] = ''.join(seq)
#     except:
#         logger = logging.getLogger('timestamp')
#         logger.error("Failed to process sequence file: {}".format(fastaFile))
#         sys.exit(1)
# 
#     return seqs
# 
# Path: checkm/util/taxonomyUtils.py
# def appendTaxonomyRanks(taxonomy, ranks=7):
# def readTaxonomy(taxonomyFile):
# def __parseTaxon(taxon):
# def LCA(taxonomy1, taxonomy2):


# Path: checkm/util/img.py
import os
import sys
import logging
from collections import defaultdict
from checkm.util.seqUtils import readFasta
from checkm.util.taxonomyUtils import ranksByLabel

        return familyIdToScaffoldIds

    def precomputeGenomeFamilyScaffolds(self, genomeIds):
        """Cache scaffold of PFAM and TIGRFAM genes in genomes."""

        # This function is intended to speed up functions, such as geneDistTable(),
        # that are called multiple times (typically during simulations)
        self.cachedGenomeFamilyScaffolds = {}
        for genomeId in genomeIds:
            self.cachedGenomeFamilyScaffolds[genomeId] = self.__genomeIdToClusterScaffold(genomeId)

        return self.cachedGenomeFamilyScaffolds

    def familyIdToGeneId(self, filename, clusterIdIndex):
        """Determine gene ids associated with PFAMs or TIGRFAMs."""
        familyIdToGeneId = defaultdict(set)
        with open(filename) as f:
            f.readline()
            for line in f:
                lineSplit = line.split('\t')

                geneId = lineSplit[0]
                familyId = lineSplit[clusterIdIndex]
                familyIdToGeneId[familyId].update([geneId])

        return familyIdToGeneId

    def __genomeSeqLens(self, genomeId):
        """Determine length of contigs/scaffolds comprising genome."""
        genomeFile = os.path.join(self.genomeDir, genomeId, genomeId + '.fna')

        seqs = readFasta(genomeFile)
