# Path: lib/log.py
# class Logger:
#     """Common logging functions."""
# 
#     def __init__(self, log_file, log_level):
#         self.log_file = log_file
#         self.log_level = LEVEL[log_level]
#         self.file_handle = open(log_file, 'a') if log_file else None
# 
#     def __del__(self):
#         if self.file_handle:
#             self.file_handle.close()
# 
#     def header(self):
#         """Log header information."""
#         self.info('#' * 80)
#         self.info('aTRAM version: {}'.format(db.ATRAM_VERSION))
#         self.info('Python version: {}'.format(' '.join(sys.version.split())))
#         self.info(' '.join(sys.argv[:]))
# 
#     def subcommand(self, cmd, temp_dir, timeout=None):
#         """
#         Call a subprocess and log the output.
# 
#         Note: stdout=PIPE is blocking and large logs cause a hang.
#         So we don't use it.
#         """
#         self.debug(cmd)
# 
#         error = None
# 
#         with tempfile.NamedTemporaryFile(mode='w', dir=temp_dir) as log_output:
#             with subprocess.Popen(
#                     cmd, shell=True, stdout=log_output, stderr=log_output) as proc:
#                 try:
#                     proc.communicate(timeout=timeout)
# 
#                 # Catch any error and kill all child processes
#                 except Exception as err:  # pylint: disable=broad-except
#                     error = err
#                     self.error('Exception: {}'.format(err))
# 
#                 # On success or failure log what we can
#                 finally:
#                     wait = 5
# 
#                     killed, alive = util.kill_proc_tree(proc.pid, timeout=wait)
# 
#                     if killed:
#                         self.error('Killed {} processes with SIGTERM.'.format(killed))
# 
#                     if alive > 0:
#                         self.error('{} alive processes after SIGTERM'.format(alive))
# 
#                         killed, alive = util.kill_proc_tree(
#                             proc.pid, timeout=wait, sig=signal.SIGKILL)
#                         self.error('Killed {} processes with SIGKILL.'.format(killed))
#                         self.error('{} alive processes after SIGKILL'.format(alive))
# 
#                     with open(log_output.name) as log_input:
#                         for line in log_input:
#                             line = line.strip()
#                             if line:
#                                 self.debug(line)
#                     if error:
#                         raise error
# 
#     def _output(self, msg, level):
#         timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
#         msg = '({:8d}) {} {:<5}: {}'.format(os.getpid(), timestamp, level, msg)
#         print(msg)
#         if self.file_handle:
#             self.file_handle.write(msg)
#             self.file_handle.write('\n')
#             self.file_handle.flush()
# 
#     def debug(self, msg):
#         """Log an info message."""
#         if self.log_level <= DEBUG:
#             self._output(msg, 'DEBUG')
# 
#     def info(self, msg):
#         """Log an info message."""
#         if self.log_level <= INFO:
#             self._output(msg, 'INFO')
# 
#     def error(self, msg):
#         """Log an error message."""
#         if self.log_level <= ERROR:
#             self._output(msg, 'ERROR')
# 
#     def fatal(self, msg):
#         """Log an error message and exit."""
#         if self.log_level <= FATAL:
#             self._output(msg, 'FATAL')
#         sys.exit(1)


import multiprocessing
import sys
import numpy as np
from os.path import basename, join, splitext
from Bio.SeqIO.FastaIO import SimpleFastaParser
from Bio.SeqIO.QualityIO import FastqGeneralIterator
from . import blast, db, db_preprocessor, util
from .log import Logger
"""Format the data so that atram can use it later in atram itself.

It takes sequence read archive (SRA) files and converts them into coordinated
blast and sqlite3 databases.
"""





def preprocess(args):
    """Build the databases required by atram."""

# Path: lib/log.py
# class Logger:
#     """Common logging functions."""
# 
#     def __init__(self, log_file, log_level):
#         self.log_file = log_file
#         self.log_level = LEVEL[log_level]
#         self.file_handle = open(log_file, 'a') if log_file else None
# 
#     def __del__(self):
#         if self.file_handle:
#             self.file_handle.close()
# 
#     def header(self):
#         """Log header information."""
#         self.info('#' * 80)
#         self.info('aTRAM version: {}'.format(db.ATRAM_VERSION))
#         self.info('Python version: {}'.format(' '.join(sys.version.split())))
#         self.info(' '.join(sys.argv[:]))
# 
#     def subcommand(self, cmd, temp_dir, timeout=None):
#         """
#         Call a subprocess and log the output.
# 
#         Note: stdout=PIPE is blocking and large logs cause a hang.
#         So we don't use it.
#         """
#         self.debug(cmd)
# 
#         error = None
# 
#         with tempfile.NamedTemporaryFile(mode='w', dir=temp_dir) as log_output:
#             with subprocess.Popen(
#                     cmd, shell=True, stdout=log_output, stderr=log_output) as proc:
#                 try:
#                     proc.communicate(timeout=timeout)
# 
#                 # Catch any error and kill all child processes
#                 except Exception as err:  # pylint: disable=broad-except
#                     error = err
#                     self.error('Exception: {}'.format(err))
# 
#                 # On success or failure log what we can
#                 finally:
#                     wait = 5
# 
#                     killed, alive = util.kill_proc_tree(proc.pid, timeout=wait)
# 
#                     if killed:
#                         self.error('Killed {} processes with SIGTERM.'.format(killed))
# 
#                     if alive > 0:
#                         self.error('{} alive processes after SIGTERM'.format(alive))
# 
#                         killed, alive = util.kill_proc_tree(
#                             proc.pid, timeout=wait, sig=signal.SIGKILL)
#                         self.error('Killed {} processes with SIGKILL.'.format(killed))
#                         self.error('{} alive processes after SIGKILL'.format(alive))
# 
#                     with open(log_output.name) as log_input:
#                         for line in log_input:
#                             line = line.strip()
#                             if line:
#                                 self.debug(line)
#                     if error:
#                         raise error
# 
#     def _output(self, msg, level):
#         timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
#         msg = '({:8d}) {} {:<5}: {}'.format(os.getpid(), timestamp, level, msg)
#         print(msg)
#         if self.file_handle:
#             self.file_handle.write(msg)
#             self.file_handle.write('\n')
#             self.file_handle.flush()
# 
#     def debug(self, msg):
#         """Log an info message."""
#         if self.log_level <= DEBUG:
#             self._output(msg, 'DEBUG')
# 
#     def info(self, msg):
#         """Log an info message."""
#         if self.log_level <= INFO:
#             self._output(msg, 'INFO')
# 
#     def error(self, msg):
#         """Log an error message."""
#         if self.log_level <= ERROR:
#             self._output(msg, 'ERROR')
# 
#     def fatal(self, msg):
#         """Log an error message and exit."""
#         if self.log_level <= FATAL:
#             self._output(msg, 'FATAL')
#         sys.exit(1)


# Path: lib/core_preprocessor.py
import multiprocessing
import sys
import numpy as np
from os.path import basename, join, splitext
from Bio.SeqIO.FastaIO import SimpleFastaParser
from Bio.SeqIO.QualityIO import FastqGeneralIterator
from . import blast, db, db_preprocessor, util
from .log import Logger

"""Format the data so that atram can use it later in atram itself.

It takes sequence read archive (SRA) files and converts them into coordinated
blast and sqlite3 databases.
"""





def preprocess(args):
    """Build the databases required by atram."""

    log = Logger(args['log_file'], args['log_level'])