Beispiel #1
0
 def test_open_individual_with_mnvs(self):
     ''' test that open_individual works with MNVs
     '''
     
     vcf = make_vcf_header()
     vcf.append(make_vcf_line(pos=1, cq='splice_region_variant',
         extra='HGNC=ATRX;MAX_AF=0.0001'))
     vcf.append(make_vcf_line(pos=2, cq='missense_variant',
         extra='HGNC=ATRX;MAX_AF=0.0001'))
     
     path = os.path.join(self.temp_dir, "temp.vcf.gz")
     write_gzipped_vcf(path, vcf)
     
     person = Person('fam_id', 'sample', 'dad', 'mom', 'F', '2', path)
     
     args = {'chrom': "1", 'position': 1, 'id': ".", 'ref': "G", 'alts': "T",
         'filter': "PASS", 'info': "CQ=splice_region_variant;HGNC=ATRX;MAX_AF=0.0001",
         'format': "DP:GT", 'sample': "50:0/1", 'gender': "female",
         'mnv_code': 'modified_protein_altering_mnv', 'qual': '1000'}
     var1 = SNV(**args)
     
     args['position'] = 2
     args['mnv_code'] = None
     args['info'] = "CQ=missense_variant;HGNC=ATRX;MAX_AF=0.0001"
     var2 = SNV(**args)
     
     # by default only one variant passes
     self.assertEqual(open_individual(person), [var2])
     
     # if we include MNVs, then the passing variants swap
     self.assertEqual(open_individual(person,
         mnvs={('1', 1): 'modified_protein_altering_mnv',
         ('1', 2): 'modified_synonymous_mnv'}), [var1])
Beispiel #2
0
 def test_open_individual(self):
     ''' test that open_individual() works correctly
     '''
     
     # missing individual returns empty list
     self.assertEqual(open_individual(None), [])
     
     vcf = make_vcf_header()
     vcf.append(make_vcf_line(pos=1, extra='HGNC=TEST;MAX_AF=0.0001'))
     vcf.append(make_vcf_line(pos=2, extra='HGNC=ATRX;MAX_AF=0.0001'))
     
     path = os.path.join(self.temp_dir, "temp.vcf")
     write_temp_vcf(path, vcf)
     
     person = Person('fam_id', 'sample', 'dad', 'mom', 'F', '2', path)
     
     var1 = SNV(chrom="1", position=1, id=".", ref="G", alts="T",
         qual='1000', filter="PASS", info="CQ=missense_variant;HGNC=TEST;MAX_AF=0.0001",
         format="DP:GT", sample="50:0/1", gender="female", mnv_code=None)
     var2 = SNV(chrom="1", position=2, id=".", ref="G", alts="T",
         qual='1000', filter="PASS", info="CQ=missense_variant;HGNC=ATRX;MAX_AF=0.0001",
         format="DP:GT", sample="50:0/1", gender="female", mnv_code=None)
     
     self.assertEqual(open_individual(person), [var2])
     
     # define a set of variants to automatically pass, and check that these
     # variants pass.
     child_keys = set([('1', 1), ('1', 2)])
     self.assertEqual(open_individual(person,
         child_variants=child_keys), [var1, var2])
    def test_add_single_variant(self):
        """ test that add_single_variant() works correctly
        """

        # the sub-functions are all tested elsewhere, this test merely checks
        # that valid variants are added to the variants list, and invalid
        # variants are passed over without being added to the variants list

        # set up an autosomal variant
        line = ["1", "100", ".", "T", "G", "1000", "PASS", ".", "GT", "0/1"]
        gender = "M"
        variant = SNV(*line[:6])

        # check that the variant is added to the variant list
        variants = []
        self.vcf_loader.add_single_variant(variants, variant, gender, line)
        self.assertEqual(variants, [variant])

        # set up an X-chrom male het
        line = ["X", "100", ".", "T", "G", "1000", "PASS", ".", "GT", "0/1"]
        variant = SNV(*line[:6])

        # check that the X-chrom male het is not added to the variant list
        variants = []
        self.vcf_loader.add_single_variant(variants, variant, gender, line)
        self.assertEqual(variants, [])
    def test_load_trio(self):
        ''' test that load_trio() works correctly
        '''
        def make_vcf(person):
            # make a VCF, where one line would pass the default filtering
            vcf = make_vcf_header()
            vcf.append(make_vcf_line(pos=1, extra='HGNC=TEST;MAX_AF=0.0001'))
            vcf.append(make_vcf_line(pos=2, extra='HGNC=ATRX;MAX_AF=0.0001'))

            path = os.path.join(self.temp_dir, "{}.vcf.gz".format(person))
            write_gzipped_vcf(path, vcf)
            return path

        child_path = make_vcf('child')
        mother_path = make_vcf('mother')
        father_path = make_vcf('father')

        family = Family('fam_id')
        family.add_child('sample', 'mother_id', 'father_id', 'female', '2',
                         child_path)
        family.add_mother('mother_id', '0', '0', 'female', '1', mother_path)
        family.add_father('father_id', '0', '0', 'male', '1', father_path)
        family.set_child()

        sum_x_lr2_proband = 0

        # define the parameters and values for the SNV class
        args = {
            'chrom': "1",
            'position': 2,
            'id': ".",
            'ref': "G",
            'alts': "T",
            'filter': "PASS",
            'info': "CQ=missense_variant;HGNC=ATRX;MAX_AF=0.0001",
            'format': "DP:GT:AD",
            'sample': "50:0/1:10,10",
            'gender': "female",
            'mnv_code': None,
            'qual': '1000'
        }
        dad_args = copy.deepcopy(args)
        dad_args['gender'] = 'male'

        self.assertEqual(load_trio(family, sum_x_lr2_proband), [
            TrioGenotypes(chrom="1",
                          pos=2,
                          child=SNV(**args),
                          mother=SNV(**args),
                          father=SNV(**dad_args))
        ])
