def _add_sv_coordinates(self, variant): """Add the neccesary sv coordinates for a variant Args: variant (puzzle.models.variant) """ variant.stop_chrom = variant.CHROM variant.start = int(variant.POS) # If we have a translocation: if ':' in variant.ALT: other_coordinates = variant.ALT.strip('ACGTN[]').split(':') variant.stop_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') variant.sv_type = 'BND' else: variant.sv_len = variant.stop - variant.start variant['cytoband_start'] = get_cytoband_coord(chrom=variant.CHROM, pos=variant.start) variant['cytoband_stop'] = get_cytoband_coord(chrom=variant.stop_chrom, pos=variant.stop)
def _add_sv_coordinates(self, variant): """Add the neccesary sv coordinates for a variant Args: variant (puzzle.models.variant) """ variant.stop_chrom = variant.CHROM variant.start = int(variant.POS) # If we have a translocation: if ":" in variant.ALT: other_coordinates = variant.ALT.strip("ACGTN[]").split(":") variant.stop_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") variant.sv_type = "BND" else: variant.sv_len = variant.stop - variant.start variant["cytoband_start"] = get_cytoband_coord(chrom=variant.CHROM, pos=variant.start) variant["cytoband_stop"] = get_cytoband_coord(chrom=variant.stop_chrom, pos=variant.stop)
def _add_sv_coordinates(self, variant): """Add the neccesary sv coordinates for a variant Args: variant (puzzle.models.variant) """ variant.stop_chrom = variant.CHROM variant.start = int(variant.POS) # If we have a translocation: if ':' in variant.ALT: other_coordinates = variant.ALT.strip('ACGTN[]').split(':') variant.stop_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') variant.sv_type = 'BND' else: variant.sv_len = variant.stop - variant.start variant['cytoband_start'] = get_cytoband_coord( chrom=variant.CHROM, pos=variant.start ) variant['cytoband_stop'] = get_cytoband_coord( chrom=variant.stop_chrom, pos=variant.stop )
def test_get_cytoband_coord(): """test get_cytoband_coord(chrom, pos) method""" print("Test get_cytoband_coord with different input formats") assert get_cytoband_coord('1', 3) == '1p36.33' assert get_cytoband_coord('chr1', 3) == '1p36.33' assert get_cytoband_coord('chr1', '3') == '1p36.33' print("Test get_cytoband_coord with non existing chromosome") assert get_cytoband_coord('chrMT', '3') is None print("Test get_cytoband_coord with non existing position") assert get_cytoband_coord('chrX', '155270600') is None
def test_get_cytoband_coord(self): """test get_cytoband_coord(chrom, pos) method""" print "Test get_cytoband_coord with different input formats" assert get_cytoband_coord('1', 3) == '1p36.33' assert get_cytoband_coord('chr1', 3) == '1p36.33' assert get_cytoband_coord('chr1', '3') == '1p36.33' print "Test get_cytoband_coord with non existing chromosome" assert get_cytoband_coord('chrMT', '3') == None print "Test get_cytoband_coord with non existing position" assert get_cytoband_coord('chrX', '155270600') == None
def _format_variant(self, gemini_variant, individual_objs, index=0): """Make a puzzle variant from a gemini variant Args: gemini_variant (GeminiQueryRow): The gemini variant individual_objs (list(dict)): A list of Individuals index(int): The index of the variant Returns: variant (dict): A Variant object """ variant_dict = { 'CHROM':gemini_variant['chrom'].lstrip('chrCHR'), 'POS':str(gemini_variant['start']), 'ID':gemini_variant['rs_ids'], 'REF':gemini_variant['ref'], 'ALT':gemini_variant['alt'], 'QUAL':gemini_variant['qual'], 'FILTER':gemini_variant['filter'] } variant = Variant(**variant_dict) variant['index'] = index # Use the gemini id for fast search variant.update_variant_id(gemini_variant['variant_id']) # Update the individuals individual_genotypes = self._get_genotypes( gemini_variant=gemini_variant, individual_objs=individual_objs ) for individual in individual_genotypes: # Add the genotype calls to the variant variant.add_individual(individual) for transcript in self._get_transcripts(gemini_variant): variant.add_transcript(transcript) #Add the most severe consequence variant['most_severe_consequence'] = gemini_variant['impact_so'] for gene in self._get_genes(variant): variant.add_gene(gene) 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') variant['sv_type'] = 'BND' else: variant['stop'] = int(gemini_variant['end']) variant['sv_len'] = variant['stop'] - variant['start'] variant['sv_type'] = gemini_variant['sub_type'] variant['stop_chrom'] = other_chrom else: variant['stop'] = int(variant_dict['POS']) + \ (len(variant_dict['REF']) - len(variant_dict['ALT'])) 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']) #### Check the impact annotations #### if gemini_variant['cadd_scaled']: variant['cadd_score'] = gemini_variant['cadd_scaled'] # We use the prediction in text polyphen = gemini_variant['polyphen_pred'] if polyphen: variant.add_severity('Polyphen', polyphen) # We use the prediction in text sift = gemini_variant['sift_pred'] if sift: variant.add_severity('SIFT', sift) #### Check the frequencies #### thousand_g = gemini_variant['aaf_1kg_all'] if thousand_g: variant['thousand_g'] = float(thousand_g) variant.add_frequency(name='1000GAF', value=float(thousand_g)) exac = gemini_variant['aaf_exac_all'] if exac: variant.add_frequency(name='EXaC', value=float(exac)) esp = gemini_variant['aaf_esp_all'] if esp: variant.add_frequency(name='ESP', value=float(esp)) max_freq = gemini_variant['max_aaf_all'] if max_freq: variant.set_max_freq(max_freq) return variant
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 _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
def test_get_cytoband_coord_chr(): chrom = 'chr1' pos = 3 assert get_cytoband_coord(chrom, pos) == '1p36.33'
def test_get_cytoband_coord_non_existing_pos(): chrom = 'chrX' pos = '155270600' assert get_cytoband_coord(chrom, pos) == None
def test_get_cytoband_coord_non_existing_chr(): chrom = 'chrMT' pos = '3' assert get_cytoband_coord(chrom, pos) == None
def test_get_cytoband_coord_str_pos(): chrom = 'chr1' pos = '3' assert get_cytoband_coord(chrom, pos) == '1p36.33'