def test_sanity(): """Test to build variant dicts""" vcf = get_vcf_file() head = get_header(vcf) header_line = head.header for line in vcf: if not line.startswith('#'): variant = get_variant_dict(variant_line=line, header_line=header_line)
def test_sanity(): """Test to build variant dicts""" vcf = get_vcf_file() head = get_header(vcf) header_line = head.header for line in vcf: if not line.startswith('#'): variant = get_variant_dict( variant_line = line, header_line = header_line )
def test_wrong_variant(): """Test to build a variant dict with a wrong formatted variant""" vcf = get_vcf_file() head = get_header(vcf) header_line = head.header # Missing one gt call wrong_variant = '3\t973348\t.\tG\tA\t100\tPASS\tMQ=1\tGT:GQ\t0/0:60\t0/0:60\n' with pytest.raises(SyntaxError): variant = get_variant_dict(variant_line=wrong_variant, header_line=header_line)
def test_wrong_variant(): """Test to build a variant dict with a wrong formatted variant""" vcf = get_vcf_file() head = get_header(vcf) header_line = head.header # Missing one gt call wrong_variant = '3\t973348\t.\tG\tA\t100\tPASS\tMQ=1\tGT:GQ\t0/0:60\t0/0:60\n' with pytest.raises(SyntaxError): variant = get_variant_dict( variant_line = wrong_variant, header_line = header_line )
def _formated_variants(self, raw_variants, case_obj): """Return variant objects Args: raw_variants (Iterable): An iterable with variant lines case_obj (puzzle.nodels.Case): A case object """ vcf_file_path = case_obj.variant_source logger.info("Parsing file {0}".format(vcf_file_path)) head = HeaderParser() handle = get_vcf_handle(infile=vcf_file_path) # Parse the header for line in handle: line = line.rstrip() if line.startswith("#"): if line.startswith("##"): head.parse_meta_data(line) else: head.parse_header_line(line) else: break handle.close() header_line = head.header # Get the individual ids for individuals in vcf file vcf_individuals = set([ind_id for ind_id in head.individuals]) variant_columns = ["CHROM", "POS", "ID", "REF", "ALT", "QUAL", "FILTER"] vep_header = head.vep_columns snpeff_header = head.snpeff_columns index = 0 for variant_line in raw_variants: if not variant_line.startswith("#"): index += 1 # Create a variant dict: variant_dict = get_variant_dict(variant_line=variant_line, header_line=header_line) variant_dict["CHROM"] = variant_dict["CHROM"].lstrip("chrCHR") # Crreate a info dict: info_dict = get_info_dict(info_line=variant_dict["INFO"]) # Check if vep annotation: vep_string = info_dict.get("CSQ") # Check if snpeff annotation: snpeff_string = info_dict.get("ANN") if vep_string: # Get the vep annotations vep_info = get_vep_info(vep_string=vep_string, vep_header=vep_header) elif snpeff_string: # Get the vep annotations snpeff_info = get_snpeff_info(snpeff_string=snpeff_string, snpeff_header=snpeff_header) variant = Variant(**{column: variant_dict.get(column, ".") for column in variant_columns}) logger.debug("Creating a variant object of variant {0}".format(variant.get("variant_id"))) variant["index"] = index logger.debug("Updating index to: {0}".format(index)) variant["start"] = int(variant_dict["POS"]) if self.variant_type == "sv": other_chrom = variant["CHROM"] # If we have a translocation: if ":" in variant_dict["ALT"] and not "<" in variant_dict["ALT"]: other_coordinates = variant_dict["ALT"].strip("ACGTN[]").split(":") other_chrom = other_coordinates[0].lstrip("chrCHR") other_position = other_coordinates[1] variant["stop"] = other_position # Set 'infinity' to length if translocation variant["sv_len"] = float("inf") else: variant["stop"] = int(info_dict.get("END", variant_dict["POS"])) variant["sv_len"] = variant["stop"] - variant["start"] variant["stop_chrom"] = other_chrom else: variant["stop"] = int(variant_dict["POS"]) + (len(variant_dict["REF"]) - len(variant_dict["ALT"])) variant["sv_type"] = info_dict.get("SVTYPE") variant["cytoband_start"] = get_cytoband_coord(chrom=variant["CHROM"], pos=variant["start"]) if variant.get("stop_chrom"): variant["cytoband_stop"] = get_cytoband_coord(chrom=variant["stop_chrom"], pos=variant["stop"]) # It would be easy to update these keys... thousand_g = info_dict.get("1000GAF") if thousand_g: logger.debug("Updating thousand_g to: {0}".format(thousand_g)) variant["thousand_g"] = float(thousand_g) variant.add_frequency("1000GAF", variant.get("thousand_g")) # SV specific tag for number of occurances occurances = info_dict.get("OCC") if occurances: logger.debug("Updating occurances to: {0}".format(occurances)) variant["occurances"] = float(occurances) variant.add_frequency("OCC", occurances) cadd_score = info_dict.get("CADD") if cadd_score: logger.debug("Updating cadd_score to: {0}".format(cadd_score)) variant["cadd_score"] = float(cadd_score) rank_score_entry = info_dict.get("RankScore") if rank_score_entry: for family_annotation in rank_score_entry.split(","): rank_score = family_annotation.split(":")[-1] logger.debug("Updating rank_score to: {0}".format(rank_score)) variant["rank_score"] = float(rank_score) genetic_models_entry = info_dict.get("GeneticModels") if genetic_models_entry: genetic_models = [] for family_annotation in genetic_models_entry.split(","): for genetic_model in family_annotation.split(":")[-1].split("|"): genetic_models.append(genetic_model) logger.debug("Updating rank_score to: {0}".format(rank_score)) variant["genetic_models"] = genetic_models # Add genotype calls: for individual in case_obj.individuals: sample_id = individual.ind_id if sample_id in vcf_individuals: raw_call = dict(zip(variant_dict["FORMAT"].split(":"), variant_dict[sample_id].split(":"))) variant.add_individual( Genotype( sample_id=sample_id, genotype=raw_call.get("GT", "./."), case_id=individual.case_name, phenotype=individual.phenotype, ref_depth=raw_call.get("AD", ",").split(",")[0], alt_depth=raw_call.get("AD", ",").split(",")[1], genotype_quality=raw_call.get("GQ", "."), depth=raw_call.get("DP", "."), supporting_evidence=raw_call.get("SU", "0"), pe_support=raw_call.get("PE", "0"), sr_support=raw_call.get("SR", "0"), ) ) # Add transcript information: gmaf = None if vep_string: for transcript_info in vep_info: transcript = self._get_vep_transcripts(transcript_info) gmaf_raw = transcript_info.get("GMAF") if gmaf_raw: gmaf = float(gmaf_raw.split(":")[-1]) variant.add_transcript(transcript) if gmaf: variant.add_frequency("GMAF", gmaf) if not variant.thousand_g: variant.thousand_g = gmaf elif snpeff_string: for transcript_info in snpeff_info: transcript = self._get_snpeff_transcripts(transcript_info) variant.add_transcript(transcript) variant["most_severe_consequence"] = get_most_severe_consequence(variant["transcripts"]) for gene in self._get_genes(variant): variant.add_gene(gene) self._add_compounds(variant=variant, info_dict=info_dict) yield variant
def father(ctx): """Check number of variants in common""" logger.info("Running variant_integrity father version {0}".format( variant_integrity.__version__)) print_columns = ['ind_id', 'fraction_of_common_variants', 'common_variants', 'number_calls'] # Children is a dictionary of children that counts the number of errors duos = [] children = {} analysis_individuals = set() for ind_id in ctx.parent.individuals: individual_object = ctx.parent.individuals[ind_id] if individual_object.father != '0': duo = { 'child': ind_id, 'father': individual_object.father } analysis_individuals.add(ind_id) analysis_individuals.add(individual_object.father) duos.append(duo) logger.info("Duo found: {0}".format( ', '.join(list(duo.values())) )) logger.info("Individuals included in analysis: {0}".format( ','.join(list(analysis_individuals)))) for duo in duos: children[duo['child']] = dict(zip( print_columns, [duo['child'], 0, 0, 0])) for line in ctx.parent.variant_file: variant_dict = get_variant_dict( variant_line=line, header_line=ctx.parent.header_line ) logger.debug("Checking genotype calls for variant {0}".format( get_variant_id(variant_dict=variant_dict) )) genotypes = get_genotypes(variant_dict, analysis_individuals) for duo in duos: child_id = duo['child'] child_genotype = genotypes[child_id] father_genotype = genotypes[duo['father']] duo_genotypes = [ child_genotype, father_genotype ] #First check if the child has the variant: if child_genotype.has_variant: # If child have high quality we count the variant if check_high_quality([child_genotype], ctx.parent.gq_treshold): children[child_id]['number_calls'] += 1 if check_common_variant(duo_genotypes): children[child_id]['common_variants'] += 1 results = [] for child_id in children: child_info = children[child_id] common = child_info['common_variants'] variants = child_info['number_calls'] percentage = common/variants child_info['fraction_of_common_variants'] = round(percentage, 3) results.append(child_info) to_json = ctx.parent.to_json outfile = ctx.parent.outfile if to_json: if outfile: json.dump(results, outfile) else: print(json.dumps(results)) else: if outfile: outfile.write("#{0}\n".format('\t'.join(print_columns))) else: print("#{0}".format('\t'.join(print_columns))) for result in results: print_line = "{0}\t{1}\t{2}\t{3}".format( result['ind_id'], result['fraction_of_common_variants'], result['common_variants'], result['number_calls'] ) if outfile: outfile.write("{0}\n".format(print_line)) else: print(print_line)
def mendel(ctx): """Check mendelian errors in all trios""" logger.info("Running variant_integrity mendel {0}".format( variant_integrity.__version__)) print_columns = ['ind_id', 'fraction_of_errors', 'mendelian_errors', 'number_calls'] # Children is a dictionary of children that counts the number of errors trios = [] children = {} analysis_individuals = set() for family in ctx.parent.families: family_object = ctx.parent.families[family] for trio in family_object.trios: trio_individuals = { 'mother':None, 'father':None, 'child':None, } for ind_id in trio: analysis_individuals.add(ind_id) individual_object = ctx.parent.individuals[ind_id] if individual_object.mother in trio: trio_individuals['child'] = ind_id elif individual_object.sex == 1: trio_individuals['father'] = ind_id else: trio_individuals['mother'] = ind_id trios.append(trio_individuals) logger.info("Trio found: {0}".format( ', '.join(list(trio_individuals.values())) )) logger.info("Individuals included in analysis: {0}".format( ','.join(list(analysis_individuals)))) for trio in trios: children[trio['child']] = dict(zip( print_columns, [trio['child'], 0, 0, 0])) for line in ctx.parent.variant_file: variant_dict = get_variant_dict( variant_line=line, header_line=ctx.parent.header_line ) logger.debug("Checking genotype calls for variant {0}".format( get_variant_id(variant_dict=variant_dict) )) genotypes = get_genotypes(variant_dict, analysis_individuals) for trio in trios: child_id = trio['child'] child_genotype = genotypes[child_id] mother_genotype = genotypes[trio['mother']] father_genotype = genotypes[trio['father']] trio_genotypes = [ child_genotype, mother_genotype, father_genotype ] #First check if the child has the variant: if child_genotype.has_variant: # If all individuals are high quality we count the variant if check_high_quality(trio_genotypes, ctx.parent.gq_treshold): children[child_id]['number_calls'] += 1 if check_mendelian_error(child_genotype, mother_genotype, father_genotype): children[child_id]['mendelian_errors'] += 1 results = [] for child_id in children: child_info = children[child_id] errors = child_info['mendelian_errors'] variants = child_info['number_calls'] percentage = errors/variants child_info['fraction_of_errors'] = round(percentage, 3) results.append(child_info) to_json = ctx.parent.to_json outfile = ctx.parent.outfile if to_json: if outfile: json.dump(results, outfile) else: print(json.dumps(results)) else: if outfile: outfile.write("#{0}\n".format('\t'.join(print_columns))) else: print("#{0}".format('\t'.join(print_columns))) for result in results: print_line = "{0}\t{1}\t{2}\t{3}".format( result['ind_id'], result['fraction_of_errors'], result['mendelian_errors'], result['number_calls'] ) if outfile: outfile.write("{0}\n".format(print_line)) else: print(print_line)
def _formated_variants(self, raw_variants, case_obj): """Return variant objects Args: raw_variants (Iterable): An iterable with variant lines case_obj (puzzle.nodels.Case): A case object """ vcf_file_path = case_obj.variant_source logger.info("Parsing file {0}".format(vcf_file_path)) head = HeaderParser() handle = get_vcf_handle(infile=vcf_file_path) # Parse the header for line in handle: line = line.rstrip() if line.startswith('#'): if line.startswith('##'): head.parse_meta_data(line) else: head.parse_header_line(line) else: break handle.close() header_line = head.header # Get the individual ids for individuals in vcf file vcf_individuals = set([ind_id for ind_id in head.individuals]) variant_columns = ['CHROM', 'POS', 'ID', 'REF', 'ALT', 'QUAL', 'FILTER'] vep_header = head.vep_columns snpeff_header = head.snpeff_columns index = 0 for variant_line in raw_variants: if not variant_line.startswith('#'): index += 1 #Create a variant dict: variant_dict = get_variant_dict( variant_line = variant_line, header_line = header_line ) variant_dict['CHROM'] = variant_dict['CHROM'].lstrip('chrCHR') #Crreate a info dict: info_dict = get_info_dict( info_line = variant_dict['INFO'] ) #Check if vep annotation: vep_string = info_dict.get('CSQ') #Check if snpeff annotation: snpeff_string = info_dict.get('ANN') if vep_string: #Get the vep annotations vep_info = get_vep_info( vep_string = vep_string, vep_header = vep_header ) elif snpeff_string: #Get the vep annotations snpeff_info = get_snpeff_info( snpeff_string = snpeff_string, snpeff_header = snpeff_header ) variant = Variant( **{column: variant_dict.get(column, '.') for column in variant_columns} ) logger.debug("Creating a variant object of variant {0}".format( variant.get('variant_id'))) variant['index'] = index logger.debug("Updating index to: {0}".format( index)) variant['start'] = int(variant_dict['POS']) if self.variant_type == 'sv': other_chrom = variant['CHROM'] # If we have a translocation: if ':' in variant_dict['ALT']: other_coordinates = variant_dict['ALT'].strip('ACGTN[]').split(':') other_chrom = other_coordinates[0].lstrip('chrCHR') other_position = other_coordinates[1] variant['stop'] = other_position #Set 'infinity' to length if translocation variant['sv_len'] = float('inf') else: variant['stop'] = int(info_dict.get('END', variant_dict['POS'])) variant['sv_len'] = variant['stop'] - variant['start'] variant['stop_chrom'] = other_chrom else: variant['stop'] = int(variant_dict['POS']) + \ (len(variant_dict['REF']) - len(variant_dict['ALT'])) variant['sv_type'] = info_dict.get('SVTYPE') variant['cytoband_start'] = get_cytoband_coord( chrom=variant['CHROM'], pos=variant['start']) if variant.get('stop_chrom'): variant['cytoband_stop'] = get_cytoband_coord( chrom=variant['stop_chrom'], pos=variant['stop']) # It would be easy to update these keys... thousand_g = info_dict.get('1000GAF') if thousand_g: logger.debug("Updating thousand_g to: {0}".format( thousand_g)) variant['thousand_g'] = float(thousand_g) variant.add_frequency('1000GAF', variant.get('thousand_g')) #SV specific tag for number of occurances occurances = info_dict.get('OCC') if occurances: logger.debug("Updating occurances to: {0}".format( occurances)) variant['occurances'] = float(occurances) variant.add_frequency('OCC', occurances) cadd_score = info_dict.get('CADD') if cadd_score: logger.debug("Updating cadd_score to: {0}".format( cadd_score)) variant['cadd_score'] = float(cadd_score) rank_score_entry = info_dict.get('RankScore') if rank_score_entry: for family_annotation in rank_score_entry.split(','): rank_score = family_annotation.split(':')[-1] logger.debug("Updating rank_score to: {0}".format( rank_score)) variant['rank_score'] = float(rank_score) genetic_models_entry = info_dict.get('GeneticModels') if genetic_models_entry: genetic_models = [] for family_annotation in genetic_models_entry.split(','): for genetic_model in family_annotation.split(':')[-1].split('|'): genetic_models.append(genetic_model) logger.debug("Updating rank_score to: {0}".format( rank_score)) variant['genetic_models'] = genetic_models #Add genotype calls: for individual in case_obj.individuals: sample_id = individual.ind_id if sample_id in vcf_individuals: raw_call = dict(zip( variant_dict['FORMAT'].split(':'), variant_dict[sample_id].split(':')) ) variant.add_individual(Genotype( sample_id = sample_id, genotype = raw_call.get('GT', './.'), case_id = individual.case_name, phenotype = individual.phenotype, ref_depth = raw_call.get('AD', ',').split(',')[0], alt_depth = raw_call.get('AD', ',').split(',')[1], genotype_quality = raw_call.get('GQ', '.'), depth = raw_call.get('DP', '.'), supporting_evidence = raw_call.get('SU', '0'), pe_support = raw_call.get('PE', '0'), sr_support = raw_call.get('SR', '0'), )) # Add transcript information: if vep_string: for transcript in self._get_vep_transcripts(variant, vep_info): variant.add_transcript(transcript) elif snpeff_string: for transcript in self._get_snpeff_transcripts(variant, snpeff_info): variant.add_transcript(transcript) variant['most_severe_consequence'] = get_most_severe_consequence( variant['transcripts'] ) for gene in self._get_genes(variant): variant.add_gene(gene) self._add_compounds(variant=variant, info_dict=info_dict) yield variant
def _format_variant(self, variant_line, index, case_obj, head): """Return variant objects Args: raw_variants (Iterable): An iterable with variant lines case_obj (puzzle.nodels.Case): A case object """ header_line = head.header # Get the individual ids for individuals in vcf file vcf_individuals = set([ind_id for ind_id in head.individuals]) variant_columns = ['CHROM', 'POS', 'ID', 'REF', 'ALT', 'QUAL', 'FILTER'] vep_header = head.vep_columns snpeff_header = head.snpeff_columns #Create a variant dict: variant_dict = get_variant_dict( variant_line = variant_line, header_line = header_line ) variant_dict['CHROM'] = variant_dict['CHROM'].lstrip('chrCHR') #Crreate a info dict: info_dict = get_info_dict( info_line = variant_dict['INFO'] ) #Check if vep annotation: vep_string = info_dict.get('CSQ') #Check if snpeff annotation: snpeff_string = info_dict.get('ANN') if vep_string: #Get the vep annotations vep_info = get_vep_info( vep_string = vep_string, vep_header = vep_header ) elif snpeff_string: #Get the vep annotations snpeff_info = get_snpeff_info( snpeff_string = snpeff_string, snpeff_header = snpeff_header ) variant = Variant( **{column: variant_dict.get(column, '.') for column in variant_columns} ) logger.debug("Creating a variant object of variant {0}".format( variant.get('variant_id'))) variant['index'] = index logger.debug("Updating index to: {0}".format( index)) variant['start'] = int(variant_dict['POS']) if self.variant_type == 'sv': other_chrom = variant['CHROM'] # If we have a translocation: if ':' in variant_dict['ALT'] and not '<' in variant_dict['ALT']: other_coordinates = variant_dict['ALT'].strip('ACGTN[]').split(':') other_chrom = other_coordinates[0].lstrip('chrCHR') other_position = other_coordinates[1] variant['stop'] = other_position #Set 'infinity' to length if translocation variant['sv_len'] = float('inf') else: variant['stop'] = int(info_dict.get('END', variant_dict['POS'])) variant['sv_len'] = variant['stop'] - variant['start'] variant['stop_chrom'] = other_chrom else: variant['stop'] = int(variant_dict['POS']) + \ (len(variant_dict['REF']) - len(variant_dict['ALT'])) variant['sv_type'] = info_dict.get('SVTYPE') variant['cytoband_start'] = get_cytoband_coord( chrom=variant['CHROM'], pos=variant['start']) if variant.get('stop_chrom'): variant['cytoband_stop'] = get_cytoband_coord( chrom=variant['stop_chrom'], pos=variant['stop']) # It would be easy to update these keys... thousand_g = info_dict.get('1000GAF') if thousand_g: logger.debug("Updating thousand_g to: {0}".format( thousand_g)) variant['thousand_g'] = float(thousand_g) variant.add_frequency('1000GAF', variant.get('thousand_g')) #SV specific tag for number of occurances occurances = info_dict.get('OCC') if occurances: logger.debug("Updating occurances to: {0}".format( occurances)) variant['occurances'] = float(occurances) variant.add_frequency('OCC', occurances) cadd_score = info_dict.get('CADD') if cadd_score: logger.debug("Updating cadd_score to: {0}".format( cadd_score)) variant['cadd_score'] = float(cadd_score) rank_score_entry = info_dict.get('RankScore') if rank_score_entry: for family_annotation in rank_score_entry.split(','): rank_score = family_annotation.split(':')[-1] logger.debug("Updating rank_score to: {0}".format( rank_score)) variant['rank_score'] = float(rank_score) genetic_models_entry = info_dict.get('GeneticModels') if genetic_models_entry: genetic_models = [] for family_annotation in genetic_models_entry.split(','): for genetic_model in family_annotation.split(':')[-1].split('|'): genetic_models.append(genetic_model) logger.debug("Updating rank_score to: {0}".format( rank_score)) variant['genetic_models'] = genetic_models #Add genotype calls: for individual in case_obj.individuals: sample_id = individual.ind_id if sample_id in vcf_individuals: raw_call = dict(zip( variant_dict['FORMAT'].split(':'), variant_dict[sample_id].split(':')) ) genotype = Genotype(**raw_call) variant.add_individual(puzzle_genotype( sample_id = sample_id, genotype = genotype.genotype, case_id = individual.case_name, phenotype = individual.phenotype, ref_depth = genotype.ref_depth, alt_depth = genotype.alt_depth, genotype_quality = genotype.genotype_quality, depth = genotype.depth_of_coverage, supporting_evidence = genotype.supporting_evidence, pe_support = genotype.pe_support, sr_support = genotype.sr_support, )) # Add transcript information: gmaf = None if vep_string: for transcript_info in vep_info: transcript = self._get_vep_transcripts(transcript_info) gmaf_raw = transcript_info.get('GMAF') if gmaf_raw: gmaf = float(gmaf_raw.split(':')[-1]) variant.add_transcript(transcript) if gmaf: variant.add_frequency('GMAF', gmaf) if not variant.thousand_g: variant.thousand_g = gmaf elif snpeff_string: for transcript_info in snpeff_info: transcript = self._get_snpeff_transcripts(transcript_info) variant.add_transcript(transcript) most_severe_consequence = get_most_severe_consequence( variant['transcripts'] ) if most_severe_consequence: variant['most_severe_consequence'] = most_severe_consequence variant['impact_severity'] = IMPACT_SEVERITIES.get(most_severe_consequence) for gene in self._get_genes(variant): variant.add_gene(gene) self._add_compounds(variant=variant, info_dict=info_dict) return variant