def add_transcript_coordinate_to_variants(db, genome, variant_list, transcript_id): """ Each variant has a 'xpos' and 'pos' positional attributes. This method takes a list of variants and adds a third position: the "transcript coordinates". This is defined as the distance from the start of the transcript, in coding bases. So a variant in the 7th base of the 6th exon of a transcript will have a transcript coordinate of the sum of the size of the first 5 exons) + 7 This is 0-based, so a variant in the first base of the first exon has a transcript coordinate of 0. You may want to add transcript coordinates for multiple transcripts, so this is stored in a variant as variant['transcript_coordinates'][transcript_id] If a variant in variant_list does not have a `transcript_coordinates` dictionary, we create one If a variant start position for some reason does not fall in any exons in this transcript, its coordinate is 0. This is perhaps logically inconsistent, but it allows you to spot errors quickly if there's a pileup at the first base. `None` would just break things. Consider the behavior if a 20 base deletion deletes parts of two exons. I think the behavior in this method is consistent, but beware that it might break things downstream. Edits variant_list in place; no return val """ import lookups # make sure exons is sorted by (start, end) exons = sorted(lookups.get_exons_in_transcript(db, genome, transcript_id), key=itemgetter('start', 'stop')) # offset from start of base for exon in ith position (so first item in this list is always 0) exon_offsets = [0 for i in range(len(exons))] for i, exon in enumerate(exons): for j in range(i + 1, len(exons)): exon_offsets[j] += exon['stop'] - exon['start'] for variant in variant_list: if 'transcript_coordinates' not in variant: variant['transcript_coordinates'] = {} variant['transcript_coordinates'][transcript_id] = 0 for i, exon in enumerate(exons): if exon['start'] <= variant['pos'] <= exon['stop']: variant['transcript_coordinates'][ transcript_id] = exon_offsets[i] + variant['pos'] - exon[ 'start']
def add_transcript_coordinate_to_variants(db, genome, variant_list, transcript_id): """ Each variant has a 'xpos' and 'pos' positional attributes. This method takes a list of variants and adds a third position: the "transcript coordinates". This is defined as the distance from the start of the transcript, in coding bases. So a variant in the 7th base of the 6th exon of a transcript will have a transcript coordinate of the sum of the size of the first 5 exons) + 7 This is 0-based, so a variant in the first base of the first exon has a transcript coordinate of 0. You may want to add transcript coordinates for multiple transcripts, so this is stored in a variant as variant['transcript_coordinates'][transcript_id] If a variant in variant_list does not have a `transcript_coordinates` dictionary, we create one If a variant start position for some reason does not fall in any exons in this transcript, its coordinate is 0. This is perhaps logically inconsistent, but it allows you to spot errors quickly if there's a pileup at the first base. `None` would just break things. Consider the behavior if a 20 base deletion deletes parts of two exons. I think the behavior in this method is consistent, but beware that it might break things downstream. Edits variant_list in place; no return val """ import lookups # make sure exons is sorted by (start, end) exons = sorted(lookups.get_exons_in_transcript(db, genome, transcript_id), key=itemgetter('start', 'stop')) # offset from start of base for exon in ith position (so first item in this list is always 0) exon_offsets = [0 for i in range(len(exons))] for i, exon in enumerate(exons): for j in range(i+1, len(exons)): exon_offsets[j] += exon['stop'] - exon['start'] for variant in variant_list: if 'transcript_coordinates' not in variant: variant['transcript_coordinates'] = {} variant['transcript_coordinates'][transcript_id] = 0 for i, exon in enumerate(exons): if exon['start'] <= variant['pos'] <= exon['stop']: variant['transcript_coordinates'][transcript_id] = exon_offsets[i] + variant['pos'] - exon['start']