# Path: taxtastic/ncbi.py
# UNORDERED_RANKS = [
#     'clade',
#     'isolate',
#     'forma_specialis',
#     'no_rank',
#     'serotype',
#     'strain']
# 
# Path: taxtastic/utils.py
# def random_name(length):
#     return ''.join([random.choice(string.ascii_letters) for n in range(length)])


import logging
import sqlalchemy
from jinja2 import Template
from sqlalchemy import MetaData, and_, or_
from sqlalchemy.sql import select
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm.session import sessionmaker
from taxtastic.ncbi import UNORDERED_RANKS
from taxtastic.utils import random_name
            with self.engine.connect() as con:
                # insert tax_ids into a temporary table

                temptab = self.prepend_schema(random_name(12))

                cmd = 'CREATE TEMPORARY TABLE "{tab}" (old_tax_id text)'.format(
                    tab=temptab)
                con.execute(cmd)

                log.info('inserting tax_ids into temporary table')
                # TODO: couldn't find an equivalent of "executemany" - does one exist?
                cmd = 'INSERT INTO "{tab}" VALUES ({x})'.format(
                    tab=temptab, x=self.placeholder)
                for tax_id in tax_ids:
                    con.execute(cmd, tax_id)

                log.info('executing recursive CTE')
                cmd = Template("""
                WITH RECURSIVE a AS (
                 SELECT tax_id as tid, 1 AS ord, tax_id, parent_id, rank
                  FROM {{ nodes }}
                  WHERE tax_id in (
                  {% if merge_obsolete %}
                  SELECT COALESCE(m.new_tax_id, "{{ temptab }}".old_tax_id)
                    FROM "{{ temptab }}" LEFT JOIN {{ merged }} m USING(old_tax_id)
                  {% else %}
                  SELECT * from "{{ temptab }}"
                  {% endif %}
                  )
                UNION ALL

# Path: taxtastic/ncbi.py
# UNORDERED_RANKS = [
#     'clade',
#     'isolate',
#     'forma_specialis',
#     'no_rank',
#     'serotype',
#     'strain']
# 
# Path: taxtastic/utils.py
# def random_name(length):
#     return ''.join([random.choice(string.ascii_letters) for n in range(length)])


# Path: taxtastic/taxonomy.py
import logging
import sqlalchemy
from jinja2 import Template
from sqlalchemy import MetaData, and_, or_
from sqlalchemy.sql import select
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm.session import sessionmaker
from taxtastic.ncbi import UNORDERED_RANKS
from taxtastic.utils import random_name

            with self.engine.connect() as con:
                # insert tax_ids into a temporary table

                temptab = self.prepend_schema(random_name(12))

                cmd = 'CREATE TEMPORARY TABLE "{tab}" (old_tax_id text)'.format(
                    tab=temptab)
                con.execute(cmd)

                log.info('inserting tax_ids into temporary table')
                # TODO: couldn't find an equivalent of "executemany" - does one exist?
                cmd = 'INSERT INTO "{tab}" VALUES ({x})'.format(
                    tab=temptab, x=self.placeholder)
                for tax_id in tax_ids:
                    con.execute(cmd, tax_id)

                log.info('executing recursive CTE')
                cmd = Template("""
                WITH RECURSIVE a AS (
                 SELECT tax_id as tid, 1 AS ord, tax_id, parent_id, rank
                  FROM {{ nodes }}
                  WHERE tax_id in (
                  {% if merge_obsolete %}
                  SELECT COALESCE(m.new_tax_id, "{{ temptab }}".old_tax_id)
                    FROM "{{ temptab }}" LEFT JOIN {{ merged }} m USING(old_tax_id)
                  {% else %}
                  SELECT * from "{{ temptab }}"
                  {% endif %}
                  )
                UNION ALL

                 SELECT a.tid, a.ord + 1, p.tax_id, p.parent_id, p.rank