Commit 22ff5512 authored by Peter Kruczkiewicz's avatar Peter Kruczkiewicz

NJ tree from cgMLST profiles

viz by phylocanvas with metadata highlighting changes
try to filter for non-missing cgMLST profiles from AOFs
parent 113a2164
Pipeline #7098 failed with stage
in 0 seconds
......@@ -978,12 +978,14 @@ public class ProjectSamplesController {
+ " INNER JOIN project_sample sample2 ON s.id = sample2.sample_id\n"
+ " INNER JOIN project p ON sample2.project_id = p.id\n"
+ "WHERE aof.file_path LIKE '%sistr-cgmlst-profiles.csv'\n" +
" AND p.id = " + project.getId() + "\n"
+ "GROUP BY s.id"; //TODO: group by in the Java code instead and select the top result
" AND p.id = " + project.getId() +
" ORDER BY aof.id DESC";
long start = System.nanoTime();
final List<CgMLSTProfile> profiles = tmpl.queryForList(query)
.stream()
.map((r) -> {
logger.debug("What is r?" + r);
CgMLSTProfile profile = new CgMLSTProfile();
profile.sampleId = (Long) r.get("sample_id");
profile.sampleName = (String) r.get("sample_name");
......@@ -1011,10 +1013,24 @@ public class ProjectSamplesController {
})
.sorted(Comparator.comparing(x -> x.aofId))
.collect(Collectors.toList());
final Map<Long, List<CgMLSTProfile>> collect = profiles.stream()
.collect(Collectors.groupingBy(x -> x.sampleId));
final List<CgMLSTProfile> collect1 = collect.entrySet()
.stream()
.map((x) -> {
final List<CgMLSTProfile> profileList = x.getValue();
for (CgMLSTProfile p : profileList) {
if (p.profile.size() == 330) {
return p;
}
}
return profileList.get(0);
})
.collect(Collectors.toList());
long end = System.nanoTime();
long elapsed = end - start;
logger.warn("Elapsed " + TimeUnit.NANOSECONDS.toMillis(elapsed));
return profiles;
return collect1;
}
private class SistrPreds {
......
......@@ -50,6 +50,7 @@
"jszip": "^3.1.3",
"lodash": "4.17.4",
"moment": "^2.18.1",
"neighbor-joining": "^1.0.4",
"ng-file-upload": "^12.2.12",
"noty": "^3.2.0-beta",
"phylocanvas": "^2.8.1",
......
......@@ -122,5 +122,5 @@ export function kruskalsMST(profileResp) {
finalEdge.push({ source: u, target: v, distances: edges[i][2] });
}
}
return { links: finalEdge, groups: groupedSamples, ids: dkeys };
return { links: finalEdge, groups: groupedSamples, ids: dkeys, dm: dm };
}
......@@ -21,8 +21,9 @@ import { FILTERS, SAMPLE_EVENTS } from "./constants";
import { download } from "../../../utilities/file.utilities";
import moment from "moment";
import "../../../../sass/pages/project-samples.scss";
import { RapidNeighborJoining } from "neighbor-joining";
import { kruskalsMST } from "../../../modules/mst/mst";
import * as PhyloCanvas from "../../../../vendor/phylocanvas-metadata/phylocanvas.min";
const QUAL_COLOURS = {
Accent: [
......@@ -124,15 +125,69 @@ const projectId = window.PAGE.projectId;
const baseUrl = window.PAGE.urls.base;
function distancesMapToMatrix(dmap, ids) {
const out = [];
for (let x of ids) {
const tmp = [];
for (let y of ids) {
if (x === y) {
tmp.push(0);
} else if (dmap.hasOwnProperty(x)) {
if (dmap[x].hasOwnProperty(y)) {
tmp.push(dmap[x][y]);
} else if (dmap.hasOwnProperty(y)) {
if (dmap[y].hasOwnProperty(x)) {
tmp.push(dmap[y][x]);
}
}
} else if (dmap.hasOwnProperty(y)) {
if (dmap[y].hasOwnProperty(x)) {
tmp.push(dmap[y][x]);
}
}
}
if (tmp.length !== ids.length) {
console.error(
"Length of dist matrix row is not equal to ids length!",
ids.length,
tmp.length
);
}
out.push(tmp);
}
return out;
}
function getProfilesRenderCgMlstMst(resp) {
console.log("GOT RESP", resp);
const $main = $("#project-samples-main");
const $cntr = $(`<div id="js-d3mst-cntr" class="js-d3mst-cntr"></div>`);
$main.prepend($cntr);
const $cntrPhylocanvas = $(
`<div id="js-phylocanvas-cntr" class="js-phylocanvas-cntr" style="height: 600px;"></div>`
);
$main.prepend($cntrPhylocanvas);
const mstView = new MST(document.getElementById("js-d3mst-cntr"));
console.log("NEW MST view", mstView);
console.log("getting kruskals mst");
const { links, groups, ids } = kruskalsMST(resp);
const { links, groups, ids, dm } = kruskalsMST(resp);
const idMaps = ids.map(x => ({ name: x }));
const nj = new RapidNeighborJoining(distancesMapToMatrix(dm, ids), idMaps);
console.log("NJ", nj, idMaps, dm);
nj.run();
var newick = nj.getAsNewick();
window.newick = newick;
try {
var phylo = new PhyloCanvas.Tree("js-phylocanvas-cntr", {
treeType: "circular"
});
window.phylo = phylo;
phylo.load(newick);
} catch (e) {
console.error(e);
}
console.log("GOT IT", links);
mstView.update_nodes(ids);
mstView.update_links(links, ids);
......@@ -149,11 +204,11 @@ function getProfilesRenderCgMlstMst(resp) {
});
$.get(`/projects/${projectId}/ajax/samples/sistr/predictions`, preds => {
console.log("preds", preds);
const md = preds.reduce((acc, x) => {
const { sampleName, predictions } = x;
acc[sampleName] = JSON.parse(predictions)[0];
return acc;
}, {});
const md = preds.reduce(
(acc, { sampleName, predictions }) =>
Object.assign(acc, { [sampleName]: JSON.parse(predictions)[0] }),
{}
);
mstView.update({
md: md,
show_distances: true,
......@@ -178,7 +233,47 @@ function getProfilesRenderCgMlstMst(resp) {
for (let i = 0; i < Math.min(20, serovarCountArr.length); i++) {
serovars.push(serovarCountArr[i].serovar);
}
console.log("serovars", serovars);
const serovarColour = new Map();
for (let i = 0; i < serovars.length; i++) {
const serovar = serovars[i];
const colour = QUAL_COLOURS.category20[i];
serovarColour.set(serovar, colour);
}
const idToCountColours = {};
for (let id of Object.keys(groups)) {
const others = groups[id];
others.push(id);
const freqs = new Map();
for (let x of others) {
const serovar2 = md[x].serovar;
if (freqs.has(serovar2)) {
freqs.set(serovar2, freqs.get(serovar2) + 1);
} else {
freqs.set(serovar2, 1);
}
}
const sumSerovarCounts = Array.from(freqs.values()).reduce(
(acc, x) => acc + x,
0
);
const derps = serovars
.map(
x =>
freqs.has(x)
? { count: freqs.get(x), color: serovarColour.get(x) }
: null
)
.filter(x => x !== null);
const sumInGroup = derps.reduce((acc, x) => acc + x.count, 0);
if (sumSerovarCounts !== sumInGroup) {
derps.push({ color: "#aaa", count: sumSerovarCounts - sumInGroup });
}
idToCountColours[id] = derps;
}
phylo.setMetadata(idToCountColours);
phylo.colorBranchesByLeafMetadata();
phylo.draw();
mstView.update_highlighted_groups(
"serovar",
serovars,
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -4309,6 +4309,12 @@ negotiator@0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.1.tgz#2b327184e8992101177b28563fb5e7102acd0ca9"
neighbor-joining@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/neighbor-joining/-/neighbor-joining-1.0.4.tgz#1159445fa41b9804f4e12189c4f0ec27f7926ff5"
dependencies:
timsort "^0.2.1"
neo-async@^2.5.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.5.0.tgz#76b1c823130cca26acfbaccc8fbaf0a2fa33b18f"
......@@ -6373,6 +6379,10 @@ timers-browserify@^2.0.4:
dependencies:
setimmediate "^1.0.4"
timsort@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/timsort/-/timsort-0.2.1.tgz#53d98d79f5f45b1fac0e4ee8134b3c799faec0d6"
tiny-emitter@^2.0.0:
version "2.0.2"
resolved "https://registry.yarnpkg.com/tiny-emitter/-/tiny-emitter-2.0.2.tgz#82d27468aca5ade8e5fd1e6d22b57dd43ebdfb7c"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment