def test_report_dnds_values(self):
        valid_dnds_report = TEST_PATH + "/data/output/dnds_report.csv"
        csv = TEST_PATH + "/data/output/mutant_types.csv"
        ref_seq = self.references[0].seq
        codon_variants = parse_codon_variants(csv, self.references)

        # Read from file and make sure there are no empty lines
        with open(valid_dnds_report, "r") as input:
            valid_report = input.read()

        # Sort and filter for comparison
        valid_dnds_values = sorted(filter(None, valid_report.split("\n")))

        # Create the report string
        test_report = codon_variants.report_dnds_values(ref_seq, self.offset)

        # Split into lines and sort
        test_values = sorted(test_report.split("\n"))

        assert len(valid_dnds_values) == len(test_values)

        # Compare each line in the test report to the valid report
        for pos in range(0, len(valid_dnds_values)):
            if valid_dnds_values[pos][0:1] != "#":
                assert valid_dnds_values[pos] == \
                    test_values[pos]
Example #2
0
    def setup(self):
        csv = TEST_PATH + "/data/output/mutant_types.csv"
        reference = TEST_PATH + "/data/hxb2_pol.fas"
        self.offset = 1269

        rs = parse_references_from_fasta(reference)
        self.ref_seq = rs[0].seq

        self.codon_variants = parse_codon_variants(csv, rs)
Example #3
0
def cli(ctx, csv, reference, offset, output):
    rs = parse_references_from_fasta(reference)
    ref_seq = rs[0].seq

    codon_variants = parse_codon_variants(csv, rs)

    if output:
        output.write(codon_variants.report_dnds_values(ref_seq, offset))
    else:
        click.echo(codon_variants.report_dnds_values(ref_seq, offset))
Example #4
0
    def test_valid_csv_file(self):
        """Tests to make sure that a valid codon variant csv file is properly
        parsed into a CodonVariantCollection object.
        """

        reference = TEST_PATH + "/data/hxb2_pol.fas"
        rs = parse_references_from_fasta(reference)

        var_obj = CodonVariantCollection(rs)

        for i in range(0, 30):
            variant = CodonVariant(chrom="hxb2_pol",
                                   pos=i,
                                   gene="gag",
                                   nt_start_gene=1309 + i,
                                   nt_end_gene=2841 + i,
                                   nt_start=2077 + i,
                                   nt_end=2079 + i,
                                   ref_codon="ata",
                                   mutant_codon="aAa",
                                   ref_aa="I",
                                   mutant_aa="K",
                                   coverage=563 + i,
                                   mutant_freq=1.60 + i,
                                   mutant_type="S",
                                   ns_count=1.0000,
                                   s_count=1.5000)

            pos = int(variant.nt_start) - int(variant.nt_start_gene)
            var_obj.variants["gag"][pos]["aAa"] = variant

        valid_csv = TEST_PATH + "/data/valid_csv.csv"

        with open(valid_csv, "w+") as f:
            f.write("#gene,nt position (gene),nt start position,"
                    "nt end position,ref codon,mutant codon,ref AA,mutant AA,"
                    "coverage,mutant frequency,mutant type,NS count,S count")

            for gene in var_obj.variants:
                for pos in var_obj.variants[gene]:
                    for codon in var_obj.variants[gene][pos]:
                        variant = var_obj.variants[gene][pos][codon]

                        f.write(
                            "%s,%i-%i,%i,%i,%s,%s,%s,%s,%i,%.2f,%s,%0.4f,%0.4f\n"
                            % (variant.gene, variant.nt_start_gene,
                               variant.nt_end_gene, variant.nt_start,
                               variant.nt_end, variant.ref_codon,
                               variant.mutant_codon, variant.ref_aa,
                               variant.mutant_aa, variant.coverage,
                               variant.mutant_freq, variant.mutant_type,
                               variant.ns_count, variant.s_count))

        parsed_codon_variants = parse_codon_variants(valid_csv, rs)

        for gene in parsed_codon_variants.variants:
            for pos in parsed_codon_variants.variants[gene]:
                for codon in parsed_codon_variants.variants[gene][pos]:
                    parsed_variant = parsed_codon_variants.variants[gene][pos][
                        codon]
                    variant = var_obj.variants[gene][pos][codon]

                    assert parsed_variant.chrom == variant.chrom
                    assert parsed_variant.nt_start_gene == variant.nt_start_gene
                    assert parsed_variant.nt_end_gene == variant.nt_end_gene
                    assert parsed_variant.nt_start == variant.nt_start
                    assert parsed_variant.nt_end == variant.nt_end
                    assert parsed_variant.ref_codon == variant.ref_codon
                    assert parsed_variant.mutant_codon == variant.mutant_codon
                    assert parsed_variant.ref_aa == variant.ref_aa
                    assert parsed_variant.mutant_aa == variant.mutant_aa
                    assert parsed_variant.coverage == variant.coverage
                    assert parsed_variant.mutant_freq == variant.mutant_freq
                    assert parsed_variant.mutant_type == variant.mutant_type
                    assert parsed_variant.ns_count == variant.ns_count
                    assert parsed_variant.s_count == variant.s_count

        os.remove(valid_csv)