# Path: taxtastic/taxtable.py
# class TaxNode(object):
#     def __init__(self, rank, tax_id, parent=None, sequence_ids=None,
#                  children=None, name=None, ranks=None):
#     def add_child(self, child):
#     def remove_child(self, child):
#     def drop(self):
#     def prune_unrepresented(self):
#     def is_leaf(self):
#     def is_root(self):
#     def at_rank(self, rank):
#     def depth_first_iter(self, self_first=True):
#     def subtree_sequence_ids(self):
#     def remove_subtree(self):
#     def path(self, tax_ids):
#     def get_node(self, tax_id):
#     def lineage(self):
#     def __repr__(self):
#     def __iter__(self):
#     def write_taxtable(self, out_fp, **kwargs):
#         def node_record(node):
#     def populate_from_seqinfo(self, seqinfo):
#     def collapse(self, remove=False):
#     def write_seqinfo(self, out_fp, include_name=True):
#     def from_taxtable(cls, taxtable_fp):
#     def from_taxdb(cls, con, root=None):
#         def add_lineage(parent):
# def read(fp):
#             L = self.parent.lineage()
#             L = {i.rank: i.tax_id for i in node.lineage()}


import argparse
import csv
import logging
import sys
from taxtastic import taxtable
    # intervening ranks, but we don't have enough information to sort that.
    tax.ranks.insert(parent_idx + 1, rank)


def action(args):
    with args.taxtable as fp:
        tax = taxtable.read(fp)

    with args.extra_nodes_csv:
        reader = csv.DictReader(args.extra_nodes_csv)
        missing_fields = frozenset(
            ['tax_id', 'tax_name', 'rank', 'parent_id']) - frozenset(reader.fieldnames)
        if missing_fields:
            raise IOError("Missing expected fields: {0}".format(
                ','.join(missing_fields)))
        for row in reader:
            if row['tax_id'] in tax.index:
                logging.warn("tax_id %s already represented in taxtable. [row %d]",
                             row['tax_id'], reader.line_num)
                continue

            parent_id = row['parent_id']
            rank = row['rank']
            try:
                parent_node = tax.get_node(parent_id)
            except ValueError:
                raise ValueError(
                    "Parent {parent_id} of {tax_id}[{tax_name}] not found.".format(**row))
            if rank not in tax.ranks:
                add_rank(tax, parent_node, rank)

# Path: taxtastic/taxtable.py
# class TaxNode(object):
#     def __init__(self, rank, tax_id, parent=None, sequence_ids=None,
#                  children=None, name=None, ranks=None):
#     def add_child(self, child):
#     def remove_child(self, child):
#     def drop(self):
#     def prune_unrepresented(self):
#     def is_leaf(self):
#     def is_root(self):
#     def at_rank(self, rank):
#     def depth_first_iter(self, self_first=True):
#     def subtree_sequence_ids(self):
#     def remove_subtree(self):
#     def path(self, tax_ids):
#     def get_node(self, tax_id):
#     def lineage(self):
#     def __repr__(self):
#     def __iter__(self):
#     def write_taxtable(self, out_fp, **kwargs):
#         def node_record(node):
#     def populate_from_seqinfo(self, seqinfo):
#     def collapse(self, remove=False):
#     def write_seqinfo(self, out_fp, include_name=True):
#     def from_taxtable(cls, taxtable_fp):
#     def from_taxdb(cls, con, root=None):
#         def add_lineage(parent):
# def read(fp):
#             L = self.parent.lineage()
#             L = {i.rank: i.tax_id for i in node.lineage()}


# Path: taxtastic/subcommands/add_to_taxtable.py
import argparse
import csv
import logging
import sys
from taxtastic import taxtable

    # intervening ranks, but we don't have enough information to sort that.
    tax.ranks.insert(parent_idx + 1, rank)


def action(args):
    with args.taxtable as fp:
        tax = taxtable.read(fp)

    with args.extra_nodes_csv:
        reader = csv.DictReader(args.extra_nodes_csv)
        missing_fields = frozenset(
            ['tax_id', 'tax_name', 'rank', 'parent_id']) - frozenset(reader.fieldnames)
        if missing_fields:
            raise IOError("Missing expected fields: {0}".format(
                ','.join(missing_fields)))
        for row in reader:
            if row['tax_id'] in tax.index:
                logging.warn("tax_id %s already represented in taxtable. [row %d]",
                             row['tax_id'], reader.line_num)
                continue

            parent_id = row['parent_id']
            rank = row['rank']
            try:
                parent_node = tax.get_node(parent_id)
            except ValueError:
                raise ValueError(
                    "Parent {parent_id} of {tax_id}[{tax_name}] not found.".format(**row))
            if rank not in tax.ranks:
                add_rank(tax, parent_node, rank)

            node = taxtable.TaxNode(