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

structure for grouping search results

parent d3cb577d
"use strict";
var Alias = require("./Alias");
var BioEntity = require("./BioEntity");
var Reaction = require("./Reaction");
// noinspection JSUnusedLocalSymbols
var logger = require('../../logger');
/**
* Class representing merged search bioEntities.
*
* @param bioEntity
* initial bioEntity from which group is created
*/
function SearchAliasGroup(bioEntity) {
if (!(bioEntity instanceof BioEntity)) {
throw new Error("Invalid argument");
}
this._bioEntites = [bioEntity];
}
SearchAliasGroup.prototype.bioEntityMatch = function (newBioEntity) {
var result = true;
var self = this;
for (var i = 0; i < self._bioEntites.length; i++) {
var bioEntity = self._bioEntites[i];
if (self.bioEntityComparator(bioEntity, newBioEntity) !== 0) {
result = false;
}
}
return result;
};
SearchAliasGroup.prototype.addBioEntity = function (newBioEntity) {
this._bioEntites.push(newBioEntity);
};
SearchAliasGroup.prototype.getBioEntities = function () {
return this._bioEntites;
};
SearchAliasGroup.prototype.bioEntityComparator = function (bioEntity1, bioEntity2) {
if (bioEntity1 instanceof Alias && bioEntity2 instanceof Alias) {
if (bioEntity1.getName() !== bioEntity2.getName()) {
return -1;
}
if (bioEntity1.getModelId() !== bioEntity2.getModelId()) {
return -2;
}
if (bioEntity1.getCompartmentId() !== bioEntity2.getCompartmentId()) {
return -3;
}
if (bioEntity1.getType() !== bioEntity2.getType()) {
return -4;
}
if (bioEntity1.getOther("structuralState") !== bioEntity2.getOther("structuralState")) {
return -5;
}
var computeSerializedModifications = function (modifications) {
if (modifications === undefined) {
return [];
}
var result = [];
for (var i = 0; i < modifications.length; i++) {
var modification = modifications[i];
result.push(modification.name + "_" + modification.state);
}
result.sort();
return result;
};
var serializedModifications1 = computeSerializedModifications(bioEntity1.getOther("modifications"));
var serializedModifications2 = computeSerializedModifications(bioEntity2.getOther("modifications"));
if (serializedModifications1.length !== serializedModifications2.length) {
return -6;
}
for (var i = 0; i < serializedModifications1.length; i++) {
if (serializedModifications1[i] !== serializedModifications2[i]) {
return -7;
}
}
return 0;
}
if (bioEntity1 instanceof Reaction && bioEntity2 instanceof Reaction) {
if (bioEntity1.getId() !== bioEntity2.getId()) {
return -8;
}
return 0;
}
return -9;
};
SearchAliasGroup.prototype.setIcon = function (icon) {
this._icon = icon;
};
SearchAliasGroup.prototype.getIcon = function () {
return this._icon;
};
module.exports = SearchAliasGroup;
"use strict";
require("../../mocha-config");
var Alias = require('../../../../main/js/map/data/Alias');
var SearchBioEntityGroup = require('../../../../main/js/map/data/SearchBioEntityGroup');
var chai = require('chai');
var assert = chai.assert;
var logger = require('../../logger');
describe('SearchBioEntityGroup', function () {
beforeEach(function () {
logger.flushBuffer();
});
describe("constructor", function () {
it("with alias", function () {
var alias = helper.createAlias();
var group = new SearchBioEntityGroup(alias);
assert.ok(group);
assert.equal(logger.getWarnings().length, 0);
assert.ok(group.bioEntityMatch(alias));
});
it("with reaction", function () {
var reaction = helper.createReaction();
var group = new SearchBioEntityGroup(reaction);
assert.ok(group);
assert.equal(logger.getWarnings().length, 0);
assert.ok(group.bioEntityMatch(reaction));
});
it("with invalid", function () {
try {
new SearchBioEntityGroup({});
assert.ok(false, "Error expected");
} catch (e) {
assert.equal(e.message.indexOf("Error expected"), -1)
}
});
});
describe("bioEntityComparator", function () {
it("equal", function () {
var map = helper.createCustomMap();
var alias1 = helper.createAlias(map);
var alias2 = helper.createAlias(map);
var group = new SearchBioEntityGroup(alias1);
assert.ok(group.bioEntityComparator(alias1, alias2) === 0);
});
it("different name", function () {
var map = helper.createCustomMap();
var alias1 = helper.createAlias(map);
var alias2 = helper.createAlias(map);
alias2.setName("different name");
var group = new SearchBioEntityGroup(alias1);
assert.ok(group.bioEntityComparator(alias1, alias2) !== 0);
});
it("different type", function () {
var map = helper.createCustomMap();
var alias1 = helper.createAlias(map);
var alias2 = helper.createAlias(map);
alias2.setType("different type");
var group = new SearchBioEntityGroup(alias1);
assert.ok(group.bioEntityComparator(alias1, alias2) !== 0);
});
it("different state", function () {
var map = helper.createCustomMap();
var alias1 = helper.createAlias(map);
var alias2 = helper.createAlias(map);
var otherData = {structuralState: "different state"};
alias2.setOther(otherData);
var group = new SearchBioEntityGroup(alias1);
assert.ok(group.bioEntityComparator(alias1, alias2) !== 0);
});
it("different modifications", function () {
var map = helper.createCustomMap();
var alias1 = helper.createAlias(map);
var alias2 = helper.createAlias(map);
var otherData = {modifications: [{"name": "S250", "state": "PHOSPHORYLATED"}]};
alias2.setOther(otherData);
var group = new SearchBioEntityGroup(alias1);
assert.ok(group.bioEntityComparator(alias1, alias2) !== 0);
});
it("different class", function () {
var map = helper.createCustomMap();
var alias = helper.createAlias(map);
var reaction = helper.createReaction(map);
var group = new SearchBioEntityGroup(alias);
assert.ok(group.bioEntityComparator(alias, reaction) !== 0);
});
it("different map", function () {
var map = helper.createCustomMap();
var alias1 = helper.createAlias(map);
var alias2 = helper.createAlias(map);
alias2.setModelId(-1);
var group = new SearchBioEntityGroup(alias1);
assert.ok(group.bioEntityComparator(alias1, alias2) !== 0);
});
it("different compartment", function () {
var map = helper.createCustomMap();
var alias1 = helper.createAlias(map);
var alias2 = helper.createAlias(map);
alias2.setCompartmentId(-3);
var group = new SearchBioEntityGroup(alias1);
assert.ok(group.bioEntityComparator(alias1, alias2) !== 0);
});
it("different reactions", function () {
var map = helper.createCustomMap();
var reaction1 = helper.createReaction(map);
var reaction2 = helper.createReaction(map);
var group = new SearchBioEntityGroup(reaction1);
assert.ok(group.bioEntityComparator(reaction1, reaction2) !== 0);
});
});
it("addBioEntity", function () {
var map = helper.createCustomMap();
var alias1 = helper.createAlias(map);
var alias2 = helper.createAlias(map);
var group = new SearchBioEntityGroup(alias1);
group.addBioEntity(alias2);
assert.equal(2, group.getBioEntities().length);
});
it("setIcon", function () {
var map = helper.createCustomMap();
var alias1 = helper.createAlias(map);
var group = new SearchBioEntityGroup(alias1);
group.setIcon("icon.png");
assert.equal("icon.png", group.getIcon());
});
});
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