# Path: ariba/common.py
# class Error (Exception): pass
# def syscall(cmd, allow_fail=False, verbose=False, verbose_filehandle=sys.stdout, print_errors=True, shell=True):
# def decode(x):
# def cat_files(infiles, outfile):
# def download_file(url, outfile, max_attempts=3, sleep_time=2, verbose=False):
# def rmtree(input_dir):


import os
import sys
import csv
import zipfile
import pyfastaq
from ariba import common
        fh_tsv = pyfastaq.utils.open_file_write(tsv)

        for seq in sorted(sequences):
            final_column = []

            if seq in annotations:
                group = annotations[seq]['group']
                final_column.append('class:' + annotations[seq]['class'] + '; mechanism:' + annotations[seq]['mechanism'] + '; group:' + group)
            else:
                group = 'unknown'
                print('WARNING: sequence "', seq, '" has no record in annotations file', sep='', file=sys.stderr)

            if seq in header_mappings:
                final_column.append('Source_Database:' + header_mappings[seq]['Source_Database'] + '; Source_Headers:' + header_mappings[seq]['Source_Headers(space_separated)'])
            else:
                print('WARNING: sequence "', seq, '" has no record in header mappings file', sep='', file=sys.stderr)

            if len(final_column) > 0:
                print(group + '.' + seq, '1', '0', '.', '.', '; '.join(final_column), sep='\t', file=fh_tsv)
            else:
                print(group + '.' + seq, '1', '0', '.', '.', '.', sep='\t', file=fh_tsv)

            sequences[seq].id = group + '.' + sequences[seq].id
            print(sequences[seq], file=fh_fasta)

        fh_fasta.close()
        fh_tsv.close()


    def run(self):

# Path: ariba/common.py
# class Error (Exception): pass
# def syscall(cmd, allow_fail=False, verbose=False, verbose_filehandle=sys.stdout, print_errors=True, shell=True):
# def decode(x):
# def cat_files(infiles, outfile):
# def download_file(url, outfile, max_attempts=3, sleep_time=2, verbose=False):
# def rmtree(input_dir):


# Path: ariba/megares_zip_parser.py
import os
import sys
import csv
import zipfile
import pyfastaq
from ariba import common

        fh_tsv = pyfastaq.utils.open_file_write(tsv)

        for seq in sorted(sequences):
            final_column = []

            if seq in annotations:
                group = annotations[seq]['group']
                final_column.append('class:' + annotations[seq]['class'] + '; mechanism:' + annotations[seq]['mechanism'] + '; group:' + group)
            else:
                group = 'unknown'
                print('WARNING: sequence "', seq, '" has no record in annotations file', sep='', file=sys.stderr)

            if seq in header_mappings:
                final_column.append('Source_Database:' + header_mappings[seq]['Source_Database'] + '; Source_Headers:' + header_mappings[seq]['Source_Headers(space_separated)'])
            else:
                print('WARNING: sequence "', seq, '" has no record in header mappings file', sep='', file=sys.stderr)

            if len(final_column) > 0:
                print(group + '.' + seq, '1', '0', '.', '.', '; '.join(final_column), sep='\t', file=fh_tsv)
            else:
                print(group + '.' + seq, '1', '0', '.', '.', '.', sep='\t', file=fh_tsv)

            sequences[seq].id = group + '.' + sequences[seq].id
            print(sequences[seq], file=fh_fasta)

        fh_fasta.close()
        fh_tsv.close()


    def run(self):

        common.download_file(self.zip_url, self.zip_file, verbose=True)