Beispiel #5
0
    def setUp(self):
        """ define a default SNV object
        """

        self.pops = [
            "AFR_AF", "AMR_AF", "ASN_AF", "DDD_AF", "EAS_AF", "ESP_AF",
            "EUR_AF", "MAX_AF", "SAS_AF", "UK10K_cohort_AF"
        ]
        Info.set_populations(self.pops)

        chrom = "1"
        pos = "15000000"
        snp_id = "."
        ref = "A"
        alt = "G"
        qual = "1000"
        filt = "PASS"

        info = "HGNC_ID=1001;CQ=missense_variant;random_tag"
        self.keys = "GT:DP:AD"
        self.values = "0/1:50:10,10"

        self.var = SNV(chrom,
                       pos,
                       snp_id,
                       ref,
                       alt,
                       qual,
                       filt,
                       info=info,
                       format=self.keys,
                       sample=self.values)
Beispiel #6
0
    def test_passes_known_genes(self):
        ''' test that genes pass or fail when they affect known genes or not
        '''

        SNV.known_genes = {"1001": {'irrelevant'}}
        info = "HGNC_ID=1001;CQ=missense_variant;random_tag"
        var = SNV('1',
                  '100',
                  '.',
                  'A',
                  'G',
                  '1000',
                  'PASS',
                  info=info,
                  format='GT:DP',
                  sample='0/1:50')

        # a variant that affects a known gene passes
        self.assertTrue(var.passes_filters())

        # a variant that doesn't affect any known genes fails
        SNV.known_genes = {"1002": {'irrelevant'}}
        self.assertFalse(var.passes_filters())

        # if we haven't provided any known genes, the variant passes
        SNV.known_genes = None
        self.assertTrue(var.passes_filters())
    def create_snv(self, gender, genotype):
        """ create a default variant
        """

        chrom = "X"
        pos = "15000000"
        snp_id = "."
        ref = "A"
        alt = "G"
        qual = "50"
        filt = "PASS"

        # set up a SNV object, since SNV inherits VcfInfo
        var = SNV(chrom, pos, snp_id, ref, alt, filt)

        info = "HGNC=TEST;CQ=missense_variant;random_tag;EUR_AF=0.0005"
        format_keys = "GT:DP"
        sample_values = genotype + ":50"

        var.vcf_line = [
            chrom, pos, snp_id, ref, alt, qual, filt, info, format_keys,
            sample_values
        ]

        var.add_info(info)
        var.add_format(format_keys, sample_values)
        var.set_gender(gender)
        var.set_genotype()

        return var
    def test_get_parental_var_snv(self):
        ''' check that get_parental_var() works correctly for SNVs
        '''

        sex = 'F'
        var = create_snv(sex, '0/1')
        mom = Person('fam_id', 'mom', '0', '0', 'F', '1', '/PATH')
        parental = []

        # try to get a matching variant for a mother. This will create a default
        # variant for a missing parental genotype
        self.assertEqual(
            get_parental_var(var, parental, mom),
            SNV(chrom="1",
                position=150,
                id=".",
                ref="A",
                alts="G",
                qual='1000',
                filter="PASS",
                info=str(var.info),
                format="GT",
                sample="0/0",
                gender="female",
                mnv_code=None))

        # now see if we can pick up a  variant where it does exist
        mother_var = create_snv(sex, '0/0')
        self.assertEqual(get_parental_var(var, [mother_var], mom), mother_var)
