# Path: picrust/format_tree_and_trait_table.py
# def filter_table_by_presence_in_tree(tree,trait_table_fields,name_field_index = 0,delimiter="\t"):
#     """yield lines of a trait table lacking organisms missing from the tree"""
# 
#     tree_tips = [str(node.Name.strip()) for node in tree.preorder()]
#     #print tree_tips
#     result_fields = []
#     for fields in trait_table_fields:
#         curr_name = fields[name_field_index].strip()
#         if curr_name not in tree_tips:
#             #print curr_name,"could not be found in tree nodes"
#             #print curr_name in tree_tips
#             #try:
#             #    print int(curr_name) in tree_tips
#             #except:
#             #    pass
#             #print curr_name.strip() in tree_tips
#             continue
#         result_fields.append(fields)
#     return result_fields
# 
# def get_sub_tree(tree,tips_not_to_prune):
#     """Get sub tree, modifying recursion limit if necessary"""
# 
#     try:
#         new_tree = tree.getSubTree(tips_not_to_prune)
#     except RuntimeError:
#         #NOTE:  getSubTree will hit
#         #maximum recursion depth on large trees
#         #Try working around this issue with a large
#         #recursion depth limit
#         old_recursion_limit = getrecursionlimit()
#         setrecursionlimit(50000)
#         new_tree = tree.getSubTree(tips_not_to_prune)
#         setrecursionlimit(old_recursion_limit)
#     return new_tree


from sys import argv
from copy import deepcopy
from random import sample, shuffle
from picrust.format_tree_and_trait_table import filter_table_by_presence_in_tree, get_sub_tree
import os
    trait_table_fields -- a list of strings from a trait table
      (the first is organism name, the rest are trait values)

    test_fn_factory -- a factory function that returns a new test tree generating function,
      given a distance parameter.  The test tree generating function, in turn, should
      return a new test tree, given a tip of interest and the full tree.

    min_dist -- the minimum distance at which to start generating test
      trees.

    max_dist -- the maximum distance for test trees

    increment -- the increment by which the distance parameter should be
      increased.

    modify_tree -- if set to False, just return the original tree without modification.

    limit_to_tips -- takes a list of organisms(tree tips) to include in the analysis.  If this is provided, instead of making test datasets
    for all tips, only generate test datasets for tree tips with names exactly matching an entry in the supplied list of tips.

    For example, to limit the datasets to just E.coli K-12 MG1655 (when using the IMG / greengenes dataset)
    pass limit_to_included_tips=['NC_000913|646311926']
    """

    if verbose:
    #Include only organisms present in the tree
        print "Prefiltering orgs in trait table:",len([f[0] for f in trait_table_fields])
        print "Orgs with single quotes:",len([f[0] for f in trait_table_fields if "'" in f[0]])
        print "Tree tips with single quotes:",len([t.Name for t in tree.iterTips() if "'" in t.Name])


# Path: picrust/format_tree_and_trait_table.py
# def filter_table_by_presence_in_tree(tree,trait_table_fields,name_field_index = 0,delimiter="\t"):
#     """yield lines of a trait table lacking organisms missing from the tree"""
# 
#     tree_tips = [str(node.Name.strip()) for node in tree.preorder()]
#     #print tree_tips
#     result_fields = []
#     for fields in trait_table_fields:
#         curr_name = fields[name_field_index].strip()
#         if curr_name not in tree_tips:
#             #print curr_name,"could not be found in tree nodes"
#             #print curr_name in tree_tips
#             #try:
#             #    print int(curr_name) in tree_tips
#             #except:
#             #    pass
#             #print curr_name.strip() in tree_tips
#             continue
#         result_fields.append(fields)
#     return result_fields
# 
# def get_sub_tree(tree,tips_not_to_prune):
#     """Get sub tree, modifying recursion limit if necessary"""
# 
#     try:
#         new_tree = tree.getSubTree(tips_not_to_prune)
#     except RuntimeError:
#         #NOTE:  getSubTree will hit
#         #maximum recursion depth on large trees
#         #Try working around this issue with a large
#         #recursion depth limit
#         old_recursion_limit = getrecursionlimit()
#         setrecursionlimit(50000)
#         new_tree = tree.getSubTree(tips_not_to_prune)
#         setrecursionlimit(old_recursion_limit)
#     return new_tree


# Path: picrust/make_test_datasets.py
from sys import argv
from copy import deepcopy
from random import sample, shuffle
from picrust.format_tree_and_trait_table import filter_table_by_presence_in_tree, get_sub_tree
import os

    trait_table_fields -- a list of strings from a trait table
      (the first is organism name, the rest are trait values)

    test_fn_factory -- a factory function that returns a new test tree generating function,
      given a distance parameter.  The test tree generating function, in turn, should
      return a new test tree, given a tip of interest and the full tree.

    min_dist -- the minimum distance at which to start generating test
      trees.

    max_dist -- the maximum distance for test trees

    increment -- the increment by which the distance parameter should be
      increased.

    modify_tree -- if set to False, just return the original tree without modification.

    limit_to_tips -- takes a list of organisms(tree tips) to include in the analysis.  If this is provided, instead of making test datasets
    for all tips, only generate test datasets for tree tips with names exactly matching an entry in the supplied list of tips.

    For example, to limit the datasets to just E.coli K-12 MG1655 (when using the IMG / greengenes dataset)
    pass limit_to_included_tips=['NC_000913|646311926']
    """

    if verbose:
    #Include only organisms present in the tree
        print "Prefiltering orgs in trait table:",len([f[0] for f in trait_table_fields])
        print "Orgs with single quotes:",len([f[0] for f in trait_table_fields if "'" in f[0]])
        print "Tree tips with single quotes:",len([t.Name for t in tree.iterTips() if "'" in t.Name])


    trait_table_fields = filter_table_by_presence_in_tree(tree,\