Skip to content

Commit

Permalink
FAIRSpec-gui fixes for substructure search
Browse files Browse the repository at this point in the history
  • Loading branch information
BobHanson committed Dec 5, 2024
1 parent 86420fb commit e00fe41
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 102 deletions.
8 changes: 8 additions & 0 deletions docs/examples/assets/FAIRSpec-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ IFD = {
contentHeader: null,
cache: {}
}

IFD.MODE_NONE = "none";
IFD.MODE_COMPOUNDS = "compounds";
IFD.MODE_STRUCTURES = "structures";
IFD.MODE_SPECTRA = "spectra";
IFD.MODE_SAMPLES = "samples";
IFD.MODE_SAMPLESPECTRA = "samplespectra";

49 changes: 39 additions & 10 deletions docs/examples/assets/FAIRSpec-get.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,20 @@

;(function() {

IFD.getCollection = function(aidID) {
return IFD.collections[aidID];
}

IFD.getSmilesForStructureID = function(aidID, id) {
var struc = IFD.collections[aidID].structures[id];
var struc = IFD.getCollection(aidID).structures[id];
if (!struc) return "";
var reps = struc.representations;
var types = IFD.getRepTypes(reps);
return types.smiles && types.smiles.data;
}

IFD.getSpectrumIDsForSample = function(aidID,id) {
var samplespecs = IFD.collections[aidID]["sample-spectra associations"];
var samplespecs = IFD.getCollection(aidID)["sample-spectra associations"];
var items = IFD.items[aidID].samplespectra;
for (var i = 0; i < items.length; i++) {
var assoc = samplespecs[items[i]];
Expand All @@ -67,11 +71,11 @@ IFD.getSpectrumIDsForSample = function(aidID,id) {
}

IFD.getCompoundIndexesForText = function(aidID, text) {
var compounds = IFD.collections[aidID].compounds;
var compounds = IFD.getCollection(aidID).compounds;
var keys = IFD.getCompoundCollectionKeys();
var ids = [];
var citems = IFD.items[aidID].compounds;
text = text.toLowerCase();
var citems = IFD.items[aidID].compounds;
for (var i = 0; i < citems.length; i++) {
var assoc = compounds[citems[i]];
var found = false;
Expand All @@ -90,8 +94,8 @@ var testText = function(s, text) {
}

IFD.getCompoundIndexesForStructures = function(aidID, strucIDs) {
var compounds = IFD.collections[aidID].compounds;
var structures = IFD.collections[aidID].structures;
var compounds = IFD.getCollection(aidID).compounds;
var structures = IFD.getCollection(aidID).structures;
var keys = IFD.getCompoundCollectionKeys();
var ids = [];
var citems = IFD.items[aidID].compounds;
Expand All @@ -112,7 +116,7 @@ IFD.getCompoundIndexesForStructures = function(aidID, strucIDs) {
}

IFD.getStructureIDsForSpectra = function(aidID, specIDs) {
var compounds = IFD.collections[aidID].compounds;
var compounds = IFD.getCollection(aidID).compounds;
var keys = IFD.getCompoundCollectionKeys();
var ids = [];
var items = IFD.items[aidID].compounds;
Expand Down Expand Up @@ -233,19 +237,44 @@ IFD.shortType = function(type) {
IFD.getCollectionSetById = function(aid) {
IFD.byID = !!aid.collectionSet.itemsByID;
IFD.itemsKey = (IFD.byID ? "itemsByID" : "items");
IFD.collectionKeys = (IFD.byID ? null : {});
IFD.collectionKeys = {};
var collections = aid.collectionSet.itemsByID || aid.collectionSet.items;
var dc = IFD.collections[aid.id] = {};
if (IFD.findingAidID == '.')
IFD.collections['.'] = dc;
for (var i in collections) {
c = collections[i];
dc[c.id] = c.items || c.itemsById;
dc[c.id] = c.items || c.itemsByID;
if (!IFD.byID) {
IFD.collectionKeys[c.id] = c.collections; // compounds only
}
}
return dc;
}

IFD.getCollectionSetItems = function(aid) {
var dc = IFD.getCollectionSetById(aid);
var dItems = IFD.items[aid.id] = {};
if (IFD.findingAidID == '.')
IFD.items['.'] = dItems;
if (dc[IFD.MODE_SAMPLES]) {
dItems[IFD.MODE_SAMPLES] = IFD.getIDs(dc[IFD.MODE_SAMPLES]);
}
if (dc[IFD.MODE_COMPOUNDS]) {
dItems[IFD.MODE_COMPOUNDS] = IFD.getIDs(dc[IFD.MODE_COMPOUNDS]);
}
if (dc[IFD.MODE_STRUCTURES]) {
dItems[IFD.MODE_STRUCTURES] = IFD.getIDs(dc[IFD.MODE_STRUCTURES]);
}
if (dc[IFD.MODE_SPECTRA]) {
dItems[IFD.MODE_SPECTRA] = IFD.getIDs(dc[IFD.MODE_SPECTRA]);
}
if (dc["sample-spectra associations"]) {
dItems[IFD.MODE_SAMPLESPECTRA] = IFD.getIDs(dc["sample-spectra associations"]);
}
return dItems;
}

IFD.getCompoundCollectionKeys = function() {
var ckeys = IFD.collectionKeys.compounds;
if (ckeys && !ckeys[0]) {
Expand All @@ -272,7 +301,7 @@ IFD.getSMILES = function(aidID, retIDs, retSMILES, allowReactions, withAidID) {
var structureIDs = IFD.items[aidID]["structures"];
for (var i = 0; i < structureIDs.length; i++) {
var id = structureIDs[i];
var struc = IFD.collections[aidID].structures[id];
var struc = IFD.getCollection(aidID).structures[id];
var reps = struc.representations;
var types = IFD.getRepTypes(reps, "smiles");
if (types.smiles) {
Expand Down
89 changes: 44 additions & 45 deletions docs/examples/assets/FAIRSpec-gui.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,15 @@
var MAIN_SEARCH_PROP = "main_search_prop";
var MAIN_SEARCH = "main_search";
var MAIN_SUMMARY = "main_summary";
var MODE_NONE = "none";
var MODE_COMPOUNDS = "compounds";
var MODE_STRUCTURES = "structures";
var MODE_SPECTRA = "spectra";
var MODE_SAMPLES = "samples";

var NMRDB_PREDICT_SMILES = "https://www.nmrdb.org/service.php?name=%TYPE%-prediction&smiles=%SMILES%";
// where type = 1h, 13c, cosy, hmbc, hsqc

var aLoading = null;
var divId = 0;

var dirFor = function(aidID) {
return IFD.properties.baseDir + "/" + aidID;
return IFD.properties.baseDir + (IFD.findingAidID == '.' ? "" : "/" + aidID);
}

var fileFor = function(aidID, fname) {
Expand Down Expand Up @@ -68,7 +64,7 @@

// external
IFD.loadFindingAids = function() {
var aids = IFD.findingAids || [];
var aids = IFD.findingAids || ["."];
IFD.aidIDs = [];
var s = (aids.length == 1 ? null : '<select id=articles onchange="IFD.loadSelected(this.selectedOptions[0].value)"><option value="">Select a Finding Aid</option>')
// set up for ./name/IFD.findingAid.json
Expand Down Expand Up @@ -126,7 +122,7 @@
aidID || (aidID = "");
setResults("");
var dir = dirFor(aidID);
if (IFD.findingAidID == aidID) return;
if (IFD.findingAidID == '.' || IFD.findingAidID == aidID) return;
IFD.findingAidID = aidID;
if (IFD.findingAidDir == dir) return;
IFD.findingAidDir = dir;
Expand All @@ -137,7 +133,7 @@
IFD.findingAidFile = fileFor(aidID, IFD.properties.findingAidFileName);
var aid = IFD.cacheGet(aidID);
if (aid == null) {
$.ajax({url:IFD.findingAidFile, dataType:"json", success:callbackLoaded, error:callbackLoadFailed});
$.ajax({url:IFD.findingAidFile, dataType:"json", success:callbackLoaded, error:function(){callbackLoadFailed()}});
} else {
loadAid(aid, collection);
}
Expand All @@ -146,8 +142,8 @@
//external
IFD.showSamples = function(aidID, ids) {
IFD.select(aidID);
setMode(MODE_SAMPLES);
ids || (ids = IFD.items[aidID][MODE_SAMPLES]);
setMode(IFD.MODE_SAMPLES);
ids || (ids = IFD.items[aidID][IFD.MODE_SAMPLES]);
var s = "<table>";
for (var i = 0; i < ids.length; i++) {
s += "<tr class=\"tableRow" + (i%2) + "\">" + showSample(aidID,ids[i]) + "</tr>";
Expand All @@ -160,22 +156,27 @@
IFD.showCompounds = function(aidID, ids) {
loadMainSummary(IFD.aid, false);
IFD.select(aidID);
setMode(MODE_COMPOUNDS);
ids || (ids = IFD.items[aidID][MODE_COMPOUNDS]);
var s = "<table>";
for (var i = 0; i < ids.length; i++) {
s += "<tr class=\"tablerow" + (i%2) + "\">" + showCompound(aidID,ids[i]) + "</tr>";
setMode(IFD.MODE_COMPOUNDS);
ids || (ids = IFD.items[aidID][IFD.MODE_COMPOUNDS]);
var s;
if (ids.length == 0) {
s = "no compounds found";
} else {
s = "<table>";
for (var i = 0; i < ids.length; i++) {
s += "<tr class=\"tablerow" + (i%2) + "\">" + showCompound(aidID,ids[i]) + "</tr>";
}
s += "</table>";
}
s += "</table>";
setResults(s);
}

//external
IFD.showSpectra = function(aidID, ids) {
loadMainSummary(IFD.aid, false);
IFD.select(aidID);
setMode(MODE_SPECTRA);
ids || (ids = IFD.items[aidID][MODE_SPECTRA]);
setMode(IFD.MODE_SPECTRA);
ids || (ids = IFD.items[aidID][IFD.MODE_SPECTRA]);
var s = "<table>";
for (var i = 0; i < ids.length; i++) {
s += "<tr class=\"tableRow" + (i%2) + "\">" + showSpectrum(aidID,ids[i]) + "</tr>";
Expand All @@ -188,8 +189,8 @@
IFD.showStructures = function(aidID, ids) {
loadMainSummary(IFD.aid, false);
IFD.select(aidID);
setMode(MODE_STRUCTURES);
ids || ids === false || (ids = IFD.getItems(aidID, MODE_STRUCTURES));
setMode(IFD.MODE_STRUCTURES);
ids || ids === false || (ids = IFD.getItems(aidID, IFD.MODE_STRUCTURES));
var s = showCompoundStructures(aidID,ids, false, true);
setResults(s);
}
Expand All @@ -204,7 +205,7 @@
}

IFD.showAid = function(aidID) {
window.open(fileFor(aidID, IFD.properties.findingAidFileName), "_blank");
window.open(fileFor(IFD.findingAidID, IFD.properties.findingAidFileName), "_blank");
}

IFD.showVersion = function(aid) {
Expand Down Expand Up @@ -234,7 +235,7 @@
loadAid(aid);
}

var callbackLoadFailed = function(x,y,z) {
var callbackLoadFailed = function(x) {
alert([x,y,z]);
return;
}
Expand Down Expand Up @@ -413,49 +414,47 @@
}

var addTopRow = function(aid) {
var dc = IFD.getCollectionSetById(aid);
var dItems = IFD.getCollectionSetItems(aid);
var id = aid.id;
var dItems = IFD.items[id] = {};
var sep = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
//+ ==&gt;
+ "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
var s = "";
if (dc.samples) {
var items = dItems[IFD.MODE_SAMPLES];
if (items) {
if (s)
s += sep;
var items = dItems[MODE_SAMPLES] = IFD.getIDs(dc.samples);
s += "<a href=\"javascript:IFD.showSamples('"+aid.id+"')\">Samples(" + items.length + ")</a>";
}
if (dc.compounds) {
var items = dItems[MODE_COMPOUNDS] = IFD.getIDs(dc.compounds);
items = dItems[IFD.MODE_COMPOUNDS];
if (items) {
if (s)
s += sep;
s += "<a href=\"javascript:IFD.showCompounds('"+id+"')\">Compounds(" + items.length + ")</a>";
}
if (dc.structures) {
var items = dItems[MODE_STRUCTURES] = IFD.getIDs(dc.structures);
items = dItems[IFD.MODE_STRUCTURES];
if (items) {
if (s)
s += sep;
s += "<a href=\"javascript:IFD.showStructures('"+aid.id+"')\">Structures(" + items.length + ")</a>";
}
if (dc.spectra) {
var items = dItems[MODE_SPECTRA] = IFD.getIDs(dc.spectra);
items = dItems[IFD.MODE_SPECTRA];
if (items) {
if (s)
s += sep;
s += "<a href=\"javascript:IFD.showSpectra('"+id+"')\">Spectra(" + items.length + ")</a>";
}

// TODO fold this information into "samples"
if (dc["sample-spectra associations"]) {
var items = dItems["samplespectra"] = IFD.getIDs(dc["sample-spectra associations"]);
items = dItems[IFD.MODE_SAMPLESPECTRA];
if (items) {
// TODO fold this information into "samples"
// s += "<a href=\"javascript:IFD.showSampleSpectraAssociations('"+aid.id+"')\">Sample-Spectra Associations(" + items.length + ")</a>";
}
return "<tr><td><b>Collections:</b>&nbsp;&nbsp;</td><td>"
+ (s ? s : "(none found)") + "</td></tr>";
}

var showSample = function(aidID,id) {
var sample = IFD.collections[aidID].samples[id];
var sample = IFD.getCollection(aidID).samples[id];
var specids = IFD.getSpectrumIDsForSample(aidID, id);
var structureIDs = IFD.getStructureIDsForSpectra(aidID,specids);
var s = getHeader("Sample/s", "Sample " + id);
Expand Down Expand Up @@ -496,7 +495,7 @@
}

var showSpectrum = function(aidID,id) {
var spec = IFD.collections[aidID].spectra[id];
var spec = IFD.getCollection(aidID).spectra[id];
var structureIDs = IFD.getStructureIDsForSpectra(aidID, [id]);
var sampleID = spec.properties && spec.properties.originating_sample_id;
var sid = (IFD.byID ? id : spec.id);
Expand Down Expand Up @@ -524,7 +523,7 @@
}

var showCompound = function(aidID,id) {
var cmpd = IFD.collections[aidID].compounds[id];
var cmpd = IFD.getCollection(aidID).compounds[id];
var keys = IFD.getCompoundCollectionKeys();
var structureIDs = cmpd[IFD.itemsKey][keys.structures];
var spectraIDs = cmpd[IFD.itemsKey][keys.spectra];
Expand Down Expand Up @@ -577,15 +576,15 @@
var showCompoundStructure = function(aidID, id, showID, tableRow) {
var cl = (tableRow > 0 ? " class=tablerow" + (tableRow%2) : "");
var s = "<td" + cl + "><table cellpadding=10><tr>";
var struc = IFD.collections[aidID].structures[id];
var struc = IFD.getCollection(aidID).structures[id];
var sid = struc.id;
var props = struc.properties;
var reps = struc.representations;

s += "<td rowspan=2 valign=\"top\">";
if (showID) {
var h = (id.indexOf("Structure") == 0 ? removeUnderline(sid) : "Structure " + sid);
s += "<span class=structurehead>"+ (IFD.resultsMode == MODE_STRUCTURES ? getHeader("Structure/s", h) : h) + "</span><br>";
s += "<span class=structurehead>"+ (IFD.resultsMode == IFD.MODE_STRUCTURES ? getHeader("Structure/s", h) : h) + "</span><br>";
}
v = IFD.getStructureVisual(reps);
if (v){
Expand All @@ -606,7 +605,7 @@
}

var showCompoundSpectra = function(aidID,ids,smiles,withTitle) {
ids || (ids = IFD.items[aidID][MODE_SPECTRA]);
ids || (ids = IFD.items[aidID][IFD.MODE_SPECTRA]);
var s = "<table>"
if (withTitle)
s += "<tr><td style=\"width:100px\" valign=top><span class=spectitle>Spectra</span></td></tr>";
Expand All @@ -632,7 +631,7 @@

var setResults = function(s) {
addOrAppendJQ("#results",s, true);
loadContents(!!s);
loadContents(s && !s.startsWith("no "));
}

var loadContents = function(hasContent) {
Expand Down Expand Up @@ -754,7 +753,7 @@
}

var addCompoundSpectrumRow = function(aidID, id, smiles, tableRow) {
var spec = IFD.collections[aidID].spectra[id];
var spec = IFD.getCollection(aidID).spectra[id];
var cl = (tableRow > 0 ? " class=tablerow" + (tableRow%2) : "");
var s = "<tr><td"+ cl + " id='q1' rowspan=2 style=\"width:100px\" valign=top>"
+ (IFD.byID ? id : "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ IFD = {
contentHeader: null,
cache: {}
}

IFD.MODE_NONE = "none";
IFD.MODE_COMPOUNDS = "compounds";
IFD.MODE_STRUCTURES = "structures";
IFD.MODE_SPECTRA = "spectra";
IFD.MODE_SAMPLES = "samples";
IFD.MODE_SAMPLESPECTRA = "samplespectra";

Loading

0 comments on commit e00fe41

Please sign in to comment.