Beispiel #9
0
    def create_snv(self,
                   chrom,
                   geno="0/1",
                   info=None,
                   pos='150',
                   snp_id='.',
                   ref='A',
                   alt='G',
                   qual='1000',
                   filt='PASS',
                   **kwargs):

        if info is None:
            info = "HGNC=ATRX;CQ=missense_variant;random_tag;AF_AFR=0.0001"

        keys = "GT:DP:TEAM29_FILTER:PP_DNM"
        values = "{0}:50:PASS:0.99".format(geno)

        return SNV(chrom,
                   pos,
                   snp_id,
                   ref,
                   alt,
                   qual,
                   filt,
                   info=info,
                   format=keys,
                   sample=values,
                   gender='male',
                   **kwargs)
Beispiel #10
0
    def construct_variant(self, line, gender):
        """ constructs a Variant object for a VCF line, specific to the variant type
        
        Args:
            line: list of elements of a single sample VCF line:
                [chrom, position, snp_id, ref_allele, alt_allele, quality,
                filter_value, info, format_keys, format_values]
            gender: gender of the individual to whom the variant line belongs
                (eg "1" or "M" for male, "2", or "F" for female).
        
        Returns:
            returns a Variant object
        """

        # CNVs are found by their alt_allele values, as either <DUP>, or <DEL>
        if line[4] == "<DUP>" or line[4] == "<DEL>":
            var = CNV(line[0], line[1], line[2], line[3], line[4], line[6])
            var.add_info(line[7])
            # CNVs require the format values for filtering
            var.set_gender(gender)
            var.add_format(line[8], line[9])
            if self.known_genes is not None:
                var.fix_gene_IDs()
        else:
            var = SNV(line[0], line[1], line[2], line[3], line[4], line[6])
            var.add_info(line[7])

        return var
    def create_snv(self, gender, genotype, chrom, pos, cq=None):
        """ create a default variant
        """

        snp_id = "."
        ref = "A"
        alt = "G"
        filt = "PASS"

        if cq is None:
            cq = "missense_variant"

        # set up a SNV object, since SNV inherits VcfInfo
        var = SNV(chrom, pos, snp_id, ref, alt, filt)

        info = "HGNC=TEST;CQ={};random_tag".format(cq)
        format_keys = "GT:DP"
        sample_values = genotype + ":50"

        var.add_info(info)
        var.add_format(format_keys, sample_values)
        var.set_gender(gender)
        var.set_genotype()

        return var
    def test_construct_variant(self):
        """ test that construct_variant() works correctly
        """

        # check that construct variant works for SNVs
        line = ["1", "100", ".", "T", "G", "1000", "PASS", ".", "GT", "0/1"]
        gender = "M"
        test_var = SNV(*line[:6])

        variant = self.vcf_loader.construct_variant(line, gender)

        self.assertEqual(variant.get_key(), test_var.get_key())
        # initally constructing a SNV shouldn't affect the format variable
        self.assertEqual(variant.format, None)

        # check that construct variant works for CNVs
        line = [
            "1", "100", ".", "T", "<DEL>", "1000", "PASS", "END=200", "GT",
            "0/1"
        ]
        gender = "M"
        test_var = CNV(*line[:6])
        test_var.add_info(line[7])

        variant = self.vcf_loader.construct_variant(line, gender)

        self.assertEqual(variant.get_key(), test_var.get_key())
        self.assertNotEqual(variant.format, None)
Beispiel #13
0
 def setUp(self):
     """ define a default VcfInfo object
     """
     
     chrom = "1"
     pos = "15000000"
     snp_id = "CM00001"
     ref = "A"
     alt = "G"
     filt = "PASS"
     
     # set up a SNV object, since SNV inherits VcfInfo
     self.var = SNV(chrom, pos, snp_id, ref, alt, filt)
     self.var.debug_chrom = "1"
     self.var.debug_pos = "15000000"
     
     self.default_info = "HGNC=ATRX;CQ=missense_variant;random_tag"
     
     # here are the default filtering criteria, as loaded into python
     known_genes = {"ATRX": {"inheritance": {"Hemizygous": \
         {"Loss of function"}}, "start": "10000000", "chrom": "1", \
         "confirmed_status": {"Confirmed DD Gene"}, "end": "20000000"}}
     
     SNV.known_genes = known_genes
     
     self.var.add_info(self.default_info)
Beispiel #14
0
    def test_construct_variant(self):
        """ test that construct_variant() works correctly
        """

        # check that construct variant works for SNVs
        line = ["1", "100", ".", "T", "G", "1000", "PASS", ".", "GT", "0/1"]
        gender = "M"
        test_var = SNV(*line, gender=gender)

        variant = construct_variant(line, gender)

        self.assertEqual(variant.get_key(), test_var.get_key())
        self.assertEqual(variant.format, {'GT': '0/1'})

        # check that construct variant works for CNVs
        line = [
            "1", "100", ".", "T", "<DEL>", "1000", "PASS", "END=200", "GT",
            "0/1"
        ]
        gender = "M"
        test_var = CNV(*line, gender=gender)

        variant = construct_variant(line, gender)

        self.assertEqual(variant.get_key(), test_var.get_key())
        self.assertEqual(variant.format, {'GT': '0/1'})
    def test_filter_de_novos(self):
        """ check that filter_de_novos() works correctly
        """

        # make a family without parents
        family = Family("fam_id")
        child_gender = "female"
        family.add_child("child_id", "child_vcf_path", "2", child_gender)
        self.vcf_loader.family = family

        # set up an autosomal variant
        line = ["1", "100", ".", "T", "G", "1000", "PASS", ".", "GT", "0/1"]
        gender = "M"
        child_var = SNV(*line[:6])
        child_var.add_info(line[7])
        child_var.add_format(line[8], line[9])
        child_var.set_gender(child_gender)
        child_var.set_genotype()

        # combine the variant into a list of TrioGenotypes
        child_vars = [child_var]
        mother_vars = []
        father_vars = []
        trio_variants = self.vcf_loader.combine_trio_variants(
            child_vars, mother_vars, father_vars)

        # check that vars without parents get passed through automatically
        self.assertEqual(self.vcf_loader.filter_de_novos(trio_variants, 0.9),
                         trio_variants)

        # now add parents to the family
        family.add_mother("mother_id", "mother_vcf_path", "1", "female")
        family.add_father("father_id", "father_vcf_path", "1", "male")

        # re-generate the variants list now that parents have been included
        trio_variants = self.vcf_loader.combine_trio_variants(
            child_vars, mother_vars, father_vars)

        # check that vars with parents, and that appear to be de novo are
        # filtered out
        self.assertEqual(self.vcf_loader.filter_de_novos(trio_variants, 0.9),
                         [])

        # check that vars with parents, but which are not de novo, are retained
        mother_vars = child_vars
        trio_variants = self.vcf_loader.combine_trio_variants(
            child_vars, mother_vars, father_vars)

        self.assertEqual(self.vcf_loader.filter_de_novos(trio_variants, 0.9),
                         trio_variants)
    def test_filter_de_novos(self):
        """ check that filter_de_novos() works correctly
        """

        # make a family without parents
        family = Family("fam_id")
        child_gender = "female"
        family.add_child('child_id', 'mother_id', 'father_id', child_gender,
                         '2', 'child_path')

        # set up an autosomal variant
        gender = "M"
        args = [
            "1", "100", ".", "T", "G", "1000", "PASS", ".", "GT", "0/1", gender
        ]
        child_var = SNV(*args)

        # combine the variant into a list of TrioGenotypes
        child_vars = [child_var]
        mother_vars = []
        father_vars = []
        trio_variants = combine_trio_variants(family, child_vars, mother_vars,
                                              father_vars)

        # check that vars without parents get passed through automatically
        self.assertEqual(filter_de_novos(trio_variants, 0.9), trio_variants)

        # now add parents to the family
        family.add_mother("mother_id", '0', '0', 'female', '1',
                          "mother_vcf_path")
        family.add_father("father_id", '0', '0', 'male', '1',
                          "father_vcf_path")
        family = family

        # re-generate the variants list now that parents have been included
        trio_variants = combine_trio_variants(family, child_vars, mother_vars,
                                              father_vars)

        # check that vars with parents, and that appear to be de novo are
        # filtered out
        self.assertEqual(filter_de_novos(trio_variants, 0.9), [])

        # check that vars with parents, but which are not de novo, are retained
        mother_vars = child_vars
        trio_variants = combine_trio_variants(family, child_vars, mother_vars,
                                              father_vars)

        self.assertEqual(filter_de_novos(trio_variants, 0.9), trio_variants)
