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_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") self.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'} 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(self.vcf_loader.open_individual(person), [var2]) # if we include MNVs, then the passing variants swap self.assertEqual(self.vcf_loader.open_individual(person, mnvs={('1', 1): 'modified_protein_altering_mnv', ('1', 2): 'modified_synonymous_mnv'}), [var1])
def test_open_individual(self): ''' test that open_individual() works correctly ''' # missing individual returns empty list self.assertEqual(self.vcf_loader.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") self.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", 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", 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(self.vcf_loader.open_individual(person), [var2]) # define a set of variants to automatically pass, and check that these # variants pass. self.vcf_loader.child_keys = set([('1', 1), ('1', 2)]) self.assertEqual(self.vcf_loader.open_individual(person, child_variants=True), [var1, var2])
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])
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)) self.write_gzipped_vcf(path, vcf) return path
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
def test_get_mnv_candidates_catch_assertion_error(self): ''' check that get_mnv_candidates works correctly ''' lines = make_vcf_header() lines.append(make_vcf_line(chrom='1', pos=1, extra='Protein_position=1;Codons=aaT/aaG')) lines.append(make_vcf_line(chrom='1', pos=2, extra='Protein_position=2;Codons=Att/Ctt')) self.write_vcf(lines) self.assertEqual(get_mnv_candidates(self.path), {})
def test_get_mnv_candidates(self): ''' check that get_mnv_candidates works correctly ''' lines = make_vcf_header() lines.append(make_vcf_line(chrom='1', pos=1, extra='Protein_position=1;Codons=aaT/aaG')) lines.append(make_vcf_line(chrom='1', pos=2, extra='Protein_position=1;Codons=Aat/Cat')) self.write_vcf(lines) self.assertEqual(get_mnv_candidates(self.path), { ('1', 1): 'alternate_residue_mnv', ('1', 2): 'alternate_residue_mnv'})
def test_find_nearby_variants_separated(self): ''' test that find_nearby_variants() doesn't include vars far apart ''' lines = make_vcf_header() lines.append(make_vcf_line(pos=1)) lines.append(make_vcf_line(pos=4)) self.write_vcf(lines) vcf = open_vcf(self.path) exclude_header(vcf) self.assertEqual(find_nearby_variants(vcf), [])
def test_find_nearby_variants(self): ''' test that find_nearby_variants() works correctly ''' lines = make_vcf_header() lines.append(make_vcf_line(pos=1)) lines.append(make_vcf_line(pos=2)) self.write_vcf(lines) vcf = open_vcf(self.path) exclude_header(vcf) self.assertEqual(find_nearby_variants(vcf), [[('1', 1), ('1', 2)]])
def test_check_mnv_consequence_alternate_residue(self): ''' test that get_mnv_consequence() works correctly ''' var1 = make_vcf_line(extra='Codons=aaT/aaG').split('\t') var2 = make_vcf_line(extra='Codons=Aat/Cat').split('\t') var1 = parse_vcf_line(var1, self.Variant) var2 = parse_vcf_line(var2, self.Variant) self.assertEqual(check_mnv_consequence(var1, var2, self.pattern), 'alternate_residue_mnv')
def test_check_mnv_consequence_modified_altering(self): ''' test that get_mnv_consequence() works correctly ''' var1 = make_vcf_line(extra='Codons=Cta/Tta').split('\t') var2 = make_vcf_line(extra='Codons=ctA/ctT').split('\t') var1 = parse_vcf_line(var1, self.Variant) var2 = parse_vcf_line(var2, self.Variant) self.assertEqual(check_mnv_consequence(var1, var2, self.pattern), 'modified_protein_altering_mnv')
def test_get_codons_duplicate_codons(self): ''' test that get_codons() works when variants duplicate codons ''' var1 = make_vcf_line(extra='Codons=aGt/aTt|aGt/aTt').split('\t') var2 = make_vcf_line(extra='Codons=agT/agC|agT/agC').split('\t') var1 = parse_vcf_line(var1, self.Variant) var2 = parse_vcf_line(var2, self.Variant) self.assertEqual(get_codons(var1, var2, self.pattern), {'reference': 'aGt', 'snv1': 'aTt', 'snv2': 'agC', 'mnv': 'aTC'})
def test_get_codons_short_codon(self): ''' test that get_codons() raises an error the codons are not 3bp long. ''' var1 = make_vcf_line(extra='Codons=aG/aT').split('\t') var2 = make_vcf_line(extra='Codons=Ag/Gg').split('\t') var1 = parse_vcf_line(var1, self.Variant) var2 = parse_vcf_line(var2, self.Variant) with self.assertRaises(AssertionError): get_codons(var1, var2, self.pattern)
def test_check_minv_consequence_unmodified_synonymous(self): ''' test that get_mnv_consequence() works correctly ''' var1 = make_vcf_line(extra='Codons=Cga/Aga').split('\t') var2 = make_vcf_line(extra='Codons=cgA/cgG').split('\t') var1 = parse_vcf_line(var1, self.Variant) var2 = parse_vcf_line(var2, self.Variant) self.assertEqual(check_mnv_consequence(var1, var2, self.pattern), 'unmodified_synonymous_mnv')
def test_check_mnv_consequence_masked_stop_gained(self): ''' test that get_mnv_consequence() works correctly ''' var1 = make_vcf_line(extra='Codons=taT/taG').split('\t') var2 = make_vcf_line(extra='Codons=Tat/Cat').split('\t') var1 = parse_vcf_line(var1, self.Variant) var2 = parse_vcf_line(var2, self.Variant) self.assertEqual(check_mnv_consequence(var1, var2, self.pattern), 'masked_stop_gain_mnv')
def test_get_codons_null_value(self): ''' test that get_codons() works correctly when some genes have null values ''' var1 = make_vcf_line(extra='Codons=aGt/aTt|.').split('\t') var2 = make_vcf_line(extra='Codons=agT/agC|.').split('\t') var1 = parse_vcf_line(var1, self.Variant) var2 = parse_vcf_line(var2, self.Variant) self.assertEqual(get_codons(var1, var2, self.pattern), {'reference': 'aGt', 'snv1': 'aTt', 'snv2': 'agC', 'mnv': 'aTC'})
def test_get_codons_without_uppercase_base(self): ''' test that get_codons() raises an error when the variant position is not un upper case. ''' var1 = make_vcf_line(extra='Codons=agt/att').split('\t') var2 = make_vcf_line(extra='Codons=agt/agc').split('\t') var1 = parse_vcf_line(var1, self.Variant) var2 = parse_vcf_line(var2, self.Variant) with self.assertRaises(AttributeError): get_codons(var1, var2, self.pattern)
def test_find_nearby_variants_different_chroms(self): ''' test that find_nearby_variants() works correctly with successive variants on different chroms, but at the same position. ''' # get the default two variants lines = make_vcf_header() lines.append(make_vcf_line(chrom='1', pos=1)) lines.append(make_vcf_line(chrom='2', pos=1)) vcf = open_vcf(self.path) exclude_header(vcf) self.assertEqual(find_nearby_variants(vcf), [])
def test_get_codons_with_different_codons(self): ''' test that get_codons() raises an error when different transcripts have different codons ''' var1 = make_vcf_line(extra='Codons=aGt/aTt|Gta/Tta').split('\t') var2 = make_vcf_line(extra='Codons=agT/agC|gTa/gCa').split('\t') var1 = parse_vcf_line(var1, self.Variant) var2 = parse_vcf_line(var2, self.Variant) with self.assertRaises(AssertionError): get_codons(var1, var2, self.pattern)
def test_same_aa_different_positions(self): ''' check that same_aa() works correctly for different amino acids ''' lines = make_vcf_header() lines.append(make_vcf_line(pos=5, extra='Protein_position=2')) lines.append(make_vcf_line(pos=7, extra='Protein_position=3')) lines.append(make_vcf_line(pos=8, extra='Protein_position=4')) self.write_vcf(lines) vcf = tabix.open(self.path) pairs = [[('1', 7), ('1', 8)]] self.assertEqual(same_aa(vcf, pairs), [])
def test_find_nearby_variants_different_threshold(self): ''' test that find_nearby_variants() works correctly when we change the threshold distance. ''' # get the default two variants lines = make_vcf_header() lines.append(make_vcf_line(pos=1)) lines.append(make_vcf_line(pos=2)) vcf = open_vcf(self.path) exclude_header(vcf) # using a lower threshold shouldn't allow any of the variants to pass self.assertEqual(find_nearby_variants(vcf, threshold=0), [])
def test_same_aa(self): ''' check that same_aa() works correctly ''' # get the VCF lines lines = make_vcf_header() lines.append(make_vcf_line(pos=2, extra='Protein_position=1')) lines.append(make_vcf_line(pos=4, extra='Protein_position=1')) self.write_vcf(lines) vcf = tabix.open(self.path) pairs = [[('1', 2), ('1', 4)]] self.assertEqual(same_aa(vcf, pairs), [[('1', 2), ('1', 4)]])
def test_check_mnv_consequence_modified_synonymous(self): ''' test that get_mnv_consequence() works correctly This should only be true for Serine residues, such as TCT -> AGT. ''' var1 = make_vcf_line(extra='Codons=tCt/tGt').split('\t') var2 = make_vcf_line(extra='Codons=Tct/Act').split('\t') var1 = parse_vcf_line(var1, self.Variant) var2 = parse_vcf_line(var2, self.Variant) self.assertEqual(check_mnv_consequence(var1, var2, self.pattern), 'modified_synonymous_mnv')
def test_find_nearby_variants_duplicate_position(self): ''' test that find_nearby_variants() works correctly with a duplicate var ''' # get the default two variants lines = make_vcf_header() lines.append(make_vcf_line(pos=1)) lines.append(make_vcf_line(pos=2)) # make a third variant, but at the same position as the second lines.append(make_vcf_line(pos=2)) self.write_vcf(lines) vcf = open_vcf(self.path) exclude_header(vcf) self.assertEqual(find_nearby_variants(vcf), [[('1', 1), ('1', 2)]])
def test_same_aa_missing_protein_positions(self): ''' check that same_aa() works correctly when the vars aren't in the CDS ''' # if one of the variants in the pair does not have a protein position # listed (i.e. residue number), that indicates the variant could be # affecting the splice site, so we can't use the pair. lines = make_vcf_header() lines.append(make_vcf_line(pos=5)) lines.append(make_vcf_line(pos=7)) lines.append(make_vcf_line(pos=8, extra='Protein_position=4')) self.write_vcf(lines) vcf = tabix.open(self.path) pairs = [[('1', 7), ('1', 8)]] self.assertEqual(same_aa(vcf, pairs), [])
def test_screen_pairs_nonstandard_pair(self): ''' test that screen_pairs() works correctly ''' # get the VCF lines lines = make_vcf_header() lines.append(make_vcf_line(pos=2)) lines.append(make_vcf_line(pos=4)) lines.append(make_vcf_line(pos=5)) lines.append(make_vcf_line(pos=7)) lines.append(make_vcf_line(pos=8)) self.write_vcf(lines) vcf = tabix.open(self.path) # set up a list of 'pairs', where one 'pair' has three variants in it. # we exclude 'pairs' where n != 2. pairs = [[('1', 2), ('1', 4), ('1', 5)], [('1', 7), ('1', 8)]] self.assertEqual(screen_pairs(vcf, pairs, is_not_indel), [[('1', 7), ('1', 8)]])
def test_parse_vcf_line_multi_alts(self): ''' check that parse_vcf_line() works when we have multiple alts ''' line = make_vcf_line(alts='C,CT').split('\t') var = parse_vcf_line(line, self.Variant) parsed = self.Variant(chrom='1', pos=1, id='.', ref='G', alts=['C', 'CT'], qual='1000', filter='PASS', info={'CQ': 'missense_variant'}) self.assertEqual(var, parsed)
def test_is_not_indel(self): ''' check that is_not_indel() works correctly ''' # check a deletion indel (from ref allele) line = make_vcf_line(ref='AA').split('\t') var = parse_vcf_line(line, self.Variant) self.assertFalse(is_not_indel(var)) # check a SNV, should pass line = make_vcf_line(ref='A').split('\t') var = parse_vcf_line(line, self.Variant) self.assertTrue(is_not_indel(var)) # check a SNV with multiple alts line = make_vcf_line(ref='A', alts='T,G').split('\t') var = parse_vcf_line(line, self.Variant) self.assertTrue(is_not_indel(var)) # check a variant with multiple alts, only one of which is for a SNV line = make_vcf_line(ref='A', alts='TT,G').split('\t') var = parse_vcf_line(line, self.Variant) self.assertTrue(is_not_indel(var)) # check an indel with multiple alts, none of which are for a SNV line = make_vcf_line(ref='A', alts='TT,*').split('\t') var = parse_vcf_line(line, self.Variant) self.assertFalse(is_not_indel(var)) # check a deletion indel line = make_vcf_line(ref='A', alts='TT').split('\t') var = parse_vcf_line(line, self.Variant) self.assertFalse(is_not_indel(var))
def test_parse_vcf_line(self): ''' test that parse_vcf_line() works correctly ''' line = make_vcf_line(extra='Protein_position=1;Codons=aGt/aTt').split('\t') var = parse_vcf_line(line, self.Variant) parsed = self.Variant(chrom='1', pos=1, id='.', ref='G', alts=['T'], qual='1000', filter='PASS', info={'Protein_position': '1', 'CQ': 'missense_variant', 'Codons': 'aGt/aTt'}) self.assertEqual(var, parsed) # check that passing in an unsplit string raises an error with self.assertRaises(ValueError): line = self.make_vcf_line() parse_vcf_line(line, self.Variant)
def test_open_individual_male_het_chrx(self): """ test that open_individual() passes over hets in males on chrX """ # 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 vcf = make_vcf_header() vcf.append(make_vcf_line(chrom='X', pos=1, genotype='0/1', extra='HGNC=TEST;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', 'M', '2', path) self.assertEqual(open_individual(person), [])
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", 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", 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", filter="PASS", info="CQ=missense_variant;HGNC=ARID1B", format="DP:GT", sample="50:0/0", gender="male", mnv_code=None)), ['single_variant'], ['Monoallelic'], ['ARID1B'])])
def test_get_matches_extra(self): ''' check that get_matches works correctly with > 2 in the 'pair' ''' # get the VCF lines lines = make_vcf_header() lines.append(make_vcf_line(pos=1)) lines.append(make_vcf_line(pos=2)) lines.append(make_vcf_line(pos=4)) lines.append(make_vcf_line(pos=5)) self.write_vcf(lines) vcf = tabix.open(self.path) pair = [('1', 2), ('1', 4), ('1', 5)] # define the expected lines var1 = parse_vcf_line(make_vcf_line(pos=2).split('\t'), self.Variant) var2 = parse_vcf_line(make_vcf_line(pos=4).split('\t'), self.Variant) var3 = parse_vcf_line(make_vcf_line(pos=5).split('\t'), self.Variant) self.assertEqual(list(get_matches(vcf, pair)), [var1, var2, var3])
def test_is_coding(self): ''' check that is_coding() works correctly ''' # check for a single transcript and variant in CDS line = make_vcf_line(cq='missense_variant').split('\t') var = parse_vcf_line(line, self.Variant) self.assertTrue(is_coding(var)) # check for a single transcript and synonymous variant line = make_vcf_line(cq='synonymous_variant').split('\t') var = parse_vcf_line(line, self.Variant) self.assertTrue(is_coding(var)) # check for a single transcript and coding variant line = make_vcf_line(cq='intergenic_variant').split('\t') var = parse_vcf_line(line, self.Variant) self.assertFalse(is_coding(var)) # check for a single transcript and coding variant line = make_vcf_line(cq='intergenic_variant|missense_variant').split('\t') var = parse_vcf_line(line, self.Variant) self.assertTrue(is_coding(var)) # check for a single transcript and coding variant line = make_vcf_line(cq='intergenic_variant,missense_variant').split('\t') var = parse_vcf_line(line, self.Variant) self.assertTrue(is_coding(var)) # check for a single transcript and coding variant line = make_vcf_line(cq='intergenic_variant,' 'intergenic_variant|missense_variant').split('\t') var = parse_vcf_line(line, self.Variant) self.assertTrue(is_coding(var)) # check for a single transcript and coding variant line = make_vcf_line(cq='intergenic_variant,' 'intergenic_variant|intergenic_variant').split('\t') var = parse_vcf_line(line, self.Variant) self.assertFalse(is_coding(var))