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
Changes from 2 commits
df6cb2f
aaa2283
d82bd33
c67183a
446e941
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,25 +2,38 @@ 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"; | ||
|
||
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" }, | ||
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. ideally this would be displayed using 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. Good catch. I thought of that when I was adding the field, but forgot to add. Done. |
||
{ 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_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 +46,91 @@ 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}`; | ||
} | ||
} | ||
|
||
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.state.hoveredNode.data[f.name] || "-" | ||
]) | ||
} | ||
]; | ||
} | ||
|
||
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> | ||
); | ||
} | ||
} | ||
|
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.