# 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

            geneIdToFamilyIds = defaultdict(set)
            for line in open(os.path.join(self.genomeDir, genomeId, genomeId + extension)):
                if bHeader:
                    bHeader = False
                    continue

                lineSplit = line.split('\t')

                geneId = lineSplit[0]
                clusterId = lineSplit[clusterIdIndex]

                # IMG may annotate multiple parts of a gene as coming
                # from the same cluster (PFAM, TIGRFAM), but this should
                # only count as 1 gene having this annotation
                if clusterId not in geneIdToFamilyIds[geneId]:
                    geneIdToFamilyIds[geneId].add(clusterId)
                    count[clusterId] = count.get(clusterId, 0) + 1

            for clusterId, c in count.items():
                if clusterId not in table:
                    table[clusterId] = {}
                table[clusterId][genomeId] = c

    def geneCountTable(self, genomeIds):
        table = {}
        self.__readTable(table, genomeIds, self.pfamExtension, 8)
        self.__readTable(table, genomeIds, self.tigrExtension, 6)

        return table


# 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

            geneIdToFamilyIds = defaultdict(set)
            for line in open(os.path.join(self.genomeDir, genomeId, genomeId + extension)):
                if bHeader:
                    bHeader = False
                    continue

                lineSplit = line.split('\t')

                geneId = lineSplit[0]
                clusterId = lineSplit[clusterIdIndex]

                # IMG may annotate multiple parts of a gene as coming
                # from the same cluster (PFAM, TIGRFAM), but this should
                # only count as 1 gene having this annotation
                if clusterId not in geneIdToFamilyIds[geneId]:
                    geneIdToFamilyIds[geneId].add(clusterId)
                    count[clusterId] = count.get(clusterId, 0) + 1

            for clusterId, c in count.items():
                if clusterId not in table:
                    table[clusterId] = {}
                table[clusterId][genomeId] = c

    def geneCountTable(self, genomeIds):
        table = {}
        self.__readTable(table, genomeIds, self.pfamExtension, 8)
        self.__readTable(table, genomeIds, self.tigrExtension, 6)

        return table


    def filterGeneCountTable(self, genomeIds, table, ubiquityThreshold=0.9, singleCopyThreshold=0.9):
