From 3669db439bb3beafda280604e0e0a09baa552d09 Mon Sep 17 00:00:00 2001 From: Adam Novak Date: Wed, 13 Mar 2024 11:14:58 -0400 Subject: [PATCH] Formalize system of using 'none' in bedFile to fix left and right buttons without BED --- src/components/HeaderForm.js | 40 +++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/src/components/HeaderForm.js b/src/components/HeaderForm.js index 7b0e7c6c..61bb6d4e 100644 --- a/src/components/HeaderForm.js +++ b/src/components/HeaderForm.js @@ -68,6 +68,8 @@ const CLEAR_STATE = { pathNames: [], tracks: {}, + // BED file of regions to jump between. Regions may have pre-extracted chunks in the last column. + // If not used, may be undefined or may have the sting value "none". bedFile: undefined, region: "", name: undefined, @@ -94,6 +96,12 @@ const EMPTY_STATE = { bedSelectOptions: [], }; +// Return true if file is set to a string file name or URL, and false if it is +// falsey or the "none" sentinel. +function isSet(file) { + return (file !== "none" && file); +} + // Creates track to be stored in ViewTarget // Modify as the track system changes // INPUT: file structure, see Types.ts @@ -289,7 +297,7 @@ class HeaderForm extends Component { initState = () => { // Populate state with either viewTarget or the first example let ds = this.props.defaultViewTarget ?? DATA_SOURCES[0]; - const bedSelect = ds.bedFile ? ds.bedFile : "none"; + const bedSelect = isSet(ds.bedFile) ? ds.bedFile : "none"; if (bedSelect !== "none") { this.getBedRegions(bedSelect); } @@ -317,7 +325,7 @@ class HeaderForm extends Component { setTrackFile = (type, index, file) => { // Set the nth track of the given type to the given file. // If there is no nth track of that type, create one. - // If the file is "none", remove that track. + // If the file is unset, remove that track. this.setState((state) => { console.log( "Set file " + @@ -340,13 +348,13 @@ class HeaderForm extends Component { let track = state.tracks[key]; if (track.trackType === type) { if (seenTracksOfType === index) { - if (file !== "none") { + if (isSet(file)) { // We want to adjust it, so keep a modified copy of it let newTrack = JSON.parse(JSON.stringify(track)); newTrack.trackFile = file; newTracks[key] = newTrack; } - // If the file is "none" we drop the track. + // If the file is unset we drop the track. } else { // We want to keep it as is newTracks[key] = track; @@ -364,7 +372,7 @@ class HeaderForm extends Component { console.log( "Saw " + seenTracksOfType + " tracks of type vs index " + index ); - if (seenTracksOfType === index && file !== "none") { + if (seenTracksOfType === index && isSet(file)) { // We need to add this track console.log("Create track at index " + (maxKey + 1)); newTracks[maxKey + 1] = createTrack({ type: type, name: file }); @@ -379,8 +387,8 @@ class HeaderForm extends Component { }; getTrackFile = (tracks, type, index) => { - // Get the file used in the nth track of the gicen type, or "none" if no - // such track exists. + // Get the file used in the nth track of the given type, or the unset + // "none" sentinel if no such track exists. let seenTracksOfType = 0; for (const key in tracks) { let track = tracks[key]; @@ -416,7 +424,7 @@ class HeaderForm extends Component { const bedSelect = json.bedFiles.includes(state.bedSelect) ? state.bedSelect : "none"; - if (bedSelect !== "none") { + if (isSet(bedSelect)) { this.getBedRegions(bedSelect); } for (const key in state.tracks) { @@ -515,7 +523,7 @@ class HeaderForm extends Component { DATA_SOURCES.forEach((ds) => { if (ds.name === value) { let bedSelect = "none"; - if (ds.bedFile) { + if (isSet(ds.bedFile)) { this.getBedRegions(ds.bedFile); bedSelect = ds.bedFile; } else { @@ -648,7 +656,7 @@ class HeaderForm extends Component { if (tracks) { this.setState({ tracks: this.convertArrayToObject(tracks) }); console.log("New tracks have been applied"); - } else if (this.state.bedFile && chunk) { + } else if (isSet(this.state.bedFile) && chunk) { // Try to retrieve tracks from the server const json = await this.api.getChunkTracks( this.state.bedFile, @@ -681,7 +689,7 @@ class HeaderForm extends Component { // update path names const graphFile = this.getTrackFile(newTracks, fileTypes.GRAPH, 0); - if (graphFile && graphFile !== "none") { + if (isSet(graphFile)) { this.getPathNames(graphFile); } }; @@ -691,7 +699,7 @@ class HeaderForm extends Component { const value = event.target.value; this.setState({ [id]: value }); - if (value !== "none") { + if (isSet(value)) { this.getBedRegions(value); } this.setState({ bedFile: value }); @@ -737,7 +745,7 @@ class HeaderForm extends Component { } canGoLeft = (regionIndex) => { - if (this.state.bedFile){ + if (isSet(this.state.bedFile)){ return (regionIndex > 0); } else { return true; @@ -745,7 +753,7 @@ class HeaderForm extends Component { } canGoRight = (regionIndex) => { - if (this.state.bedFile){ + if (isSet(this.state.bedFile)){ if (!this.state.regionInfo["chr"]){ return false; } @@ -757,7 +765,7 @@ class HeaderForm extends Component { handleGoRight = () => { - if (this.state.bedFile){ + if (isSet(this.state.bedFile)){ this.jumpRegion(1); } else { this.budgeRegion(0.5); @@ -765,7 +773,7 @@ class HeaderForm extends Component { }; handleGoLeft = () => { - if (this.state.bedFile){ + if (isSet(this.state.bedFile)){ this.jumpRegion(-1); } else { this.budgeRegion(-0.5);