workflow/scripts/assemble_visualization_webpage.py
def main():
    """Parse command line, run default functions."""
    # parse command line
    parser = argparse.ArgumentParser(description="Generate HTML visual report from VCF variants",
                                     epilog="at minimum, either provide `-v` and `-c` or provide `-w`")
    parser.add_argument('-f', '--consensus', metavar='FASTA', required=False,
                        type=str, dest='consensus_file', help="consensus sequence of this sample")
    parser.add_argument('-c', '--coverage', metavar='TSV', required=False,
                        default='variants/coverage.tsv',
                        type=str, dest='coverage_file', help="global coverage table")
    parser.add_argument('-v', '--vcf', metavar='VCF', required=False,
                        type=str, dest='vcf_file', help="VCF containing the SNPs to be visualised")
    parser.add_argument('-g', '--gff', metavar='DIR', required=False,
                        type=str, dest='gff_directory', help="directory containing GFF annotations")
    parser.add_argument('-p', '--primers', metavar='CSV', required=False,
                        type=str, dest='primers_file', help="table with primers")
    parser.add_argument('-m', '--metainfo', metavar='YAML', required=False,
                        type=str, dest='metainfo_yaml', help="metainformation for the GFF and primers")
    parser.add_argument('-x', '--snv_calling_template', metavar='HTML', required=False,
                        default=f'{os.path.dirname(__file__)}/snv_calling_visualization.html',
                        type=str, dest='html_template_snv', help="HTML template used to generate visual report for snv calling")
    parser.add_argument('-y', '--alignment_template', metavar='HTML', required=False,
                        default=f'{os.path.dirname(__file__)}/alignment_visualization.html',
                        type=str, dest='html_template_alignment', help="HTML template used to generate visual report for alignments")
    parser.add_argument('-s', '--html_out_snv_calling', metavar='HTML', required=False,
                        type=str, dest='html_out_snv_calling', help="produced HTML report for SNV calling")
    parser.add_argument('-a', '--html_out_alignment', metavar='HTML', required=False,
                        type=str, dest='html_out_alignment', help="produced HTML report for phylogeny and read alignment")
    parser.add_argument('-w', '--wildcards', metavar='SAMPLE/DATE', required=False,
                        type=str, dest='wildcards_dataset', help="sample's two-level directory hierarchy prefix")
    parser.add_argument('-r', '--reference', metavar='FASTA', required=False,
                        default='variants/cohort_consensus.fasta',
                        type=str, dest='reference_file', help="reference against which SNVs were called (e.g.: cohort's consensus)")
    parser.add_argument('-n', '--nwk', metavar='NWK', required=False,
                        type=str, dest='nwk_file', help="phylogenetic tree in NWK format")
    parser.add_argument('-u', '--reference_uri_file', metavar='FILE', required=False,
                        type=str, dest='reference_uri_file', help="reference file uri")
    parser.add_argument('-b', '--bam_uri_file', metavar='FILE', required=False,
                        type=str, dest='bam_uri_file', help="bam file uri")

    args = parser.parse_args()

    # defaults which can be guess from one another
    if args.vcf_file == None:  # e.g.: samples/140074_395_D02/20200615_J6NRK/variants/SNVs/snvs.vcf
        assert args.wildcards_dataset != None, 'cannot automatically find VCF without wildcards'
        args.vcf_file = os.path.join(
            args.wildcards_dataset, 'variants', 'SNVs', 'snvs.vcf')

    if args.consensus_file == None:  # e.g.: samples/140074_395_D02/20200615_J6NRK/references/ref_majority.fasta
        assert args.wildcards_dataset != None, 'cannot automatically find consensus without wildcards'
        args.consensus_file = os.path.join(
            args.wildcards_dataset, 'references', 'ref_majority.fasta')

    if args.wildcards_dataset == None:
        assert args.vcf_file != None and args.consensus != None, 'cannot deduce wilcards without a consensus and a vcf'
        try1 = '/'.join(os.path.normpath(args.vcf_file)
                        .split(os.path.sep)[-5:-3])
        try2 = '/'.join(os.path.normpath(args.consensus_file)
                        .split(os.path.sep)[-4:-2])
        assert try1 == try2, f'cannot deduce wildcards automatically from <{args.vcf_file}> and <{args.consensus_file}>, please specify explicitly using `--wirdcards`'
        args.wildcards_dataset = try1

    if args.html_out_snv_calling == None:
        args.html_out_snv_calling = os.path.join(
            args.wildcards_dataset, 'visualization', 'snv_calling.html')

    if args.html_out_alignment == None:
        args.html_out_alignment = os.path.join(
            args.wildcards_dataset, 'visualization', 'alignment.html')

    # check mandatory files exist
    for n, f in {'vcf': args.vcf_file,
                 'consensus': args.consensus_file,
                 'coverage': args.coverage_file,
                 'snv_calling_template': args.html_template_snv,
                 'alignment_template': args.html_template_alignment,
                 }.items():
        if not os.path.exists(f):
            parser.error(f"{n} file <{f}> does not exist!")

    # check optional files exist if specified
    for n, f in {'gff': args.gff_directory,
                 'primers': args.primers_file,
                 'metainfo': args.metainfo_yaml,
                 }.items():
        if f and not os.path.exists(f):
            parser.error(f"{n} file <{f}> does not exist!")

    if args.reference_uri_file == None: # e.g.: samples/140074_395_D02/20200615_J6NRK/visualization/reference_uri_file
        assert args.wildcards_dataset != None, 'cannot automatically find reference_uri_file without wildcards'
        args.reference_uri_file = os.path.join(
            args.wildcards_dataset, 'visualization', 'reference_uri_file')

    if args.bam_uri_file == None:  # e.g.: samples/140074_395_D02/20200615_J6NRK/visualization/bam_uri_file
        assert args.wildcards_dataset != None, 'cannot automatically find bam_uri_file without wildcards'
        args.bam_uri_file = os.path.join(
            args.wildcards_dataset, 'visualization', 'bam_uri_file')

    # parse the sample name
    path_components = os.path.normpath(args.wildcards_dataset).split(os.path.sep)
    sample_name = '/'.join(path_components[-2:])

    # run the visual report generator for SNV calling
    assemble_snv_calling_visualization_webpage( 
        sample_name,   
        consensus_file = args.consensus_file,
        coverage_file = args.coverage_file,
        vcf_file = args.vcf_file,
        gff_directory = args.gff_directory,
        primers_file = args.primers_file,
        html_file_in = args.html_template_snv,
        html_file_out = args.html_out_snv_calling,
        reference_file = args.reference_file,
        metainfo_yaml = args.metainfo_yaml)

    # run the visual report generator for read alignment and phylogeny
    
    assemble_alignment_visualization_webpage(
        sample_name,
        nwk_file = args.nwk_file,
        reference_uri_file = args.reference_uri_file,
        bam_uri_file = args.bam_uri_file,
        html_file_in = args.html_template_alignment,
        html_file_out = args.html_out_alignment)
Parse command line, run default functions.
Parse command line, run default functions.
Parse command line run default functions .
def main():
    parser = argparse.ArgumentParser(description="Generate HTML visual report from VCF variants",
                                     epilog="at minimum, either provide `-v` and `-c` or provide `-w`")
    parser.add_argument('-f', '--consensus', metavar='FASTA', required=False,
                        type=str, dest='consensus_file', help="consensus sequence of this sample")
    parser.add_argument('-c', '--coverage', metavar='TSV', required=False,
                        default='variants/coverage.tsv',
                        type=str, dest='coverage_file', help="global coverage table")
    parser.add_argument('-v', '--vcf', metavar='VCF', required=False,
                        type=str, dest='vcf_file', help="VCF containing the SNPs to be visualised")
    parser.add_argument('-g', '--gff', metavar='DIR', required=False,
                        type=str, dest='gff_directory', help="directory containing GFF annotations")
    parser.add_argument('-p', '--primers', metavar='CSV', required=False,
                        type=str, dest='primers_file', help="table with primers")
    parser.add_argument('-m', '--metainfo', metavar='YAML', required=False,
                        type=str, dest='metainfo_yaml', help="metainformation for the GFF and primers")
    parser.add_argument('-x', '--snv_calling_template', metavar='HTML', required=False,
                        default=f'{os.path.dirname(__file__)}/snv_calling_visualization.html',
                        type=str, dest='html_template_snv', help="HTML template used to generate visual report for snv calling")
    parser.add_argument('-y', '--alignment_template', metavar='HTML', required=False,
                        default=f'{os.path.dirname(__file__)}/alignment_visualization.html',
                        type=str, dest='html_template_alignment', help="HTML template used to generate visual report for alignments")
    parser.add_argument('-s', '--html_out_snv_calling', metavar='HTML', required=False,
                        type=str, dest='html_out_snv_calling', help="produced HTML report for SNV calling")
    parser.add_argument('-a', '--html_out_alignment', metavar='HTML', required=False,
                        type=str, dest='html_out_alignment', help="produced HTML report for phylogeny and read alignment")
    parser.add_argument('-w', '--wildcards', metavar='SAMPLE/DATE', required=False,
                        type=str, dest='wildcards_dataset', help="sample's two-level directory hierarchy prefix")
    parser.add_argument('-r', '--reference', metavar='FASTA', required=False,
                        default='variants/cohort_consensus.fasta',
                        type=str, dest='reference_file', help="reference against which SNVs were called (e.g.: cohort's consensus)")
    parser.add_argument('-n', '--nwk', metavar='NWK', required=False,
                        type=str, dest='nwk_file', help="phylogenetic tree in NWK format")
    parser.add_argument('-u', '--reference_uri_file', metavar='FILE', required=False,
                        type=str, dest='reference_uri_file', help="reference file uri")
    parser.add_argument('-b', '--bam_uri_file', metavar='FILE', required=False,
                        type=str, dest='bam_uri_file', help="bam file uri")
    args = parser.parse_args()
    if args.vcf_file == None:  
        assert args.wildcards_dataset != None, 'cannot automatically find VCF without wildcards'
        args.vcf_file = os.path.join(
            args.wildcards_dataset, 'variants', 'SNVs', 'snvs.vcf')
    if args.consensus_file == None:  
        assert args.wildcards_dataset != None, 'cannot automatically find consensus without wildcards'
        args.consensus_file = os.path.join(
            args.wildcards_dataset, 'references', 'ref_majority.fasta')
    if args.wildcards_dataset == None:
        assert args.vcf_file != None and args.consensus != None, 'cannot deduce wilcards without a consensus and a vcf'
        try1 = '/'.join(os.path.normpath(args.vcf_file)
                        .split(os.path.sep)[-5:-3])
        try2 = '/'.join(os.path.normpath(args.consensus_file)
                        .split(os.path.sep)[-4:-2])
        assert try1 == try2, f'cannot deduce wildcards automatically from <{args.vcf_file}> and <{args.consensus_file}>, please specify explicitly using `--wirdcards`'
        args.wildcards_dataset = try1
    if args.html_out_snv_calling == None:
        args.html_out_snv_calling = os.path.join(
            args.wildcards_dataset, 'visualization', 'snv_calling.html')
    if args.html_out_alignment == None:
        args.html_out_alignment = os.path.join(
            args.wildcards_dataset, 'visualization', 'alignment.html')
    for n, f in {'vcf': args.vcf_file,
                 'consensus': args.consensus_file,
                 'coverage': args.coverage_file,
                 'snv_calling_template': args.html_template_snv,
                 'alignment_template': args.html_template_alignment,
                 }.items():
        if not os.path.exists(f):
            parser.error(f"{n} file <{f}> does not exist!")
    for n, f in {'gff': args.gff_directory,
                 'primers': args.primers_file,
                 'metainfo': args.metainfo_yaml,
                 }.items():
        if f and not os.path.exists(f):
            parser.error(f"{n} file <{f}> does not exist!")
    if args.reference_uri_file == None: 
        assert args.wildcards_dataset != None, 'cannot automatically find reference_uri_file without wildcards'
        args.reference_uri_file = os.path.join(
            args.wildcards_dataset, 'visualization', 'reference_uri_file')
    if args.bam_uri_file == None:  
        assert args.wildcards_dataset != None, 'cannot automatically find bam_uri_file without wildcards'
        args.bam_uri_file = os.path.join(
            args.wildcards_dataset, 'visualization', 'bam_uri_file')
    path_components = os.path.normpath(args.wildcards_dataset).split(os.path.sep)
    sample_name = '/'.join(path_components[-2:])
    assemble_snv_calling_visualization_webpage( 
        sample_name,   
        consensus_file = args.consensus_file,
        coverage_file = args.coverage_file,
        vcf_file = args.vcf_file,
        gff_directory = args.gff_directory,
        primers_file = args.primers_file,
        html_file_in = args.html_template_snv,
        html_file_out = args.html_out_snv_calling,
        reference_file = args.reference_file,
        metainfo_yaml = args.metainfo_yaml)
    assemble_alignment_visualization_webpage(
        sample_name,
        nwk_file = args.nwk_file,
        reference_uri_file = args.reference_uri_file,
        bam_uri_file = args.bam_uri_file,
        html_file_in = args.html_template_alignment,
        html_file_out = args.html_out_alignment)
