# Path: picrust/count.py
# def wagner_for_picrust(tree_path,trait_table_path,gain=None,max_paralogs=None,HALT_EXEC=False):
#     '''Runs count application controller given path of tree and trait table and returns a Table'''
#     #initialize Count app controller
#     count=Count(HALT_EXEC=HALT_EXEC)
# 
#     #set the parameters
#     if gain:
#         count.Parameters['-gain'].on(gain)
#     if max_paralogs:
#         count.Parameters['-max_paralogs'].on(max_paralogs)
# 
#     ###Have to manipulate the trait table some. Need to transpose it and strip ids surrounded in quotes.
#     table = LoadTable(filename=trait_table_path,header=True,sep='\t')
# 
#     #get the first column (containing row ids)
#     genome_ids = table.getRawData(table.Header[0])
#     #remove single quotes from the id if they exist
#     genome_ids=[str(id).strip('\'') for id in genome_ids]
#     #transpose the matrix
#     table = table.transposed(new_column_name=table.Header[0])
#     #Change the headers
#     table=table.withNewHeader(table.Header[1:],genome_ids)
#     #write the modified table to a tmp file
#     tmp_table_path =get_tmp_filename()
#     table.writeToFile(tmp_table_path,sep='\t')
# 
#     #Run Count here
#     result = count(data=(tree_path,tmp_table_path))
# 
#     #Remove tmp file
#     remove(tmp_table_path)
# 
#     #tree=LoadTree(tree_path)
#     tree=DndParser(open(tree_path))
# 
#     #parse the results into a Cogent Table
#     asr_table= parse_wagner_parsimony_output(result["StdOut"].readlines(),remove_num_tips=len(tree.tips()))
# 
#     #transpose the table
#     asr_table = asr_table.transposed(new_column_name='nodes')
# 
#     return asr_table
# 
# def parse_wagner_parsimony_output(raw_output_with_comments,remove_num_tips=0):
#     '''Parses wagner parsimony output from Count and returns a Cogent Table object'''
# 
#     #keep only lines with actual ASR count information
#     #throw away first 2 columns and last 4 columns (these are extra stuff from Count)
#     filtered_output=[x.split('\t')[1:-4] for x in raw_output_with_comments if x[0:8] == '# FAMILY']
# 
#     if(remove_num_tips):
#         #remove columns that contain trait data for tips (not internal node data)
#         filtered_output=[[x[0]]+ x[remove_num_tips+1:] for x in filtered_output]
# 
# 
#     #Take the first row as the header and the rest as rows in the table
#     table=Table(filtered_output[0],filtered_output[1:])
#     return table


from cogent.util.unit_test import TestCase, main
from picrust.count import wagner_for_picrust, parse_wagner_parsimony_output
from cogent.app.util import get_tmp_filename
from cogent.util.misc import remove_files
from cogent import LoadTable
from cogent.util.table import Table
        self.in_tree1_file.write(in_tree1)
        self.in_tree1_file.close()

        #create a tmp tree file (with quoted tip names)
        self.in_tree2_fp = get_tmp_filename(prefix='CountTests',suffix='.nwk')
        self.in_tree2_file = open(self.in_tree2_fp,'w')
        self.in_tree2_file.write(in_tree2)
        self.in_tree2_file.close()

        #create a tmp trait file
        self.in_trait1_fp = get_tmp_filename(prefix='CountTests',suffix='.tsv')
        self.in_trait1_file=open(self.in_trait1_fp,'w')
        self.in_trait1_file.write(in_trait1)
        self.in_trait1_file.close()
        
        #create a tmp trait file (with quoted tip names)
        self.in_trait3_fp = get_tmp_filename(prefix='CountTests',suffix='.tsv')
        self.in_trait3_file=open(self.in_trait3_fp,'w')
        self.in_trait3_file.write(in_trait3)
        self.in_trait3_file.close()

        self.files_to_remove = [self.in_tree1_fp,self.in_trait1_fp]

    def tearDown(self):
        remove_files(self.files_to_remove)
                       

    def test_wagner_for_picrust(self):
        """ test_wagner_for_picrust functions as expected with valid input
        """

