# Path: picrust/parse.py
# def parse_trait_table(trait_table_lines,delimiter="\t",has_header=True):
#     """Return a header line, and a generator that will yield data fields
# 
#     trait_table_lines -- tab-seperated lines that may have newline characters
# 
#     if has_header is True, then the first non-blank, non-comment line
#     must be a header line of equal length to the number of columns, with
#     labels for the contents of each column.
# 
#     Comment lines (starting with '#') and blank lines will be ignored,
#     and won't show up in the output.
#     """
#     header_line = None
#     if not has_header:
#         header_line = ''
#     else:
#         for i,line in enumerate(trait_table_lines):
#             if not line or line.startswith("#"):
#                 continue
#             if i == 0:
#                 header_line = line
#                 break
#     if header_line is None:
#         raise RuntimeError("Could not find header line in input trait table lines.  Was it skipped due to starting with a comment ('#') sign?")
#     #Now that we have the header (if present) yield_trait_table_fields
#     #can assume no header exists, and just parse data fields
#     return header_line, yield_trait_table_fields(trait_table_lines,\
#       delimiter=delimiter,has_header=False)
# 
# def yield_trait_table_fields(trait_table_lines,delimiter="\t",\
#     skip_comment_lines=True,has_header=False):
#     """Yield fields from trait table lines
# 
#     The current definition for the header lines is as follows:
#     -- can't start with a comment
#     -- must be the first line
# 
#     """
#     for i,line in enumerate(trait_table_lines):
# 
#         if line.startswith("#") and skip_comment_lines:
#             #ignore these and remove from outputs
#             continue
# 
#         if has_header and i == 0:
#             #header line has no fields and should be skipped
#             continue
# 
#         #Check for bad delimiters and try to intelligently warn user
#         #if they used the wrong delimiter
#         if delimiter and delimiter not in line:
#             delimiters_to_check = {"tab":"\t","space":"","comma":","}
#             possible_delimiters = []
#             for delim in delimiters_to_check.keys():
#                 if delimiters_to_check[delim] in line:
#                     possible_delimiters.append(delim)
#             error_line =\
#                 "Delimiter '%s' not in line.  The following delimiters were found:  %s.  Is the correct delimiter one of these?"
#             raise RuntimeError(error_line % (delimiter,\
#              ",".join(possible_delimiters)))
# 
# 
# 
#         fields = line.strip().split(delimiter)
#         yield fields


from os.path import splitext
from string import maketrans
from sys import getrecursionlimit,setrecursionlimit
from cogent.parse.tree import DndParser
from cogent.util.option_parsing import parse_command_line_parameters,\
    make_option
from picrust.parse import parse_trait_table,yield_trait_table_fields
from util import PicrustNode
import re
    replace_ambiguous_states -- if True, replace various strings representing ambiguous character states,
    as well as '-1' or -1 (used by IMG to represent a lack of data) with 0 values.

    replace_problematic_table_chars -- if True, replace ':' and ';' in the results with '_', and remove double quotes.
    (AncSR methods like ace can't handle these characters in organism labels)

    min_branch_length -- set the minimum branch length for all edges in the tree.

    This function combines the various reformatting functions in the
    library into a catch-all reformatter.

    TODO: This function is monolithic, so despite the individual
    parts being tested seperately, it probably needs to be broken
    down into several modular parts.  This would need to be done
    with care however, as the order of steps matters quite a bit.


    """



    input_tree = tree

    #Parse lines to fields once


    if trait_table_lines:
        if verbose:
            print "Parsing trait table...."
        header_line,trait_table_fields =\

# Path: picrust/parse.py
# def parse_trait_table(trait_table_lines,delimiter="\t",has_header=True):
#     """Return a header line, and a generator that will yield data fields
# 
#     trait_table_lines -- tab-seperated lines that may have newline characters
# 
#     if has_header is True, then the first non-blank, non-comment line
#     must be a header line of equal length to the number of columns, with
#     labels for the contents of each column.
# 
#     Comment lines (starting with '#') and blank lines will be ignored,
#     and won't show up in the output.
#     """
#     header_line = None
#     if not has_header:
#         header_line = ''
#     else:
#         for i,line in enumerate(trait_table_lines):
#             if not line or line.startswith("#"):
#                 continue
#             if i == 0:
#                 header_line = line
#                 break
#     if header_line is None:
#         raise RuntimeError("Could not find header line in input trait table lines.  Was it skipped due to starting with a comment ('#') sign?")
#     #Now that we have the header (if present) yield_trait_table_fields
#     #can assume no header exists, and just parse data fields
#     return header_line, yield_trait_table_fields(trait_table_lines,\
#       delimiter=delimiter,has_header=False)
# 
# def yield_trait_table_fields(trait_table_lines,delimiter="\t",\
#     skip_comment_lines=True,has_header=False):
#     """Yield fields from trait table lines
# 
#     The current definition for the header lines is as follows:
#     -- can't start with a comment
#     -- must be the first line
# 
#     """
#     for i,line in enumerate(trait_table_lines):
# 
#         if line.startswith("#") and skip_comment_lines:
#             #ignore these and remove from outputs
#             continue
# 
#         if has_header and i == 0:
#             #header line has no fields and should be skipped
#             continue
# 
#         #Check for bad delimiters and try to intelligently warn user
#         #if they used the wrong delimiter
#         if delimiter and delimiter not in line:
#             delimiters_to_check = {"tab":"\t","space":"","comma":","}
#             possible_delimiters = []
#             for delim in delimiters_to_check.keys():
#                 if delimiters_to_check[delim] in line:
#                     possible_delimiters.append(delim)
#             error_line =\
#                 "Delimiter '%s' not in line.  The following delimiters were found:  %s.  Is the correct delimiter one of these?"
#             raise RuntimeError(error_line % (delimiter,\
#              ",".join(possible_delimiters)))
# 
# 
# 
#         fields = line.strip().split(delimiter)
#         yield fields


# Path: picrust/format_tree_and_trait_table.py
from os.path import splitext
from string import maketrans
from sys import getrecursionlimit,setrecursionlimit
from cogent.parse.tree import DndParser
from cogent.util.option_parsing import parse_command_line_parameters,\
    make_option
from picrust.parse import parse_trait_table,yield_trait_table_fields
from util import PicrustNode
import re

    replace_ambiguous_states -- if True, replace various strings representing ambiguous character states,
    as well as '-1' or -1 (used by IMG to represent a lack of data) with 0 values.

    replace_problematic_table_chars -- if True, replace ':' and ';' in the results with '_', and remove double quotes.
    (AncSR methods like ace can't handle these characters in organism labels)

    min_branch_length -- set the minimum branch length for all edges in the tree.

    This function combines the various reformatting functions in the
    library into a catch-all reformatter.

    TODO: This function is monolithic, so despite the individual
    parts being tested seperately, it probably needs to be broken
    down into several modular parts.  This would need to be done
    with care however, as the order of steps matters quite a bit.


    """



    input_tree = tree

    #Parse lines to fields once


    if trait_table_lines:
        if verbose:
            print "Parsing trait table...."
        header_line,trait_table_fields =\

          parse_trait_table(trait_table_lines,delimiter = input_trait_table_delimiter)