Skip to content

Commit

Permalink
support variant y axis for BED if AC/AF columns present
Browse files Browse the repository at this point in the history
  • Loading branch information
slulla committed Sep 20, 2023
1 parent aac4a6d commit 6e202f5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
12 changes: 9 additions & 3 deletions transcriptionary/get_coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,16 @@ def get_variants_bed(bed_path):
print('Warning: number of fields in variant set {} header argument not equal to number of fields in file {} (check for trailing delimiters).'.format(variant_set, bed_path))

df = pd.read_csv(bed_path, names=variant_params[variant_set]['header'], sep='\t', index_col=None, keep_default_na=False, comment='#')
variant_params[variant_set]['has_yaxis_info'] = False
variant_params[variant_set]['has_yaxis_info'] = 'AC' in variant_params[variant_set]['header'] and 'AF' in variant_params[variant_set]['header'] #if 'AC' and 'AF' fields are present, they will be used for lollipop heights, otherwise these will be set to 0 and lollipop heights will not be meaningful

if variant_params[variant_set]['consequence_idx']: plot_params['add_variant_severity_checkbox'] = True

for _,row in df.iterrows():
if str(row.iloc[0]) != str(variant_params[variant_set]['chrom']): continue
di_variant = dict(pos=row.iloc[1], compact_start=-1, variant_set=variant_set, info_annotations=variant_params[variant_set]['info_annotations'], vep=variant_params[variant_set]['vep'], allele_count=0, allele_frequency=0, allele_number=0)
allele_count = 0 if not variant_params[variant_set]['has_yaxis_info'] else int(row['AC'])
allele_frequency = 0 if not variant_params[variant_set]['has_yaxis_info'] else float(row['AF'])

di_variant = dict(pos=row.iloc[1], compact_start=-1, variant_set=variant_set, info_annotations=variant_params[variant_set]['info_annotations'], vep=variant_params[variant_set]['vep'], allele_count=allele_count, allele_frequency=allele_frequency)

for info_field in variant_params[variant_set]['info_annotations']: di_variant[info_field] = row[info_field]

Expand All @@ -115,7 +119,9 @@ def get_variants_bed(bed_path):
elif form == 'bed':
variant_ls = get_variants_bed(filepath)

else: raise ValueError('Invalid filepath: {}'.format(filepath))
else: raise ValueError('Invalid file format for variant set {}: {}'.format(variant_set, filepath))

if not variant_params[variant_set]['has_yaxis_info']: print('Warning: variant set {} does not have both AC (allele count) and AF (allele frequency) fields; lollipop heights will all be set to 0.')

return variant_ls

Expand Down
10 changes: 2 additions & 8 deletions transcriptionary/glyphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .colors import color_variants,lighten_hex_color
import math

def flatten(ls): return [i for s in ls for i in s]
def flatten(ls): return sum(ls, [])

def center_feature(feature):
center = feature[0] + (feature[1] - feature[0])/2
Expand Down Expand Up @@ -162,7 +162,6 @@ def add_variant_glyph(plot_params, variant_params, variant_set, transcript_ID, p
x = [v['compact_pos'] for v in variant_ls]
y0s = [plot_params['y0'] - plot_params['exon_height'] / 2] * N

#def get_y1(ls, yaxis, yaxis_scale):
def get_y1(ys, all_ys, yaxis, yaxis_scale):

if yaxis == 'AC' and yaxis_scale == 'linear':
Expand Down Expand Up @@ -210,7 +209,6 @@ def round_up_to_half(n): #round float up to 0.5
hover_colors = [lighten_hex_color(c, 40) for c in colors]

allele_counts = [v['allele_count'] for v in variant_ls]
#allele_numbers = [v['allele_number'] for v in variant_ls]
allele_frequencies = [v['allele_frequency'] for v in variant_ls]

all_vars = flatten([variant_params[var_set]['variant_ls'] for var_set in variant_params]) #will need list of all vars to get_y1s since we want every variant set to be plotted on the same scale
Expand Down Expand Up @@ -258,10 +256,6 @@ def round_up_to_half(n): #round float up to 0.5

if plot_params['add_variant_axis']:

# y1_ci_li_ct, y1_sg_li_ct = get_y1(allele_counts, 'AC', 'linear')
# y1_ci_lg_ct, y1_sg_lg_ct = get_y1(allele_counts, 'AC', 'log')
# y1_ci_li_fr, y1_sg_li_fr = get_y1(allele_frequencies, 'AF', 'linear')
# y1_ci_lg_fr, y1_sg_lg_fr = get_y1(allele_frequencies, 'AF', 'log')
y1_ci_li_ct, y1_sg_li_ct = get_y1(allele_counts, all_allele_counts, 'AC', 'linear')
y1_ci_lg_ct, y1_sg_lg_ct = get_y1(allele_counts, all_allele_counts, 'AC', 'log')
y1_ci_li_fr, y1_sg_li_fr = get_y1(allele_frequencies, all_allele_frequencies, 'AF', 'linear')
Expand Down Expand Up @@ -295,7 +289,7 @@ def add_track_glyph(user_track_params, track_name, plot, tracks, height, y_pos):
tracks = [{'ID': 'empty track', 'start': 0, 'end': 0, 'compact_start': [0], 'compact_end': [0], 'color': '#111111', 'strand': ''}]
for field in user_track_params[track_name]['annotate_with']: tracks[0][field] = ''
tracks = sorted(tracks, key=lambda di: di['end']-di['start'], reverse=True) #make sure smaller tracks are in front of larger tracks
tracks_compact = flatten([zip(di['compact_start'], di['compact_end']) for di in tracks])
tracks_compact = flatten([list(zip(di['compact_start'], di['compact_end'])) for di in tracks])
color_by = flatten([[di['ID']]*len(di['compact_start']) for di in tracks]) #duplicate color_by for each exon spanned by box
colors = flatten([[di['color']]*len(di['compact_start']) for di in tracks]) #duplicate color for each exon spanned by box

Expand Down

0 comments on commit 6e202f5

Please sign in to comment.