Beispiel #17
0
    def get_parental_var(self, var, parental_vars, gender, matcher):
        """ get the corresponding parental variant to a childs variant, or
        create a default variant with reference genotype.
        
        Args:
            var: childs var, as Variant object
            parental_vars: list of parental variants
            gender: gender of the parent
            matcher: cnv matcher for parent
        
        Returns:
            returns a Variant object, matched to the proband's variant
        """

        key = var.get_key()

        # if the variant is a CNV, the corresponding variant might not match
        # the start site, so we look a variant that overlaps
        if isinstance(var, CNV) and matcher.has_match(var):
            key = matcher.get_overlap_key(key)

        for parental in parental_vars:
            if key == parental.get_key():
                return parental

        # if the childs variant does not exist in the parents VCF, then we
        # create a default variant for the parent
        if isinstance(var, CNV):
            parental = CNV(var.chrom, var.position, var.variant_id,
                           var.ref_allele, var.alt_allele, var.filter)
        else:
            parental = SNV(var.chrom, var.position, var.variant_id,
                           var.ref_allele, var.alt_allele, var.filter)

        parental.set_gender(gender)
        parental.set_default_genotype()

        return parental
Beispiel #18
0
    def setUp(self):
        """ define a default VcfInfo object
        """

        chrom = "1"
        pos = "15000000"
        snp_id = "."
        ref = "A"
        alt = "G"
        filt = "PASS"

        # set up a SNV object, since SNV inherits VcfInfo
        self.var = SNV(chrom, pos, snp_id, ref, alt, filt)

        info = "HGNC=ATRX;CQ=missense_variant;random_tag"
        self.pops = ["AFR_AF", "AMR_AF", "ASN_AF", "DDD_AF", \
            "EAS_AF", "ESP_AF", "EUR_AF", "MAX_AF", "SAS_AF", \
            "UK10K_cohort_AF"]

        self.format_keys = "GT:DP"
        self.sample_values = "0/1:50"

        self.var.add_info(info)
