Skip to content

Commit

Permalink
Use prefix for balance col & col_names dataset #55
Browse files Browse the repository at this point in the history
  • Loading branch information
fedarko committed Feb 23, 2019
1 parent 2498bce commit adb4a1d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
12 changes: 9 additions & 3 deletions rankratioviz/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,13 @@ def gen_sample_plot(table, metadata, category, palette='Set1'):
# every sample to NaN so that Altair will filter them out (producing an
# empty scatterplot by default, which makes sense).
balance = pd.Series(index=table.index).fillna(float('nan'))
data = pd.DataFrame({'balance': balance}, index=table.index)
data = pd.DataFrame({'rankratioviz_balance': balance}, index=table.index)
# At this point, "data" is a DataFrame with its index as sample IDs and
# one column ("balance", which is solely NaNs).
data = pd.merge(data, metadata[[category]], left_index=True,
right_index=True)
# TODO note dropped samples from this merge (by comparing data with
# metadata and table) and report them to user (#54).

# Construct unified DataFrame, combining our "data" DataFrame with the
# "table" variable (in order to associate each sample with its
Expand Down Expand Up @@ -200,7 +204,8 @@ def gen_sample_plot(table, metadata, category, palette='Set1'):
title="Log Ratio of Abundances in Samples"
).mark_circle().encode(
alt.X(smaa_cn2si[category], title=str(category)),
alt.Y(smaa_cn2si["balance"], title="log(Numerator / Denominator)"),
alt.Y(smaa_cn2si["rankratioviz_balance"],
title="log(Numerator / Denominator)"),
color=alt.Color(
smaa_cn2si[category],
title=str(category),
Expand All @@ -217,7 +222,8 @@ def gen_sample_plot(table, metadata, category, palette='Set1'):
# Save JSON for sample plot (including the column-identifying dict from
# earlier).
sample_logratio_chart_json = sample_logratio_chart.to_dict()
sample_logratio_chart_json["datasets"]["col_names"] = smaa_cn2si
col_names_ds = "rankratioviz_col_names"
sample_logratio_chart_json["datasets"][col_names_ds] = smaa_cn2si
return sample_logratio_chart_json


Expand Down
17 changes: 10 additions & 7 deletions rankratioviz/support_files/rankratioviz.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ ssmv.botTaxa = undefined;
// We set ssmv.selectMicrobes to undefined when no select microbes file has
// been provided yet.
ssmv.selectMicrobes = undefined;
// Abstracted frequently used long strings
ssmv.col_names = "rankratioviz_col_names";
ssmv.balance_col = "rankratioviz_balance";

ssmv.makeRankPlot = function(spec) {
vegaEmbed("#rankPlot", spec, {"actions": false}).then(function(result) {
Expand Down Expand Up @@ -119,19 +122,19 @@ ssmv.filterTaxa = function(inputText, searchType) {
// TODO: is there a risk of having metadata column names that clash
// with taxon IDs? I don't think so, but might be worth changing up how
// this works to make this safer.
taxa = Object.keys(ssmv.samplePlotJSON["datasets"]["col_names"]);
taxa = Object.keys(ssmv.samplePlotJSON["datasets"][ssmv.col_names]);
}
var filteredTaxa = [];
var taxonomyPart;
var ranksOfTaxon;
for (var ti = 0; ti < taxa.length; ti++) {
// NOTE this check filters out sample metadata/etc.
// Everything on or after position 3 in the col_names dataset
// Everything on or after position 3 in the rankratioviz_col_names dataset
// (0-indexed) is a taxon.
// TODO when we add all metadata here, we'll need to save this "3"
// value (for the number of leading metadata columns) in the JSON file
// so we can change it up for different datasets.
if (ssmv.samplePlotJSON["datasets"]["col_names"][taxa[ti]] >= 3) {
if (ssmv.samplePlotJSON["datasets"][ssmv.col_names][taxa[ti]] >= 3) {
if (searchType === "text") {
// Just use the input text to literally search through taxa for
// matches (including semicolons corresponding to rank
Expand Down Expand Up @@ -196,7 +199,7 @@ ssmv.sumAbundancesForSampleTaxa = function(sampleRow, taxa) {
// just spent an hour debugging.
var zfi = parseFloat(document.getElementById("zeroFillInput").value);
for (var t = 0; t < taxa.length; t++) {
var colIndex = ssmv.samplePlotJSON["datasets"]["col_names"][taxa[t]];
var colIndex = ssmv.samplePlotJSON["datasets"][ssmv.col_names][taxa[t]];
if (sampleRow[colIndex] === 0) {
abundance += zfi;
}
Expand Down Expand Up @@ -306,7 +309,7 @@ ssmv.changeSamplePlot = function(updateBalanceFunc, updateRankColorFunc) {
vega.truthy,
// column int for "balance" (this is the column for each
// sample we want to change)
ssmv.samplePlotJSON["datasets"]["col_names"]["balance"],
ssmv.samplePlotJSON["datasets"][ssmv.col_names][ssmv.balance_col],
// function to run to determine what the new balances are
updateBalanceFunc
)).run();
Expand Down Expand Up @@ -347,8 +350,8 @@ ssmv.updateSamplePlotSingle = function() {
// microbes.
var dataName = ssmv.samplePlotJSON["data"]["name"];

ssmv.taxonLowCol = ssmv.samplePlotJSON["datasets"]["col_names"][ssmv.newTaxonLow];
ssmv.taxonHighCol = ssmv.samplePlotJSON["datasets"]["col_names"][ssmv.newTaxonHigh];
ssmv.taxonLowCol = ssmv.samplePlotJSON["datasets"][ssmv.col_names][ssmv.newTaxonLow];
ssmv.taxonHighCol = ssmv.samplePlotJSON["datasets"][ssmv.col_names][ssmv.newTaxonHigh];
ssmv.changeSamplePlot(
ssmv.updateBalanceSingle,
ssmv.updateRankColorSingle
Expand Down

0 comments on commit adb4a1d

Please sign in to comment.