Example #1
0
def cli(ctx, bam, reference, variants, bed4_file, min_freq, mutation_db,
        reporting_threshold, output):
    rs = parse_references_from_fasta(reference)

    mapped_read_collection_arr = []
    for r in rs:
        # Create a MappedReadCollection object
        mapped_read_collection_arr.append(parse_mapped_reads_from_bam(r, bam))

    variants_obj = parse_nt_variants_from_vcf(variants, rs)

    # Mask the unconfident differences
    for mrc in mapped_read_collection_arr:
        mrc.mask_unconfident_differences(variants_obj)

    # Parse the genes from the gene file
    genes = parse_BED4_file(bed4_file, rs[0].name)

    # Determine which frames our genes are in
    frames = set()

    for gene in genes:
        frames.add(genes[gene]['frame'])

    # Create an AACensus object
    aa_census = AACensus(reference, mapped_read_collection_arr, genes, frames)

    # Create AAVar collection and print the aavf file
    aa_vars = AAVariantCollection.from_aacensus(aa_census)

    # Filter for mutant frequency
    aa_vars.filter('mf' + str(min_freq), 'freq<' + str(min_freq), True)

    # Build the mutation database
    mutation_db = MutationDB(mutation_db, genes)

    # Generate the mutation report
    if output:
        output.write(
            aa_vars.report_dr_mutations(mutation_db, reporting_threshold))
        output.close()
    else:
        click.echo(
            aa_vars.report_dr_mutations(mutation_db, reporting_threshold))
Example #2
0
def aavar(bam, reference, variants, genes_file, min_freq, mutation_db, output):
    rs = parse_references_from_fasta(reference)

    mapped_read_collection_arr = []
    for r in rs:
        # Create a MappedReadCollection object
        mapped_read_collection_arr.append(parse_mapped_reads_from_bam(r, bam))

    variants_obj = parse_nt_variants_from_vcf(variants, rs)

    # Mask the unconfident differences
    for mrc in mapped_read_collection_arr:
        mrc.mask_unconfident_differences(variants_obj)

    # Parse the genes from the gene file
    genes = parse_genes_file(genes_file, rs[0].name)

    # Determine which frames our genes are in
    frames = set()

    for gene in genes:
        frames.add(genes[gene]['frame'])

    # Create an AACensus object
    aa_census = AACensus(reference, mapped_read_collection_arr, genes, frames)

    # Create AAVar collection and print the hmcf file
    aa_vars = AAVariantCollection.from_aacensus(aa_census)

    # Filter for mutant frequency
    aa_vars.filter('mf0.01', 'freq<0.01', True)

    # Build the mutation database and update collection
    if mutation_db is not None:
        mutation_db = MutationDB(mutation_db, genes)
        aa_vars.apply_mutation_db(mutation_db)

    if output:
        output.write(aa_vars.to_hmcf_file(CONFIDENT))
    else:
        click.echo(aa_vars.to_hmcf_file(CONFIDENT))
Example #3
0
def codonvar(bam, reference, offset, bed4_file, variants, error_rate, output):
    rs = parse_references_from_fasta(reference)
    mapped_read_collection_arr = []

    # Create a MappedReadCollection object
    for r in rs:
        mapped_read_collection_arr.append(parse_mapped_reads_from_bam(r, bam))

    if variants:
        variants_obj = parse_nt_variants_from_vcf(variants, rs)
    else:
        variants = NTVariantCollection.from_mapped_read_collections(
            error_rate, rs, *mapped_read_collection_arr)
        variants.filter('q30', 'QUAL<30', True)
        variants.filter('ac5', 'AC<5', True)
        variants.filter('dp100', 'DP<100', True)
        variants_obj = variants

    # Mask the unconfident differences
    for mrc in mapped_read_collection_arr:
        mrc.mask_unconfident_differences(variants_obj)

    # Parse the genes from the gene file
    genes = parse_BED4_file(bed4_file, rs[0].name)

    # Determine which frames our genes are in
    frames = set()

    for gene in genes:
        frames.add(genes[gene]['frame'])

    aa_census = AACensus(reference, mapped_read_collection_arr, genes, frames)

    codon_variants = CodonVariantCollection.from_aacensus(aa_census)

    if output:
        output.write(codon_variants.to_csv_file(offset))
        output.close()
    else:
        click.echo(codon_variants.to_csv_file(offset))
Example #4
0
    def setup(self):
        reference = TEST_PATH + "/data/hxb2_pol.fas"
        bam = TEST_PATH + "/data/align.bam"
        genes_file = TEST_PATH + "/data/hxb2_pol.bed"
        mutation_db = TEST_PATH + "/data/mutation_db.tsv"
        min_freq = 0.01

        rs = parse_references_from_fasta(reference)

        mapped_read_collection_arr = []
        for r in rs:
            # Create a MappedReadCollection object
            mapped_read_collection_arr.append(
                parse_mapped_reads_from_bam(r, bam))

        variants_obj = parse_nt_variants_from_vcf(VARIANTS_FILE, rs)

        # Mask the unconfident differences
        for mrc in mapped_read_collection_arr:
            mrc.mask_unconfident_differences(variants_obj)

        # Parse the genes from the gene file
        genes = parse_genes_file(genes_file, rs[0].name)

        # Determine which frames our genes are in
        frames = set()

        for gene in genes:
            frames.add(genes[gene]['frame'])

        # Create an AACensus object
        aa_census = AACensus(reference, mapped_read_collection_arr, genes,
                             frames)

        # Find the AA mutations
        self.aa_collection = AAVariantCollection.from_aacensus(aa_census)

        # Build the mutation database
        self.mutation_db = MutationDB(mutation_db, genes)
