Skip to content

Commit

Permalink
add group autoscale
Browse files Browse the repository at this point in the history
  • Loading branch information
e-sollier committed Jun 19, 2024
1 parent 4d36a66 commit 7c2537c
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 56 deletions.
14 changes: 13 additions & 1 deletion docs/content/describe_figure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,18 @@ Parameters:
* ``scale``: how the maximum value for the y-axis is chosen

* ``auto`` (default): will select as maximum the maximum across all regions.

* ``group auto`` (default): will select as maximum the maximum across all regions, for all tracks in the same group (defined by the group parameter, see below).

* ``auto per region``: will select as maximum the maximum of each region (so a different scale is used for each region)

* ``group auto per region``: will select as maximum the maximum of each region (so a different scale is used for each region), for all tracks in the same group (defined by the group parameter, see below).

* ``custom``: manually specify the maximum value. Can either specify a single value, which will then be used for all regions, or a comma-separated list of values (one per region)

* ``scale_max``: in case "scale" is "custom", indicate the maximum value for the y-axis.

* ``group``: if "scale" is "group auto" or "group auto per region", all tracks with the same value for this parameter will be scaled together.

* ``scale_pos``: where the scale (min and max value of the y-axis) will be displayed

Expand Down Expand Up @@ -218,12 +224,18 @@ Parameters:
* ``scale``: how the maximum value for the y-axis is chosen

* ``auto`` (default): will select as maximum the maximum across all regions.

* ``group auto`` (default): will select as maximum the maximum across all regions, for all tracks in the same group (defined by the group parameter, see below).

* ``auto per region``: will select as maximum the maximum of each region (so a different scale is used for each region)

* ``group auto per region``: will select as maximum the maximum of each region (so a different scale is used for each region), for all tracks in the same group (defined by the group parameter, see below).

* ``custom``: manually specify the maximum value. Can either specify a single value, which will then be used for all regions, or a comma-separated list of values (one per region)

* ``scale_max``: in case "scale" is "custom", indicate the maximum value for the y-axis.

* ``group``: if "scale" is "group auto" or "group auto per region", all tracks with the same value for this parameter will be scaled together.

* ``scale_pos``: where the scale (min and max value of the y-axis) will be displayed

Expand Down
2 changes: 1 addition & 1 deletion figeno/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from figeno.cli import gui, init,make

__version__ = "1.3.3"
__version__ = "1.4.0"

def main():
parser = ArgumentParser("figeno",formatter_class=ArgumentDefaultsHelpFormatter)
Expand Down
52 changes: 52 additions & 0 deletions figeno/figeno.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,9 @@ def draw(self,output_config=None,warnings=[]):
if max_size>100*min_size:
warnings.append("You used regions with very different sizes, so the smaller regions may not be visible.")

# Check if group autoscale was used, in which case compute them.
self.compute_scales()

if self.figure_layout=="horizontal":
self.draw_horizontal(**self.output,warnings=warnings)
elif self.figure_layout=="stacked":
Expand Down Expand Up @@ -507,6 +510,55 @@ def draw_circular(self,file,width=183,dpi=150,transparent=False,warnings=[]):
plt.clf()
plt.close('all')

def compute_scales(self):
self.compute_scales_instance(bigwig_track,"bigwig")
self.compute_scales_instance(coverage_track,"coverage")

def compute_scales_instance(self,instance=bigwig_track,instance_name="bigwig"):
group2scales={}
group2scaletype={}
for t in self.tracks_list:
if isinstance(t,instance):
if t.scale=="custom":
if t.scale_max=="": t.scale_max=None
if t.scale_max is None: raise KnownException("Please provide the scale_max parameter if you use a custom scale.")
if isinstance(t.scale_max,str) and "," in t.scale_max:
try:t.scale_max=[float(x) for x in t.scale_max.split(",")]
except: raise KnownException("The scale_max parameter in a "+instance_name+" track should be a number (or a list of numbers separated by commas): "+str(self.scale_max))
else:
try: t.scale_max=[float(t.scale_max)]
except: raise KnownException("The scale_max parameter in a "+instance_name+" track should be a number: "+str(self.scale_max))
elif t.scale=="auto":
t.scale_max=t.compute_max(self.regions,per_region=False)
elif t.scale=="group auto":
if not hasattr(t,"group"): raise KnownException("Please provide the group parameter, if you use the group auto scale.")
maximum=t.compute_max(self.regions,per_region=False)
if t.group in group2scaletype:
if group2scaletype[t.group]!="group auto": raise KnownException("Please use the same scale (group auto or group auto per region) for all bigwig tracks of the same group.")
else: group2scaletype[t.group]="group auto"
if t.group in group2scales: group2scales[t.group] = max(maximum,group2scales[t.group])
else: group2scales[t.group] = maximum
elif t.scale=="auto per region":
t.scale_max= t.compute_max(self.regions,per_region=True)
elif t.scale=="group auto per region":
if not hasattr(t,"group"): raise KnownException("Please provide the group parameter, if you use the group auto per region scale.")
if t.group in group2scaletype:
if group2scaletype[t.group]!="group auto per region": raise KnownException("Please use the same scale (group auto or group auto per region) for all bigwig tracks of the same group.")
else: group2scaletype[t.group]="group auto per region"
maxima=t.compute_max(self.regions,per_region=True)
if t.group in group2scales: group2scales[t.group] = [max(group2scales[t.group][i],maxima[i]) for i in range(len(maxima))]
else: group2scales[t.group] = maxima
else:
raise KnownException("Invalid scale for "+instance_name+" track: "+str(t.scale)+". Must be auto, auto per region, group auto, group auto per region, or custom.")