def main ( ) : parser = argparse . ArgumentParser ( description = "Generate HTML visual report from VCF variants" , epilog = "at minimum, either provide `-v` and `-c` or provide `-w`" ) parser . add_argument ( '-f' , '--consensus' , metavar = 'FASTA' , required = False , type = str , dest = 'consensus_file' , help = "consensus sequence of this sample" ) parser . add_argument ( '-c' , '--coverage' , metavar = 'TSV' , required = False , default = 'variants/coverage.tsv' , type = str , dest = 'coverage_file' , help = "global coverage table" ) parser . add_argument ( '-v' , '--vcf' , metavar = 'VCF' , required = False , type = str , dest = 'vcf_file' , help = "VCF containing the SNPs to be visualised" ) parser . add_argument ( '-g' , '--gff' , metavar = 'DIR' , required = False , type = str , dest = 'gff_directory' , help = "directory containing GFF annotations" ) parser . add_argument ( '-p' , '--primers' , metavar = 'CSV' , required = False , type = str , dest = 'primers_file' , help = "table with primers" ) parser . add_argument ( '-m' , '--metainfo' , metavar = 'YAML' , required = False , type = str , dest = 'metainfo_yaml' , help = "metainformation for the GFF and primers" ) parser . add_argument ( '-x' , '--snv_calling_template' , metavar = 'HTML' , required = False , default = f'{os.path.dirname(__file__)}/snv_calling_visualization.html' , type = str , dest = 'html_template_snv' , help = "HTML template used to generate visual report for snv calling" ) parser . add_argument ( '-y' , '--alignment_template' , metavar = 'HTML' , required = False , default = f'{os.path.dirname(__file__)}/alignment_visualization.html' , type = str , dest = 'html_template_alignment' , help = "HTML template used to generate visual report for alignments" ) parser . add_argument ( '-s' , '--html_out_snv_calling' , metavar = 'HTML' , required = False , type = str , dest = 'html_out_snv_calling' , help = "produced HTML report for SNV calling" ) parser . add_argument ( '-a' , '--html_out_alignment' , metavar = 'HTML' , required = False , type = str , dest = 'html_out_alignment' , help = "produced HTML report for phylogeny and read alignment" ) parser . add_argument ( '-w' , '--wildcards' , metavar = 'SAMPLE/DATE' , required = False , type = str , dest = 'wildcards_dataset' , help = "sample's two-level directory hierarchy prefix" ) parser . add_argument ( '-r' , '--reference' , metavar = 'FASTA' , required = False , default = 'variants/cohort_consensus.fasta' , type = str , dest = 'reference_file' , help = "reference against which SNVs were called (e.g.: cohort's consensus)" ) parser . add_argument ( '-n' , '--nwk' , metavar = 'NWK' , required = False , type = str , dest = 'nwk_file' , help = "phylogenetic tree in NWK format" ) parser . add_argument ( '-u' , '--reference_uri_file' , metavar = 'FILE' , required = False , type = str , dest = 'reference_uri_file' , help = "reference file uri" ) parser . add_argument ( '-b' , '--bam_uri_file' , metavar = 'FILE' , required = False , type = str , dest = 'bam_uri_file' , help = "bam file uri" ) args = parser . parse_args ( ) if args . vcf_file == None : assert args . wildcards_dataset != None , 'cannot automatically find VCF without wildcards' args . vcf_file = os . path . join ( args . wildcards_dataset , 'variants' , 'SNVs' , 'snvs.vcf' ) if args . consensus_file == None : assert args . wildcards_dataset != None , 'cannot automatically find consensus without wildcards' args . consensus_file = os . path . join ( args . wildcards_dataset , 'references' , 'ref_majority.fasta' ) if args . wildcards_dataset == None : assert args . vcf_file != None and args . consensus != None , 'cannot deduce wilcards without a consensus and a vcf' try1 = '/' . join ( os . path . normpath ( args . vcf_file ) . split ( os . path . sep ) [ - 5 : - 3 ] ) try2 = '/' . join ( os . path . normpath ( args . consensus_file ) . split ( os . path . sep ) [ - 4 : - 2 ] ) assert try1 == try2 , f'cannot deduce wildcards automatically from <{args.vcf_file}> and <{args.consensus_file}>, please specify explicitly using `--wirdcards`' args . wildcards_dataset = try1 if args . html_out_snv_calling == None : args . html_out_snv_calling = os . path . join ( args . wildcards_dataset , 'visualization' , 'snv_calling.html' ) if args . html_out_alignment == None : args . html_out_alignment = os . path . join ( args . wildcards_dataset , 'visualization' , 'alignment.html' ) for n , f in { 'vcf' : args . vcf_file , 'consensus' : args . consensus_file , 'coverage' : args . coverage_file , 'snv_calling_template' : args . html_template_snv , 'alignment_template' : args . html_template_alignment , } . items ( ) : if not os . path . exists ( f ) : parser . error ( f"{n} file <{f}> does not exist!" ) for n , f in { 'gff' : args . gff_directory , 'primers' : args . primers_file , 'metainfo' : args . metainfo_yaml , } . items ( ) : if f and not os . path . exists ( f ) : parser . error ( f"{n} file <{f}> does not exist!" ) if args . reference_uri_file == None : assert args . wildcards_dataset != None , 'cannot automatically find reference_uri_file without wildcards' args . reference_uri_file = os . path . join ( args . wildcards_dataset , 'visualization' , 'reference_uri_file' ) if args . bam_uri_file == None : assert args . wildcards_dataset != None , 'cannot automatically find bam_uri_file without wildcards' args . bam_uri_file = os . path . join ( args . wildcards_dataset , 'visualization' , 'bam_uri_file' ) path_components = os . path . normpath ( args . wildcards_dataset ) . split ( os . path . sep ) sample_name = '/' . join ( path_components [ - 2 : ] ) assemble_snv_calling_visualization_webpage ( sample_name , consensus_file = args . consensus_file , coverage_file = args . coverage_file , vcf_file = args . vcf_file , gff_directory = args . gff_directory , primers_file = args . primers_file , html_file_in = args . html_template_snv , html_file_out = args . html_out_snv_calling , reference_file = args . reference_file , metainfo_yaml = args . metainfo_yaml ) assemble_alignment_visualization_webpage ( sample_name , nwk_file = args . nwk_file , reference_uri_file = args . reference_uri_file , bam_uri_file = args . bam_uri_file , html_file_in = args . html_template_alignment , html_file_out = args . html_out_alignment )
Parse command line, run default functions.
Parse command line run default functions .
"""Parse command line, run default functions.""" # parse command line # defaults which can be guess from one another # e.g.: samples/140074_395_D02/20200615_J6NRK/variants/SNVs/snvs.vcf # e.g.: samples/140074_395_D02/20200615_J6NRK/references/ref_majority.fasta # check mandatory files exist # check optional files exist if specified # e.g.: samples/140074_395_D02/20200615_J6NRK/visualization/reference_uri_file # e.g.: samples/140074_395_D02/20200615_J6NRK/visualization/bam_uri_file # parse the sample name # run the visual report generator for SNV calling # run the visual report generator for read alignment and phylogeny