Commit e2f6baa9 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

extracting common interface for targeting database from drug

this interface can be used by rna chemicals later
parent 545ecb8d
......@@ -107,7 +107,7 @@ DrugPanel.prototype.addResultTab = function(query, elements) {
contentElement.appendChild(contentDiv);
contentDiv.appendChild(this.createDrugHeader(elements.drug));
contentDiv.appendChild(this.createDrugHeader(elements.element));
var tableDiv = document.createElement("table");
tableDiv.className = "table table-bordered";
......
......@@ -18,7 +18,6 @@ function AbstractDbOverlay(params) {
this._elementsByQuery = [];
this._drugsByQuery = [];
this.registerListenerType('onSearch');
this.registerListenerType('onTargetVisibilityChange');
......@@ -97,8 +96,6 @@ AbstractDbOverlay.prototype.searchNamesByTarget = function(element) {
var self = this;
var query = self.encodeQuery(AbstractDbOverlay.QueryType.SEARCH_BY_TARGET, element);
ServerConnector.getSessionData().setQuery({type:self.getName(),query:query});
return new Promise(function(resolve, reject) {
if (self._elementsByQuery[query] !== undefined) {
resolve(self._elementsByQuery[query]);
......@@ -156,7 +153,7 @@ AbstractDbOverlay.prototype.searchByEncodedQuery = function(originalQuery) {
if (query.type === AbstractDbOverlay.QueryType.SEARCH_BY_QUERY) {
return this.searchByQuery(query.query, query.perfect);
} else if (query.type === AbstractDbOverlay.QueryType.SEARCH_BY_TARGET) {
return this.searchByTarget(query.target);
return this.searchNamesByTarget(query.target);
} else if (query.type === AbstractDbOverlay.QueryType.SEARCH_BY_COORDINATES) {
return this.searchByCoordinates(query.modelId, query.coordinates);
} else {
......@@ -168,32 +165,4 @@ AbstractDbOverlay.prototype.clear = function() {
return this.searchByQuery("");
};
AbstractDbOverlay.prototype.getDetailDataByIdentifiedElement = function(element, general) {
var self = this;
if (general) {
return self.searchNamesByTarget(element);
} else {
return new Promise(function(resolve){
var result =[];
var queries =self.getQueries();
for (var i=0;i<queries.length;i++) {
var drugs = self._drugsByQuery[queries[i]];
for (var j=0;j<drugs.length;j++){
var drug = drugs[j];
var targets = drug.getTargets();
for (var k=0;k<targets.length;k++) {
var elements = targets[k].getTargetElements();
for (var l=0;l<elements.length;l++) {
if (element.equals(elements[l])) {
result.push(drug.getName());
}
}
}
}
}
resolve(result);
});
}
};
module.exports = AbstractDbOverlay;
"use strict";
var logger = require('../../logger');
var Promise = require("bluebird");
var IdentifiedElement = require('../data/IdentifiedElement');
var AbstractDbOverlay = require('./AbstractDbOverlay');
var ServerConnector = require('../../ServerConnector');
function AbstractTargettingDbOverlay(params) {
// call super constructor
AbstractDbOverlay.call(this, params);
}
AbstractTargettingDbOverlay.prototype = Object.create(AbstractDbOverlay.prototype);
AbstractTargettingDbOverlay.prototype.constructor = AbstractTargettingDbOverlay;
AbstractTargettingDbOverlay.prototype.getElementsByQuery = function(query) {
var self = this;
return new Promise(function(resolve){
var i;
var queryId = null;
var queries =self.getQueries();
for (i=0;i<queries.length;i++) {
if (queries[i]===query) {
queryId = i;
}
}
if (queryId===null) {
throw new Error("Invalid query: "+query);
}
var colourId = queryId;
var elements = self._elementsByQuery[query];
var result = [];
if (elements.length>0) {
if (elements.length>1) {
logger.warn("More than one element per query not implemented");
}
var element = elements[0];
result.element = element;
var iconCounter = 1;
for (i=0; i<element.getTargets().length; i++) {
var target = element.getTargets()[i];
var icon = self.getIcon(colourId, iconCounter++);
if (target.getTargetElements().length===0) {
icon = null;
}
result.push({target:target, icon:icon});
}
}
resolve(result);
});
};
AbstractTargettingDbOverlay.prototype.searchBySingleQuery = function(originalQuery) {
var self = this;
return new Promise(function(resolve, reject) {
var query = self.encodeQuery(AbstractDbOverlay.QueryType.SEARCH_BY_QUERY, originalQuery);
if (self._elementsByQuery[query] !== undefined) {
resolve(self._elementsByQuery[query]);
} else {
return self.getElementsByQueryFromServer({
query:originalQuery
}).then(function(elements) {
self._elementsByQuery[query] = elements;
var identifiedElements = [];
for (var i=0;i<elements.length;i++) {
var targets = elements[i].getTargets();
for (var j=0;j<targets.length;j++) {
var target = targets[j];
for (var k=0;k<target.targetElements;k++) {
identifiedElements.push(new IdentifiedElement(target.targetElements[i]));
}
}
}
return self.getMap().fetchIdentifiedElements(identifiedElements, true);
}).then(function(){
resolve(self._elementsByQuery[query]);
}).catch(reject);
}
});
};
AbstractTargettingDbOverlay.prototype.getIdentifiedElements = function(){
var self = this;
return new Promise(function(resolve){
var queries = self.getQueries();
var result = [];
var colourId = 0;
for (var i=0;i<queries.length;i++) {
var query = queries[i];
var elements = self._elementsByQuery[query];
for (var j=0;j<elements.length;j++) {
var element = elements[j];
var targetElements = self.createIdentifiedElementsForTargetingClass(element, colourId);
result.push.apply(result, targetElements);
colourId++;
}
}
resolve(result);
});
};
AbstractTargettingDbOverlay.prototype.getDetailDataByIdentifiedElement = function(element, general) {
var self = this;
if (general) {
return self.searchNamesByTarget(element);
} else {
return new Promise(function(resolve){
var result =[];
var queries =self.getQueries();
for (var i=0;i<queries.length;i++) {
var drugs = self._elementsByQuery[queries[i]];
for (var j=0;j<drugs.length;j++){
var drug = drugs[j];
var targets = drug.getTargets();
for (var k=0;k<targets.length;k++) {
var elements = targets[k].getTargetElements();
for (var l=0;l<elements.length;l++) {
if (element.equals(elements[l])) {
result.push(drug.getName());
}
}
}
}
}
resolve(result);
});
}
};
module.exports = AbstractTargettingDbOverlay;
......@@ -5,7 +5,7 @@ var logger = require('../../logger');
var Promise = require("bluebird");
var IdentifiedElement = require('../data/IdentifiedElement');
var AbstractDbOverlay = require('./AbstractDbOverlay');
var AbstractTargettingDbOverlay = require('./AbstractTargettingDbOverlay');
var ServerConnector = require('../../ServerConnector');
......@@ -13,137 +13,19 @@ function DrugDbOverlay(params) {
params.iconType= "drug";
params.iconColorStart = 5
// call super constructor
AbstractDbOverlay.call(this, params);
AbstractTargettingDbOverlay.call(this, params);
}
DrugDbOverlay.prototype = Object.create(AbstractDbOverlay.prototype);
DrugDbOverlay.prototype = Object.create(AbstractTargettingDbOverlay.prototype);
DrugDbOverlay.prototype.constructor = DrugDbOverlay;
DrugDbOverlay.prototype.getElementsByQuery = function(query) {
var self = this;
return new Promise(function(resolve){
var i;
var queryId = null;
var queries =self.getQueries();
for (i=0;i<queries.length;i++) {
if (queries[i]===query) {
queryId = i;
}
}
if (queryId===null) {
throw new Error("Invalid query: "+query);
}
var colourId = queryId;
var drugs = self._drugsByQuery[query];
var result = [];
if (drugs.length>0) {
if (drugs.length>1) {
logger.warn("More than one drug per query not implemented");
}
var drug = drugs[0];
result.drug = drug;
var iconCounter = 1;
for (i=0; i<drug.getTargets().length; i++) {
var target = drug.getTargets()[i];
var icon = self.getIcon(colourId, iconCounter++);
if (target.getTargetElements().length===0) {
icon = null;
}
result.push({target:target, icon:icon});
}
}
resolve(result);
});
};
DrugDbOverlay.prototype.searchBySingleQuery = function(originalQuery) {
var self = this;
return new Promise(function(resolve, reject) {
var query = self.encodeQuery(AbstractDbOverlay.QueryType.SEARCH_BY_QUERY, originalQuery);
if (self._elementsByQuery[query] !== undefined) {
resolve(self._elementsByQuery[query]);
} else {
return ServerConnector.getDrugsByQuery({
query:originalQuery
}).then(function(drugs) {
self._drugsByQuery[query] = drugs;
var result = [];
for (var i=0;i<drugs.length;i++) {
var targets = drugs[i].getTargets();
for (var j=0;j<targets.length;j++) {
var target = targets[j];
for (var k=0;k<target.targetElements;k++) {
result.push(new IdentifiedElement(target.targetElements[i]));
}
}
}
self._elementsByQuery[query]=result;
return self.getMap().fetchIdentifiedElements(result, true);
}).then(function(){
resolve(self._elementsByQuery[query]);
}).catch(reject);
}
});
};
DrugDbOverlay.prototype.getIdentifiedElements = function(){
var self = this;
return new Promise(function(resolve){
var queries = self.getQueries();
var result = [];
var colourId = 0;
for (var i=0;i<queries.length;i++) {
var query = queries[i];
var drugs = self._drugsByQuery[query];
for (var j=0;j<drugs.length;j++) {
var drug = drugs[j];
var drugElements = self.createIdentifiedElementsForTargetingClass(drug, colourId);
result.push.apply(result, drugElements);
colourId++;
}
}
resolve(result);
});
};
DrugDbOverlay.prototype.getNamesByTargetFromServer = function(param) {
return ServerConnector.getDrugNamesByTarget(param);
}
DrugDbOverlay.prototype.getDetailDataByIdentifiedElement = function(element, general) {
var self = this;
if (general) {
return self.searchNamesByTarget(element);
} else {
return new Promise(function(resolve){
var result =[];
var queries =self.getQueries();
for (var i=0;i<queries.length;i++) {
var drugs = self._drugsByQuery[queries[i]];
for (var j=0;j<drugs.length;j++){
var drug = drugs[j];
var targets = drug.getTargets();
for (var k=0;k<targets.length;k++) {
var elements = targets[k].getTargetElements();
for (var l=0;l<elements.length;l++) {
if (element.equals(elements[l])) {
result.push(drug.getName());
}
}
}
}
}
resolve(result);
});
}
};
DrugDbOverlay.prototype.getElementsByQueryFromServer = function(param) {
return ServerConnector.getDrugsByQuery(param);
}
module.exports = DrugDbOverlay;
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