for t in self.tracks_list:
if isinstance(t,instance):
if t.scale=="group auto" or t.scale=="group auto per region":
t.scale_max=group2scales[t.group]
if len(t.scale_max)>1 and t.scale_pos!="none": t.scale_pos="corner all"



def figeno_make(config=None,config_file=None,warnings=[]):
if config is None and config_file is None: raise Exception("ERROR: a config or a config_file is required for figeno_make.")
tp = tracks_plot(config=config,config_file=config_file)
Expand Down
2 changes: 1 addition & 1 deletion figeno/gui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "figeno",
"version": "1.3.3",
"version": "1.4.0",
"private": true,
"homepage": "./",
"dependencies": {
Expand Down
7 changes: 5 additions & 2 deletions figeno/gui/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,15 @@ export default function App() {
t.bedmethyls = t.bedmethyls.map((b)=>{const newBed={...b};delete newBed.id; return newBed;})
}
else if (t.type=="bigwig" || t.type=="coverage"){
if (t.scale=="auto"){
if (t.scale!="custom"){
delete t.scale_max;
}
if (t.scale!="group auto" && t.scale!="group auto per region"){
delete t.group;
}
}
else if (t.type=="hic"){
if (t.scale=="auto"){
if (t.scale!="auto"){
delete t.scale_min;
delete t.scale_max;
}
Expand Down
17 changes: 17 additions & 0 deletions figeno/gui/src/Track.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,9 @@ function BigWigTrack({track,set_value,openColorPanel, setFileDialogData,setFileD
<label htmlFor={"scale"+track.id}>Scale:</label>
<select id={"scale"+track.id} value={track.scale} onChange={(e) =>{set_value("scale",e.target.value)}}>
<option className="dropDownOption" key="auto" value="auto">auto</option>
<option className="dropDownOption" key="group auto" value="group auto">group auto</option>
<option className="dropDownOption" key="auto per region" value="auto per region">auto per region</option>
<option className="dropDownOption" key="group auto per region" value="group auto per region">group auto per region</option>
<option className="dropDownOption" key="custom" value="custom">custom</option>
</select>
</div>
Expand All @@ -254,6 +256,13 @@ function BigWigTrack({track,set_value,openColorPanel, setFileDialogData,setFileD
<input id={"scale_max"+track.id} style={{width:"3em"}} value={track.scale_max} onChange={(e) => set_value("scale_max",e.target.value)} ></input>
</div>):""
}
{(track.scale=="group auto" || track.scale=="group auto per region")?(
<div className='formItem'>
<label htmlFor={"group"+track.id}>Group:</label>
<input id={"group"+track.id} style={{width:"3em"}} value={track.group} onChange={(e) => set_value("group",e.target.value)} ></input>
</div>):""
}

<div className='formItem'>
<label htmlFor={"scale_pos"+track.id}>Scale pos:</label>
<select id={"scale_pos"+track.id} value={track.scale_pos} onChange={(e) =>{set_value("scale_pos",e.target.value)}}>
Expand Down Expand Up @@ -294,7 +303,9 @@ function CoverageTrack({track,set_value,openColorPanel, setFileDialogData,setFil
<label htmlFor={"scale"+track.id}>Scale:</label>
<select id={"scale"+track.id} value={track.scale} onChange={(e) =>{set_value("scale",e.target.value)}}>
<option className="dropDownOption" key="auto" value="auto">auto</option>
<option className="dropDownOption" key="group auto" value="group auto">group auto</option>
<option className="dropDownOption" key="auto per region" value="auto per region">auto per region</option>
<option className="dropDownOption" key="group auto per region" value="group auto per region">group auto per region</option>
<option className="dropDownOption" key="custom" value="custom">custom</option>
</select>
</div>
Expand All @@ -304,6 +315,12 @@ function CoverageTrack({track,set_value,openColorPanel, setFileDialogData,setFil
<input id={"scale_max"+track.id} style={{width:"3em"}} value={track.scale_max} onChange={(e) => set_value("scale_max",e.target.value)} ></input>
</div>):""
}
{(track.scale=="group auto" || track.scale=="group auto per region")?(
<div className='formItem'>
<label htmlFor={"group"+track.id}>Group:</label>
<input id={"group"+track.id} style={{width:"3em"}} value={track.group} onChange={(e) => set_value("group",e.target.value)} ></input>
</div>):""
}
<div className='formItem'>
<label htmlFor={"scale_pos"+track.id}>Scale pos:</label>
<select id={"scale_pos"+track.id} value={track.scale_pos} onChange={(e) =>{set_value("scale_pos",e.target.value)}}>
Expand Down
2 changes: 2 additions & 0 deletions figeno/gui/src/TracksContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const defaultTrackValues={
n_bins:500,
scale:"auto",
scale_max:"",
group:"1",
scale_pos:"corner",
upside_down:false
},
Expand All @@ -72,6 +73,7 @@ export const defaultTrackValues={
n_bins:500,
scale:"auto",
scale_max:"",
group:"1",
scale_pos:"corner",
upside_down:false
},
Expand Down
Loading

0 comments on commit 7c2537c

Please sign in to comment.