# Path: picrust/predict_metagenomes.py
# def get_overlapping_ids(otu_table,genome_table,genome_table_ids="sample",\
#   otu_table_ids="observation"):
#     """Get the ids that overlap between the OTU and genome tables
#     otu_table - a BIOM Table object representing the OTU table
#     genome_table - a BIOM Table object for the genomes
#     genome_table_ids - specifies whether the ids of interest are sample or observation
#       in the genome table.  Useful because metagenome tables are often represented with genes
#       as observations.
#     """
#     for id_type in [genome_table_ids,otu_table_ids]:
#         if id_type not in ["sample", "observation"]:
#             raise ValueError(\
#               "%s is not a valid id type. Choices are 'sample' or 'observation'" % id_type)
#     overlapping_otus = list(set(otu_table.ids(axis=otu_table_ids)) &
#                             set(genome_table.ids(axis=genome_table_ids)))
# 
#     if len(overlapping_otus) < 1:
#         raise ValueError,\
#          "No common OTUs between the otu table and the genome table, so can't predict metagenome."
#     return overlapping_otus
# 
# def extract_otu_and_genome_data(otu_table, genome_table,
#                                 genome_table_ids="sample",
#                                 otu_table_ids="observation"):
#     """Return lists of otu,genome data, and overlapping genome/otu ids
# 
#     otu_table -- biom Table object for the OTUs
#     genome_table -- biom Table object for the genomes
#     """
#     overlapping_otus = get_overlapping_ids(otu_table, genome_table,
#         genome_table_ids=genome_table_ids,otu_table_ids=otu_table_ids)
#     # create lists to contain filtered data - we're going to need the data in
#     # numpy arrays, so it makes sense to compute this way rather than filtering
#     # the tables
#     otu_data = []
#     genome_data = []
# 
#     # build lists of filtered data
#     for obs_id in overlapping_otus:
#         if otu_data == "sample":
#            otu_data.append(otu_table.data(obs_id))
#         else:
#             otu_data.append(otu_table.data(obs_id, axis='observation'))
#         if genome_table_ids == "observation":
#            genome_data.append(genome_table.data(obs_id, axis='observation'))
#         else:
#            genome_data.append(genome_table.data(obs_id))
#     return otu_data, genome_data, overlapping_otus


from numpy import dot, array, around
from biom.table import Table
from picrust.predict_metagenomes import get_overlapping_ids,extract_otu_and_genome_data
    otu_table -- the BIOM Table object for the OTU table
    genome_table -- the BIOM Table object for the predicted genomes
    limit_to_functions -- a list of function ids to include.
      If empty, include all function ids

    limit_by_function_categories -- if provided limit by functional category.
      For example, this can be used to limit output by KEGG functional categories

    Output table as a list of lists with header
    Function\tOrganism\tSample\tCounts\tpercent_of_sample
    """

    if limit_to_functions:
        if verbose:
            print "Filtering the genome table to include only user-specified functions:",limit_to_functions
        ok_ids = frozenset(map(str,limit_to_functions))

        filter_by_set = lambda vals,gene_id,metadata: str(gene_id) in ok_ids
        genome_table = genome_table.filter(filter_by_set, axis='observation')

        if genome_table.is_empty():
            raise ValueError("User filtering by functions (%s) removed all results from the genome table"%(str(limit_to_functions)))

    if limit_to_functional_categories:
        fn_cat_filter = make_pathway_filter_fn(ok_values = frozenset(map(str,limit_to_functional_categories)),metadata_key=metadata_key)
        genome_table = genome_table.filter(fn_cat_filter, axis='observation', inplace=False)

        if genome_table.is_empty():
            raise ValueError("User filtering by functional categories (%s) removed all results from the genome table"%(str(limit_to_functional_categories)))


# Path: picrust/predict_metagenomes.py
# def get_overlapping_ids(otu_table,genome_table,genome_table_ids="sample",\
#   otu_table_ids="observation"):
#     """Get the ids that overlap between the OTU and genome tables
#     otu_table - a BIOM Table object representing the OTU table
#     genome_table - a BIOM Table object for the genomes
#     genome_table_ids - specifies whether the ids of interest are sample or observation
#       in the genome table.  Useful because metagenome tables are often represented with genes
#       as observations.
#     """
#     for id_type in [genome_table_ids,otu_table_ids]:
#         if id_type not in ["sample", "observation"]:
#             raise ValueError(\
#               "%s is not a valid id type. Choices are 'sample' or 'observation'" % id_type)
#     overlapping_otus = list(set(otu_table.ids(axis=otu_table_ids)) &
#                             set(genome_table.ids(axis=genome_table_ids)))
# 
#     if len(overlapping_otus) < 1:
#         raise ValueError,\
#          "No common OTUs between the otu table and the genome table, so can't predict metagenome."
#     return overlapping_otus
# 
# def extract_otu_and_genome_data(otu_table, genome_table,
#                                 genome_table_ids="sample",
#                                 otu_table_ids="observation"):
#     """Return lists of otu,genome data, and overlapping genome/otu ids
# 
#     otu_table -- biom Table object for the OTUs
#     genome_table -- biom Table object for the genomes
#     """
#     overlapping_otus = get_overlapping_ids(otu_table, genome_table,
#         genome_table_ids=genome_table_ids,otu_table_ids=otu_table_ids)
#     # create lists to contain filtered data - we're going to need the data in
#     # numpy arrays, so it makes sense to compute this way rather than filtering
#     # the tables
#     otu_data = []
#     genome_data = []
# 
#     # build lists of filtered data
#     for obs_id in overlapping_otus:
#         if otu_data == "sample":
#            otu_data.append(otu_table.data(obs_id))
#         else:
#             otu_data.append(otu_table.data(obs_id, axis='observation'))
#         if genome_table_ids == "observation":
#            genome_data.append(genome_table.data(obs_id, axis='observation'))
#         else:
#            genome_data.append(genome_table.data(obs_id))
#     return otu_data, genome_data, overlapping_otus


# Path: picrust/metagenome_contributions.py
from numpy import dot, array, around
from biom.table import Table
from picrust.predict_metagenomes import get_overlapping_ids,extract_otu_and_genome_data

    otu_table -- the BIOM Table object for the OTU table
    genome_table -- the BIOM Table object for the predicted genomes
    limit_to_functions -- a list of function ids to include.
      If empty, include all function ids

    limit_by_function_categories -- if provided limit by functional category.
      For example, this can be used to limit output by KEGG functional categories

    Output table as a list of lists with header
    Function\tOrganism\tSample\tCounts\tpercent_of_sample
    """

    if limit_to_functions:
        if verbose:
            print "Filtering the genome table to include only user-specified functions:",limit_to_functions
        ok_ids = frozenset(map(str,limit_to_functions))

        filter_by_set = lambda vals,gene_id,metadata: str(gene_id) in ok_ids
        genome_table = genome_table.filter(filter_by_set, axis='observation')

        if genome_table.is_empty():
            raise ValueError("User filtering by functions (%s) removed all results from the genome table"%(str(limit_to_functions)))

    if limit_to_functional_categories:
        fn_cat_filter = make_pathway_filter_fn(ok_values = frozenset(map(str,limit_to_functional_categories)),metadata_key=metadata_key)
        genome_table = genome_table.filter(fn_cat_filter, axis='observation', inplace=False)

        if genome_table.is_empty():
            raise ValueError("User filtering by functional categories (%s) removed all results from the genome table"%(str(limit_to_functional_categories)))


    otu_data,genome_data,overlapping_ids = extract_otu_and_genome_data(otu_table,genome_table)