# Path: picrust/count.py
# def wagner_for_picrust(tree_path,trait_table_path,gain=None,max_paralogs=None,HALT_EXEC=False):
#     '''Runs count application controller given path of tree and trait table and returns a Table'''
#     #initialize Count app controller
#     count=Count(HALT_EXEC=HALT_EXEC)
# 
#     #set the parameters
#     if gain:
#         count.Parameters['-gain'].on(gain)
#     if max_paralogs:
#         count.Parameters['-max_paralogs'].on(max_paralogs)
# 
#     ###Have to manipulate the trait table some. Need to transpose it and strip ids surrounded in quotes.
#     table = LoadTable(filename=trait_table_path,header=True,sep='\t')
# 
#     #get the first column (containing row ids)
#     genome_ids = table.getRawData(table.Header[0])
#     #remove single quotes from the id if they exist
#     genome_ids=[str(id).strip('\'') for id in genome_ids]
#     #transpose the matrix
#     table = table.transposed(new_column_name=table.Header[0])
#     #Change the headers
#     table=table.withNewHeader(table.Header[1:],genome_ids)
#     #write the modified table to a tmp file
#     tmp_table_path =get_tmp_filename()
#     table.writeToFile(tmp_table_path,sep='\t')
# 
#     #Run Count here
#     result = count(data=(tree_path,tmp_table_path))
# 
#     #Remove tmp file
#     remove(tmp_table_path)
# 
#     #tree=LoadTree(tree_path)
#     tree=DndParser(open(tree_path))
# 
#     #parse the results into a Cogent Table
#     asr_table= parse_wagner_parsimony_output(result["StdOut"].readlines(),remove_num_tips=len(tree.tips()))
# 
#     #transpose the table
#     asr_table = asr_table.transposed(new_column_name='nodes')
# 
#     return asr_table
# 
# def parse_wagner_parsimony_output(raw_output_with_comments,remove_num_tips=0):
#     '''Parses wagner parsimony output from Count and returns a Cogent Table object'''
# 
#     #keep only lines with actual ASR count information
#     #throw away first 2 columns and last 4 columns (these are extra stuff from Count)
#     filtered_output=[x.split('\t')[1:-4] for x in raw_output_with_comments if x[0:8] == '# FAMILY']
# 
#     if(remove_num_tips):
#         #remove columns that contain trait data for tips (not internal node data)
#         filtered_output=[[x[0]]+ x[remove_num_tips+1:] for x in filtered_output]
# 
# 
#     #Take the first row as the header and the rest as rows in the table
#     table=Table(filtered_output[0],filtered_output[1:])
#     return table


# Path: tests/test_count.py
from cogent.util.unit_test import TestCase, main
from picrust.count import wagner_for_picrust, parse_wagner_parsimony_output
from cogent.app.util import get_tmp_filename
from cogent.util.misc import remove_files
from cogent import LoadTable
from cogent.util.table import Table

        self.in_tree1_file.write(in_tree1)
        self.in_tree1_file.close()

        #create a tmp tree file (with quoted tip names)
        self.in_tree2_fp = get_tmp_filename(prefix='CountTests',suffix='.nwk')
        self.in_tree2_file = open(self.in_tree2_fp,'w')
        self.in_tree2_file.write(in_tree2)
        self.in_tree2_file.close()

        #create a tmp trait file
        self.in_trait1_fp = get_tmp_filename(prefix='CountTests',suffix='.tsv')
        self.in_trait1_file=open(self.in_trait1_fp,'w')
        self.in_trait1_file.write(in_trait1)
        self.in_trait1_file.close()
        
        #create a tmp trait file (with quoted tip names)
        self.in_trait3_fp = get_tmp_filename(prefix='CountTests',suffix='.tsv')
        self.in_trait3_file=open(self.in_trait3_fp,'w')
        self.in_trait3_file.write(in_trait3)
        self.in_trait3_file.close()

        self.files_to_remove = [self.in_tree1_fp,self.in_trait1_fp]

    def tearDown(self):
        remove_files(self.files_to_remove)
                       

    def test_wagner_for_picrust(self):
        """ test_wagner_for_picrust functions as expected with valid input
        """

        actual= wagner_for_picrust(self.in_tree1_fp,self.in_trait1_fp)