Example #5
0
def aavar(bam, reference, bed4_file, variants, mutation_db, min_freq,
          error_rate, output):
    rs = parse_references_from_fasta(reference)

    mapped_read_collection_arr = []
    for r in rs:
        # Create a MappedReadCollection object
        mapped_read_collection_arr.append(parse_mapped_reads_from_bam(r, bam))

    if variants:
        variants_obj = parse_nt_variants_from_vcf(variants, rs)
    else:
        variants = NTVariantCollection.from_mapped_read_collections(
            error_rate, rs, *mapped_read_collection_arr)
        variants.filter('q30', 'QUAL<30', True)
        variants.filter('ac5', 'AC<5', True)
        variants.filter('dp100', 'DP<100', True)
        variants_obj = variants

    # Mask the unconfident differences
    for mrc in mapped_read_collection_arr:
        mrc.mask_unconfident_differences(variants_obj)

    # Parse the genes from the gene file
    genes = parse_BED4_file(bed4_file, rs[0].name)

    # Determine which frames our genes are in
    frames = set()

    for gene in genes:
        frames.add(genes[gene]['frame'])

    # Create an AACensus object
    aa_census = AACensus(reference, mapped_read_collection_arr, genes, frames)

    # Create AAVar collection and print the aavf file
    aa_vars = AAVariantCollection.from_aacensus(aa_census)

    # Filter for mutant frequency
    aa_vars.filter('mf0.01', 'freq<0.01', True)

    # Build the mutation database and update collection
    if mutation_db is not None:
        mutation_db = MutationDB(mutation_db, genes)
        aa_vars.apply_mutation_db(mutation_db)

    aavf_obj = aa_vars.to_aavf_obj("aavar", os.path.basename(reference),
                                   CONFIDENT)
    records = list(aavf_obj)

    if output:
        writer = parser.Writer(output, aavf_obj)
    else:
        writer = parser.Writer(sys.stdout, aavf_obj)

    for record in records:
        writer.write_record(record)

    if output:
        output.close

    writer.close()
    def test_valid_vcf_file(self):
        """Tests to ensure that valid vcf files are parsed properly."""

        reference = TEST_PATH + \
            "/data/hxb2_pol.fas"
        bam = TEST_PATH + "/data/align.bam"

        rs = parse_references_from_fasta(reference)

        mapped_read_collection_arr = []
        for r in rs:
            # Create a MappedReadCollection object
            mapped_read_collection_arr.append(
                parse_mapped_reads_from_bam(r, bam))

        variants_obj = NTVariantCollection(rs)

        for i in range(0, 20):
            variant = NTVariant(chrom="hxb2_pol",
                                pos=i,
                                id=".",
                                ref='a',
                                alt='t',
                                qual="50",
                                filter="PASS",
                                info={
                                    "DP": "300",
                                    "AC": "1",
                                    "AF": "0.0025"
                                })

            variants_obj.variants["hxb2_pol"][i]['t'] = variant

        #Create a valid vcf file
        valid_vcf_file = TEST_PATH + "/data/valid_vcf_file.vcf"

        with open(valid_vcf_file, "w+") as f:
            f.write(
                "##fileformat=VCFv4.2\n"
                "##fileDate=20171005\n"
                "##source=quasitools\n"
                "##INFO=<ID=DP,Number=1,Type=Integer,Description=\"Total Depth\">\n"
                "##INFO=<ID=AC,Number=A,Type=Integer,Description=\"Allele Count\">\n"
                "##INFO=<ID=AF,Number=A,Type=Float,Description=\"Allele Frequency\">\n"
                "##FILTER=<ID=q30,Description=\"Quality below 30\">\n"
                "##FILTER=<ID=dp100,Description=\"Read depth below 100\">\n"
                "##FILTER=<ID=ac5,Description=\"Allele count below 5\">\n"
                "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO")

            for rid in variants_obj.variants:
                for pos in variants_obj.variants[rid]:
                    for alt in variants_obj.variants[rid][pos]:
                        variant = variants_obj.variants[rid][pos][alt]
                        f.write("\n%s\t%i\t%s\t%s\t%s\t%s\t%s" %
                                (variant.chrom, int(
                                    variant.pos), variant.id, variant.ref,
                                 variant.alt, variant.qual, variant.filter))
                        f.write(
                            "\tDP=%i;AC=%i;AF=%0.4f" %
                            (int(variant.info["DP"]), int(variant.info["AC"]),
                             float(variant.info["AF"])))

        parsed_nt_var = parse_nt_variants_from_vcf(valid_vcf_file, rs)

        # Check equality of parsed NTVariantCollection vs. the valid NTVariantCollection
        for rid in parsed_nt_var.variants:
            for pos in parsed_nt_var.variants[rid]:
                for alt in parsed_nt_var.variants[rid][pos]:
                    parsed_variant = parsed_nt_var.variants[rid][pos][alt]
                    variant = variants_obj.variants[rid][pos][alt]

                    assert parsed_variant.chrom == variant.chrom
                    assert parsed_variant.pos == variant.pos
                    assert parsed_variant.id == variant.id
                    assert parsed_variant.ref == variant.ref
                    assert parsed_variant.alt == variant.alt
                    assert parsed_variant.qual == variant.qual
                    assert parsed_variant.filter == variant.filter
                    assert parsed_variant.info["DP"] == variant.info["DP"]
                    assert parsed_variant.info["AC"] == variant.info["AC"]
                    assert parsed_variant.info["AF"] == variant.info["AF"]

        os.remove(valid_vcf_file)