Skip to content
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

#1055 export selected tips #1067

Merged
merged 11 commits into from
Apr 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ s3/
node_modules/
npm-debug.log
*tgz
.vscode/launch.json
63 changes: 46 additions & 17 deletions src/components/download/downloadModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { stopProp } from "../tree/infoPanels/click";
import * as helpers from "./helperFunctions";
import * as icons from "../framework/svg-icons";
import { getAcknowledgments} from "../framework/footer";
import { createSummary } from "../info/info";
import { createSummary, getNumSelectedTips } from "../info/info";
import { getFullAuthorInfoFromNode } from "../../util/treeMiscHelpers";

const RectangularTreeIcon = withTheme(icons.RectangularTree);
const PanelsGridIcon = withTheme(icons.PanelsGrid);
Expand Down Expand Up @@ -156,32 +157,60 @@ class DownloadModal extends React.Component {
});
return x;
}

getNumUniqueAuthors(nodes) {
const authors = nodes.map((n) => getFullAuthorInfoFromNode(n))
.filter((a) => a && a.value);
const uniqueAuthors = new Set(authors.map((a) => a.value));
return uniqueAuthors.size;
}
downloadButtons() {
// getNumSelectedTips() is redundant work with createSummaryWrapper() below,
// and with the check done to make sure the node is visible in strainTSV(),
// so if speed becomes a concern, could alter this to just generate the list of selected nodes once,
// on modal creation, and add it as a property on the modal
const selectedTipsCount = getNumSelectedTips(this.props.nodes, this.props.tree.visibility);
// likewise, this is somewhat redundant with authorTSV()
const uniqueAuthorCount = this.getNumUniqueAuthors(this.props.nodes);
const filePrefix = this.getFilePrefix();
const iconWidth = 25;
const buttons = [
["Tree (newick)", (<RectangularTreeIcon width={iconWidth} selected />), () => helpers.newick(this.props.dispatch, filePrefix, this.props.nodes[0], false)],
["TimeTree (newick)", (<RectangularTreeIcon width={iconWidth} selected />), () => helpers.newick(this.props.dispatch, filePrefix, this.props.nodes[0], true)],
["Strain Metadata (TSV)", (<MetaIcon width={iconWidth} selected />), () => helpers.strainTSV(this.props.dispatch, filePrefix, this.props.nodes, this.props.metadata.colorings)]
["Tree", "Phylogenetic tree in Newick format with branch lengths in units of divergence.",
(<RectangularTreeIcon width={iconWidth} selected />), () => helpers.newick(this.props.dispatch, filePrefix, this.props.nodes[0], false)],
["TimeTree", "Phylogenetic tree in Newick format with branch lengths measured in years.",
(<RectangularTreeIcon width={iconWidth} selected />), () => helpers.newick(this.props.dispatch, filePrefix, this.props.nodes[0], true)],
["All Metadata (TSV)", `Per-sample metadata for all samples in the dataset (n = ${this.props.metadata.mainTreeNumTips}).`,
(<MetaIcon width={iconWidth} selected />), () => helpers.strainTSV(this.props.dispatch, filePrefix, this.props.nodes, this.props.metadata.colorings, false, null)]
];
if (selectedTipsCount > 0) {
buttons.push(["Selected Metadata (TSV)", `Per-sample metadata for strains which are currently displayed (n = ${selectedTipsCount}/${this.props.metadata.mainTreeNumTips}).`,
(<MetaIcon width={iconWidth} selected />), () => helpers.strainTSV(this.props.dispatch, filePrefix, this.props.nodes,
this.props.metadata.colorings, true, this.props.tree.visibility)]);
}
if (helpers.areAuthorsPresent(this.props.tree)) {
buttons.push(["Author Metadata (TSV)", (<MetaIcon width={iconWidth} selected />), () => helpers.authorTSV(this.props.dispatch, filePrefix, this.props.tree)]);
buttons.push(["Author Metadata (TSV)", `Metadata for all samples in the dataset (n = ${this.props.metadata.mainTreeNumTips}) grouped by their ${uniqueAuthorCount} authors.`,
(<MetaIcon width={iconWidth} selected />), () => helpers.authorTSV(this.props.dispatch, filePrefix, this.props.tree)]);
}
buttons.push(
["Screenshot (SVG)", (<PanelsGridIcon width={iconWidth} selected />), () => helpers.SVG(this.props.dispatch, filePrefix, this.props.panelsToDisplay, this.props.panelLayout, this.makeTextStringsForSVGExport())]
["Screenshot (SVG)", "Screenshot of the current nextstrain display in SVG format.",
(<PanelsGridIcon width={iconWidth} selected />), () => helpers.SVG(this.props.dispatch, filePrefix, this.props.panelsToDisplay, this.props.panelLayout, this.makeTextStringsForSVGExport())]
);
const buttonTextStyle = Object.assign({}, materialButton, {backgroundColor: "rgba(0,0,0,0)", paddingLeft: "10px", color: "white"});
const buttonTextStyle = Object.assign({}, materialButton, {backgroundColor: "rgba(0,0,0,0)", paddingLeft: "10px", color: "white", minWidth: "300px", textAlign: "left" });
const buttonLabelStyle = { fontStyle: "italic", fontSize: "14px", color: "lightgray" };
return (
<div style={{display: "flex", flexWrap: "wrap", justifyContent: "space-around"}}>
{buttons.map((data) => (
<div key={data[0]} onClick={data[2]} style={{cursor: 'pointer'}}>
{data[1]}
<button style={buttonTextStyle}>
{data[0]}
</button>
</div>
))}
<div style={{display: "block", justifyContent: "space-around", marginLeft: "25px", width: "100%" }}>
<div style={{ width: "100%" }}>
{buttons.map((data) => (
<div key={data[0]} onClick={data[3]} style={{cursor: 'pointer' }}>
{data[2]}
<button style={buttonTextStyle} name={data[0]}>
{data[0]}
</button>
<div style={{ display: "inline-block", height: "30px", verticalAlign: "top", paddingTop: "6px" }}>
<label style={buttonLabelStyle} htmlFor={data[0]}>{data[1]}</label>
</div>
</div>
))}
</div>
</div>
);
}
Expand Down
11 changes: 8 additions & 3 deletions src/components/download/helperFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { infoNotification, warningNotification } from "../../actions/notificatio
import { spaceBetweenTrees } from "../tree/tree";
import { getTraitFromNode, getDivFromNode, getFullAuthorInfoFromNode, getVaccineFromNode, getAccessionFromNode } from "../../util/treeMiscHelpers";
import { numericToCalendar } from "../../util/dateHelpers";
import { NODE_VISIBLE } from "../../util/globals";

