New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add tooltips to the phylo tree for both NCBI and Sample entries #1532
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
df6cb2f
* Add tooltips to the phylo tree for both NCBI and Sample entries
aaa2283
Remove access to originating window.
d82bd33
Merge branch 'master' into tiago/sprint-phylo
tfrcarvalho c67183a
Add extra fields to tooltip.
446e941
Merge branch 'tiago/sprint-phylo' of github.com:chanzuckerberg/idseq-…
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React from "react"; | ||
import PropTypes from "prop-types"; | ||
|
||
const DataTooltip = ({ data, subtitle, title }) => { | ||
// DataTooltip receives: | ||
// - title | ||
// - subtitle | ||
// - an object(section_name: array([label, value], [..])) | ||
|
||
const renderSection = (sectionName, dataValues) => { | ||
return ( | ||
<div className="data-tooltip__section" key={`section-${sectionName}`}> | ||
<div className="data-tooltip__section-name">{sectionName}</div> | ||
{dataValues.map(keyValuePair => { | ||
return ( | ||
<div | ||
className="data-tooltip__value-pair" | ||
key={`value-${sectionName}-${keyValuePair[0]}`} | ||
> | ||
<div className="data-tooltip__label">{keyValuePair[0]}</div> | ||
<div className="data-tooltip__value">{keyValuePair[1]}</div> | ||
</div> | ||
); | ||
})} | ||
</div> | ||
); | ||
}; | ||
|
||
const renderSections = data => { | ||
return data.map(section => renderSection(section.name, section.data)); | ||
}; | ||
|
||
return ( | ||
<div className="data-tooltip"> | ||
{title && <div className="data-tooltip__title">{title}</div>} | ||
{subtitle && <div className="data-tooltip__subtitle">{subtitle}</div>} | ||
<div className="data-tooltip__data">{renderSections(data)}</div> | ||
</div> | ||
); | ||
}; | ||
|
||
DataTooltip.propTypes = { | ||
data: PropTypes.array, | ||
subtitle: PropTypes.string, | ||
title: PropTypes.string | ||
}; | ||
export default DataTooltip; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,25 +2,49 @@ import React from "react"; | |
import Tree from "../../utils/structures/Tree"; | ||
import Dendogram from "../../visualizations/dendrogram/Dendogram"; | ||
import PropTypes from "prop-types"; | ||
import DataTooltip from "../../ui/containers/DataTooltip"; | ||
import Moment from "react-moment"; | ||
|
||
class PhyloTreeVis extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
newick: props.newick | ||
hoveredNode: null | ||
}; | ||
|
||
this.treeVis = null; | ||
} | ||
(this.newick = props.newick), | ||
(this.nodeData = props.nodeData), | ||
(this.treeVis = null); | ||
|
||
static getDerivedStateFromProps(props, state) { | ||
if (props.newick !== state.newick || props.nodeData !== state.nodeData) { | ||
return { | ||
newick: props.newick, | ||
props: props.nodeData | ||
}; | ||
} | ||
this.handleNodeHover = this.handleNodeHover.bind(this); | ||
|
||
this.sampleFields = [ | ||
{ name: "project_name", label: "Project" }, | ||
{ name: "sample_location", label: "Location" }, | ||
{ | ||
name: "created_at", | ||
label: "Upload Date", | ||
parser: val => { | ||
return <Moment fromNow date={val} />; | ||
} | ||
}, | ||
{ name: "sample_date", label: "Collection Date" }, | ||
{ name: "sample_tissue", label: "Tissue" }, | ||
{ name: "sample_template", label: "Template" }, | ||
{ name: "sample_library", label: "Library" }, | ||
{ name: "sample_sequencer", label: "Sequencer" }, | ||
{ name: "sample_unique_id", label: "Unique ID" }, | ||
{ name: "sample_input_pg", label: "Input PG" }, | ||
{ name: "sample_batch", label: "Batch" }, | ||
{ name: "sample_diagnosis", label: "Diagnosis" }, | ||
{ name: "sample_detection", label: "Detection" }, | ||
{ name: "sample_notes", label: "Notes" } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sorry I realized I forgot some:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. |
||
]; | ||
this.ncbiFields = [ | ||
{ name: "country", label: "Country" }, | ||
{ name: "collection_date", label: "Collection Date" } | ||
]; | ||
} | ||
|
||
componentDidMount() { | ||
|
@@ -33,26 +57,101 @@ class PhyloTreeVis extends React.Component { | |
// Name for the legend when the attribute is missing / other | ||
colorGroupAbsentName: "NCBI References", | ||
legendX: 880, | ||
legendY: 50 | ||
legendY: 50, | ||
tooltipContainer: this.tooltipContainer, | ||
onNodeTextClick: this.handleNodeClick, | ||
onNodeHover: this.handleNodeHover | ||
}); | ||
this.treeVis.update(); | ||
} | ||
|
||
componentDidUpdate() { | ||
this.treeVis.setTree( | ||
Tree.fromNewickString(this.props.newick, this.props.nodeData) | ||
); | ||
this.treeVis.update(); | ||
if ( | ||
this.props.newick != this.newick || | ||
this.props.nodeData != this.nodeData | ||
) { | ||
this.newick = this.props.newick; | ||
this.nodeData = this.props.nodeData; | ||
this.treeVis.setTree( | ||
Tree.fromNewickString(this.props.newick, this.props.nodeData) | ||
); | ||
this.treeVis.update(); | ||
} | ||
} | ||
|
||
handleNodeHover(node) { | ||
this.setState({ hoveredNode: node }); | ||
} | ||
|
||
handleNodeClick(node) { | ||
if (node.data.accession) { | ||
let url = `https://www.ncbi.nlm.nih.gov/nuccore/${node.data.accession}`; | ||
window.open(url, "_blank", "noopener", "noreferrer"); | ||
} else if (node.data.id) { | ||
location.href = `/samples/${node.data.id}`; | ||
} | ||
} | ||
|
||
getFieldValue(field) { | ||
let value = this.state.hoveredNode.data[field.name]; | ||
if (value && field.parser) { | ||
try { | ||
value = field.parser(value); | ||
} catch (err) { | ||
// TODO: handle error | ||
console.error(err); | ||
} | ||
} | ||
return value || "-"; | ||
} | ||
|
||
getTooltipData() { | ||
let fields = this.state.hoveredNode.data.accession | ||
? this.ncbiFields | ||
: this.sampleFields; | ||
|
||
let topSectionName = null; | ||
let topSectionData = null; | ||
if (this.state.hoveredNode.data.project_name) { | ||
topSectionName = "Sample"; | ||
topSectionData = [ | ||
["Project", this.state.hoveredNode.data.project_name || "-"] | ||
]; | ||
} else { | ||
topSectionName = "NCBI Reference"; | ||
topSectionData = [ | ||
["Accession", this.state.hoveredNode.data.accession || "-"] | ||
]; | ||
} | ||
return [ | ||
{ name: topSectionName, data: topSectionData }, | ||
{ | ||
name: "Metadata", | ||
data: fields.map(f => [f.label, this.getFieldValue(f) || "-"]) | ||
} | ||
]; | ||
} | ||
|
||
render() { | ||
return ( | ||
<div | ||
className="phylo-tree-vis" | ||
ref={container => { | ||
this.treeContainer = container; | ||
}} | ||
/> | ||
<div className="phylo-tree-vis"> | ||
<div | ||
className="phylo-tree-vis__tree-container" | ||
ref={container => { | ||
this.treeContainer = container; | ||
}} | ||
/> | ||
<div | ||
className="phylo-tree-vis__tooltip-container" | ||
ref={tooltip => { | ||
this.tooltipContainer = tooltip; | ||
}} | ||
> | ||
{this.state.hoveredNode && ( | ||
<DataTooltip data={this.getTooltipData()} /> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did we all agree that window.sessionStorage is OK? is it supported on all the modern browsers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be fine: https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage. We should do a more through css/js audit for issues in older browsers.