Skip to content

Commit

Permalink
Added a new base.cfg option norm to adjust the normalisation of tra…
Browse files Browse the repository at this point in the history
…ck values (#67 #34)
  • Loading branch information
mnshgl0110 committed May 22, 2024
1 parent 79c314d commit e80c52f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
6 changes: 5 additions & 1 deletion config/base.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ alpha:0.8
## Margins and dimensions:
chrmar:0.1 ## Adjusts the gap between chromosomes and tracks. Higher values leads to more gap
exmar:0.1 ## Extra margin at the top and bottom of plot area
marginchr:0.1 ## Margin between adjacent chromosomes when using --itx
marginchr:0.1 ## Margin between adjacent chromosomes when using --itx

## LEGEND
legend:T ## To plot legend use T, use F to not plot legend
genlegcol:-1 ## Number of columns for genome legend, set -1 for automatic setup
bbox:0,1.01,0.5,0.3 ## [Left edge, bottom edge, width, height]
bbox_v:0,1.1,0.5,0.3 ## For vertical chromosomes (using -v option)
bboxmar:0.5 ## Margin between genome and annotation legends


## Tracks:
norm:T ## For each chromosome, independently normalise the y-axis of tracks. Use T for normalising independently, and F to normalise based on max value across all chromosomes
6 changes: 5 additions & 1 deletion example/base.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ alpha:0.8
## Margins and dimensions:
chrmar:0.1 ## Adjusts the gap between chromosomes and tracks. Higher values leads to more gap
exmar:0.1 ## Extra margin at the top and bottom of plot area
marginchr:0.1 ## Margin between adjacent chromosomes when using --itx
marginchr:0.1 ## Margin between adjacent chromosomes when using --itx

## LEGEND
legend:T ## To plot legend use T, use F to not plot legend
genlegcol:-1 ## Number of columns for genome legend, set -1 for automatic setup
bbox:0,1.01,0.5,0.3 ## [Left edge, bottom edge, width, height]
bbox_v:0,1.1,0.5,0.3 ## For vertical chromosomes (using -v option)
bboxmar:0.5 ## Margin between genome and annotation legends


## Tracks:
norm:T ## For each chromosome, independently normalise the y-axis of tracks. Use T for normalising independently, and F to normalise based on max value across all chromosomes
30 changes: 22 additions & 8 deletions plotsr/scripts/func.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,21 @@ def readbasecfg(f, v):
cfg['tralwd'] = 0.1
cfg['duplwd'] = 0.1
cfg['alpha'] = 0.8

# Set chromosome margins
cfg['chrmar'] = 0.1
cfg['exmar'] = 0.1
cfg['marginchr'] = 0.01 # Set ITX margin

# Set legend properties
cfg['legend'] = True
cfg['genlegcol'] = -1
cfg['bbox'] = [0, 1.01, 0.5, 0.3]
cfg['bbox_v'] = [0, 1.1, 0.5, 0.3]
cfg['bboxmar'] = 0.5
# Set ITX margin
cfg['marginchr'] = 0.01

# track properties
cfg['norm'] = 'T'


if f == '':
Expand Down Expand Up @@ -257,11 +261,11 @@ def readbasecfg(f, v):
logger.error("Non-numerical values {} provided for {}. Using default value.".format(line[1], line[0]))
continue
cfg['bboxmar'] = [float(i) for i in line[1]]
elif line[0] == 'legend':
elif line[0] in {'legend', 'norm'}:
if line[1] not in ['T', 'F']:
logger.warning("Invalid value {} for legend in base.cfg. Valid values: T/F".format(line[1]))
logger.warning("Invalid value {} for {} in base.cfg. Valid values: T/F".format(line[1], line[0]))
continue
cfg['legend'] = line[1] == 'T'
cfg[line[0]] = line[1]
return cfg
# END

Expand Down Expand Up @@ -1792,7 +1796,7 @@ def drawtracks(ax, tracks, s, chrgrps, chrlengths, v, itx, cfg, minl=0, maxl=-1)
rbuff = genbuff(0, chrlengths, chrgrps, chrs, maxl, v, cfg)
for i in range(len(tracks)):
# Plot background rectangles for the tracks
ti = tracks[i].ti # tranck index
ti = tracks[i].ti # track index
for j in range(cl):
if not v:
x0 = 0 if not itx else 0 + rbuff[chrs[j]]
Expand All @@ -1806,16 +1810,26 @@ def drawtracks(ax, tracks, s, chrgrps, chrlengths, v, itx, cfg, minl=0, maxl=-1)

if tracks[i].ft in ['bed', 'bedgraph']:
bedbin = tracks[i].bincnt
# Select positions that are within the limits
globaltposmax = None
if cfg['norm'] == 'F':
for j in range(cl):
# Select positions that are within the limits
if maxl != -1:
tpos = [k[1] for k in bedbin[chrs[j]] if minl <= k[0] <= maxl]
else:
tpos = [k[1] for k in bedbin[chrs[j]]]
globaltposmax = (max(tpos) if globaltposmax is None else max(tpos + [globaltposmax])) if len(tpos) > 0 else globaltposmax

for j in range(cl):
# Select positions that are within the limits
if maxl != -1:
chrpos = [k[0] if not itx else k[0] + rbuff[chrs[j]] for k in bedbin[chrs[j]] if minl <= k[0] <= maxl]
tpos = [k[1] for k in bedbin[chrs[j]] if minl <= k[0] <= maxl]
else:
chrpos = [k[0] if not itx else k[0] + rbuff[chrs[j]] for k in bedbin[chrs[j]]]
tpos = [k[1] for k in bedbin[chrs[j]]]
# print(cl, len(tpos))
tposmax = max(tpos) if len(tpos) > 0 else 0
tposmax = (max(tpos) if len(tpos) > 0 else 0) if cfg['norm'] == 'T' else globaltposmax
tvars = {'color': tracks[i].lc, 'lw': tracks[i].lw, 'zorder': 2, 'alpha': tracks[i].ta}
if not v:
y0 = cl - j - th*(ti) if not itx else 1 - th*(ti)
Expand Down

0 comments on commit e80c52f

Please sign in to comment.