export const isPaperURLValid = (d) => {
return (
Expand Down Expand Up @@ -105,15 +106,19 @@ export const authorTSV = (dispatch, filePrefix, tree) => {
* Create & write a TSV file where each row is a strain in the tree,
* with the relevent information (accession, traits, etcetera)
*/
export const strainTSV = (dispatch, filePrefix, nodes, colorings) => {
export const strainTSV = (dispatch, filePrefix, nodes, colorings, selectedNodesOnly, nodeVisibilities) => {

/* traverse the tree & store tip information. We cannot write this out as we go as we don't know
exactly which header fields we want until the tree has been traversed. */
const tipTraitValues = {};
const headerFields = ["Strain"];

for (const node of nodes) {
for (const [i, node] of nodes.entries()) {
if (node.hasChildren) continue; /* we only consider tips */

if (selectedNodesOnly && nodeVisibilities &&
(nodeVisibilities[i] !== NODE_VISIBLE || !node.inView)) {continue;} /* skip unselected nodes if requested */

tipTraitValues[node.name] = {Strain: node.name};
if (!node.node_attrs) continue; /* if this is not set then we don't have any node info! */

Expand Down Expand Up @@ -189,7 +194,7 @@ export const strainTSV = (dispatch, filePrefix, nodes, colorings) => {
}

/* write out information we've collected */
const filename = `${filePrefix}_metadata.tsv`;
const filename = `${filePrefix}${selectedNodesOnly ? "_selected_" : "_"}metadata.tsv`;
write(filename, MIME.tsv, linesToWrite.join("\n"));
dispatch(infoNotification({message: `Metadata exported to ${filename}`}));
};
Expand Down
4 changes: 3 additions & 1 deletion src/components/info/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ const styliseDateRange = (date) => {
return dateStr;
};

const getNumSelectedTips = (nodes, visibility) => {
export const getNumSelectedTips = (nodes, visibility) => {
let count = 0;
nodes.forEach((d, idx) => {
// nodes which are not inView have a visibility of NODE_NOT_VISIBLE
// so this check accounts for them as well
if (!d.hasChildren && visibility[idx] === NODE_VISIBLE) count += 1;
});
return count;
Expand Down
2 changes: 1 addition & 1 deletion src/util/treeVisibilityHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ FILTERS:
- filters (in this code) is a list of filters to apply
e.g. [{trait: "country", values: [...]}, ...]
*/
const calcVisibility = (tree, controls, dates) => {
export const calcVisibility = (tree, controls, dates) => {
if (tree.nodes) {
/* inView represents nodes that are within the current view window (i.e. not off the screen) */
let inView;
Expand Down