
from VcfQC import VcfQC
from ReseqTrackDB import File
from ReseqTrackDB import ReseqTrackDB

import argparse
import os
import logging
import datetime

#get command line arguments

parser = argparse.ArgumentParser(description='Script to subset a VCF by excluding the variants within the regions defined by a BED file')

'''
Reseqtrack DB connection parameters
'''
parser.add_argument('--hostname', type=str, required=True, help='Hostname for ReseqTrack DB' )
parser.add_argument('--username', type=str, required=True, help='User for ReseqTrack DB' )
parser.add_argument('--port', type=int, required=True, help='Port number in the ReseqTrack DB' )
parser.add_argument('--pwd', type=str, help='PWD for the ReseqTrack DB' )
parser.add_argument('--db', type=str, required=True, help='DB name in the ReseqTrack DB' )
parser.add_argument('--type', type=str, required=True, help='Type of the new VCF file' )

parser.add_argument('--vcftools_folder', type=str, required=True, help='Folder containing the VCFtools binary' )
parser.add_argument('--bgzip_folder', type=str, required=True, help='Folder containing the bgzip binary')
parser.add_argument('--filename', type=str, required=True, help='Name (without the fullpath) of the VCF file that will be analysed. It assumes that the filename format is for example lc_bams.gatk.xxxx.vcf.gz, where lc_bams is the analysis group and gatk is the method used' )
parser.add_argument('--bed', type=str, required=True, help='BED file containing the coordinates to exclude' )
parser.add_argument('--outsuffix', type=str, required=True, help='Suffix for vcf output file. i.e. no_cms or no_offtarget' )
parser.add_argument('--outdir', type=str, required=True, help='Directory used to put the output files.' )

args = parser.parse_args()

if __name__ == '__main__':
    if os.path.isdir(args.outdir) == False:
        raise Exception("Output dir does not exist: %s"%args.outdir)
    
    hostname=args.hostname
    username=args.username
    db=args.db
    port=args.port
    pwd=args.pwd

    reseqdb = ReseqTrackDB(host=hostname,user=username,port=port,pwd=pwd,db=db)

    file=reseqdb.fetch_file_by_filename(args.filename)
    
    #constructing the out filename
    now = datetime.datetime.now().strftime('%Y%m%d')
    bits= os.path.basename(file.name).split('.')
    outprefix=bits[0]+"."+bits[1]+"."+args.outsuffix+"."+now

    log_filename="subset_vcf_%s.log"% outprefix

    logger = logging.getLogger("subset_vcf")
    logger.setLevel(logging.INFO)

    #  create the logging file handler
    fh = logging.FileHandler(log_filename)

    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    fh.setFormatter(formatter)

    # add handler to logger object
    logger.addHandler(fh)

    logger.info("Program started")

    vcfQC = VcfQC(vcf=file.path,bgzip_folder=args.bgzip_folder,vcftools_folder=args.vcftools_folder)
    vcffile=vcfQC.subset_vcf(bed=args.bed,outprefix=outprefix,outdir=args.outdir,create_index=True)

    f=File(path=vcffile,type=args.type,host_id=1,withdrawn=0)
    f.store(reseqdb,do_md5=True)
    
    logger.info("Done!.")

[((13, 9, 13, 137), 'argparse.ArgumentParser', 'argparse.ArgumentParser', (), '', False, 'import argparse\n'), ((44, 14, 44, 79), 'ReseqTrackDB.ReseqTrackDB', 'ReseqTrackDB', (), '', False, 'from ReseqTrackDB import ReseqTrackDB\n'), ((55, 13, 55, 44), 'logging.getLogger', 'logging.getLogger', ({(55, 31, 55, 43): '"""subset_vcf"""'}, {}), "('subset_vcf')", False, 'import logging\n'), ((59, 9, 59, 42), 'logging.FileHandler', 'logging.FileHandler', ({(59, 29, 59, 41): 'log_filename'}, {}), '(log_filename)', False, 'import logging\n'), ((61, 16, 61, 89), 'logging.Formatter', 'logging.Formatter', ({(61, 34, 61, 88): '"""%(asctime)s - %(name)s - %(levelname)s - %(message)s"""'}, {}), "('%(asctime)s - %(name)s - %(levelname)s - %(message)s')", False, 'import logging\n'), ((69, 12, 69, 100), 'VcfQC.VcfQC', 'VcfQC', (), '', False, 'from VcfQC import VcfQC\n'), ((72, 6, 72, 61), 'ReseqTrackDB.File', 'File', (), '', False, 'from ReseqTrackDB import File\n'), ((35, 7, 35, 33), 'os.path.isdir', 'os.path.isdir', ({(35, 21, 35, 32): 'args.outdir'}, {}), '(args.outdir)', False, 'import os\n'), ((49, 10, 49, 33), 'datetime.datetime.now', 'datetime.datetime.now', ({}, {}), '()', False, 'import datetime\n'), ((50, 10, 50, 37), 'os.path.basename', 'os.path.basename', ({(50, 27, 50, 36): 'file.name'}, {}), '(file.name)', False, 'import os\n')]