Beispiel #19
0
    def create_snv(self, chrom, geno="0/1"):
        """ create a default variant
        """

        pos = "15000000"
        snp_id = "."
        ref = "A"
        alt = "G"
        filt = "PASS"

        # set up a SNV object, since SNV inherits VcfInfo
        var = SNV(chrom, pos, snp_id, ref, alt, filt)

        default_info = "HGNC=ATRX;CQ=missense_variant;random_tag;AF_AFR=0.0001"
        keys = "GT:DP:TEAM29_FILTER:PP_DNM"
        values = "{0}:50:PASS:0.99".format(geno)

        var.add_info(default_info)
        var.add_format(keys, values)
        var.set_gender("male")
        var.set_genotype()

        return var
    def create_snv(self, gender, genotype):
        """ create a default variant
        """

        chrom = "1"
        pos = "15000000"
        snp_id = "."
        ref = "A"
        alt = "G"
        filt = "PASS"

        # set up a SNV object, since SNV inherits VcfInfo
        var = SNV(chrom, pos, snp_id, ref, alt, filt)

        info = "HGNC=TEST;CQ=missense_variant;DENOVO-SNP;PP_DNM=0.99"
        keys = "GT:DP:TEAM29_FILTER:PP_DNM"
        values = genotype + ":50:PASS:0.99"

        var.add_info(info)
        var.add_format(keys, values)
        var.set_gender(gender)
        var.set_genotype()

        return var
    def test_analyse_trio(self):
        ''' test that analyse_trio() works correctly
        '''

        # construct the VCFs for the trio members
        paths = {}
        for member in ['child', 'mom', 'dad']:
            vcf = make_vcf_header()

            geno, pp_dnm = '0/0', ''
            if member == 'child':
                geno, pp_dnm = '0/1', ';DENOVO-SNP;PP_DNM=1'

            vcf.append(
                make_vcf_line(genotype=geno, extra='HGNC=ARID1B' + pp_dnm))

            # write the VCF data to a file
            handle = tempfile.NamedTemporaryFile(dir=self.temp_dir,
                                                 delete=False,
                                                 suffix='.vcf')
            for x in vcf:
                handle.write(x.encode('utf8'))
            handle.flush()

            paths[member] = handle.name

        # create a Family object, so we can load the data from the trio's VCFs
        fam_id = 'fam01'
        child = Person(fam_id, 'child', 'dad', 'mom', 'female', '2',
                       paths['child'])
        mom = Person(fam_id, 'mom', '0', '0', 'female', '1', paths['mom'])
        dad = Person(fam_id, 'dad', '0', '0', 'male', '1', paths['dad'])
        family = Family(fam_id, [child], mom, dad)

        self.assertEqual(self.finder.analyse_trio(family), [(TrioGenotypes(
            chrom="1",
            pos=1,
            child=SNV(
                chrom="1",
                position=1,
                id=".",
                ref="G",
                alts="T",
                qual='1000',
                filter="PASS",
                info="CQ=missense_variant;DENOVO-SNP;HGNC=ARID1B;PP_DNM=1",
                format="DP:GT",
                sample="50:0/1",
                gender="female",
                mnv_code=None),
            mother=SNV(chrom="1",
                       position=1,
                       id=".",
                       ref="G",
                       alts="T",
                       qual='1000',
                       filter="PASS",
                       info="CQ=missense_variant;HGNC=ARID1B",
                       format="DP:GT",
                       sample="50:0/0",
                       gender="female",
                       mnv_code=None),
            father=SNV(chrom="1",
                       position=1,
                       id=".",
                       ref="G",
                       alts="T",
                       qual='1000',
                       filter="PASS",
                       info="CQ=missense_variant;HGNC=ARID1B",
                       format="DP:GT",
                       sample="50:0/0",
                       gender="male",
                       mnv_code=None)), ['single_variant'], [
                           'Monoallelic', 'Mosaic'
                       ], ['ARID1B'])])