#make output table, one line per hog,
rs = {
    'hog': hog,
    'target_sel': 0,
    'target_non': 0,
    'other_sel': 0,
    'other_non': 0,
    'target_sel_nom': 0,
    'other_sel_nom': 0,
    'nom_branches': "",
    'cor_branches': ""
}

for line in res_list:
    taxa = line[0]
    if t.search_nodes(name=taxa):
        pval = float(line[6])
        pvalholm = float(line[7])
        is_testclass = node_in_class(taxa, t, testclade)
        if pval < 0.05:
            if pvalholm < 0.05:
                if is_testclass:
                    rs['target_sel'] = rs['target_sel'] + 1
                else:
                    rs['other_sel'] = rs['other_sel'] + 1
                rs['cor_branches'] = rs['cor_branches'] + taxa + ":"
            if is_testclass:
                rs['target_sel_nom'] = rs['target_sel_nom'] + 1
            else:
                rs['other_sel_nom'] = rs['other_sel_nom'] + 1
            rs['nom_branches'] = rs['nom_branches'] + taxa + ":"
        tree.prune(taxa_in_alignment, preserve_branch_length=True)

test_taxa = []
with open(test_taxa_file, 'r') as test_taxa_list:
    for taxon in test_taxa_list:
        taxon = taxon.rstrip()
        test_taxa.append(taxon)

nodes_to_mark = set()  # set since we want it to be all unique ids

# Mark the test taxa
for taxon in test_taxa:
    taxon_node = tree & taxon  # ete3 notation for finding a node within a tree
    taxon_id = taxon_node.node_id  # mark_tree only takes node_ids, not labels
    nodes_to_mark.add(taxon_id)

# Find internal nodes below the test taxa and mark them
for i in range(len(test_taxa), 1, -1):
    taxa_groups = [x for x in combinations(test_taxa, i)]
    for group in taxa_groups:
        common_node = tree.get_common_ancestor(*group)
        taxon_id = common_node.node_id
        nodes_to_mark.add(taxon_id)

#TODO change the names of the nodes
for mark_id in nodes_to_mark:
    test_node = tree.search_nodes(node_id=mark_id)[0]
    test_node.name += '{test}'

tree.write(outfile=out_tree_name, format=1)
with open(treefile, 'r') as treefile:
    treestring=treefile.read().replace('\n', '')

treestring=re.sub(r"{\w+}", "", treestring)
t=EvolTree(treestring, format=1)

#now read results into list
with open(resultsfile, 'r') as f:
    reader=csv.reader(f)
    res_list=list(reader)

#make output table, one line per hog, 
rs={'hog' : hog, 'selected_nom' : 0, 'selected_holm' : 0, 'total_tests' : 0, 'nom_branches' : "", 'holm_branches' : "", 'tree' : treestring}

for line in res_list:
    taxa=line[0]
    if t.search_nodes(name=taxa):
        pval=float(line[6])
        pvalholm=float(line[7])
        node_id=trans_node(taxa, t)
        rs['total_tests'] = rs['total_tests'] + 1
        if pval < 0.05:
            if pvalholm < 0.05:
                rs['selected_holm'] = rs['selected_holm'] + 1
                rs['holm_branches'] = rs['holm_branches'] + node_id + ":"
            rs['selected_nom'] = rs['selected_nom'] + 1
            rs['nom_branches'] = rs['nom_branches'] + node_id + ":"

print(rs['hog'], rs['selected_holm'], rs['selected_nom'], rs['total_tests'], rs['holm_branches'][:-1], rs['nom_branches'][:-1], rs['tree'], sep="\t")