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

drug view factory removed

parent e8dcd0bf
......@@ -13,10 +13,12 @@ import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.annotation.data.Drug;
import lcsb.mapviewer.annotation.data.Target;
import lcsb.mapviewer.annotation.data.TargettingStructure;
import lcsb.mapviewer.annotation.services.ChEMBLParser;
import lcsb.mapviewer.annotation.services.DrugSearchException;
import lcsb.mapviewer.annotation.services.DrugbankHTMLParser;
import lcsb.mapviewer.annotation.services.PubmedParser;
import lcsb.mapviewer.annotation.services.PubmedSearchException;
import lcsb.mapviewer.annotation.services.annotators.AnnotatorException;
import lcsb.mapviewer.annotation.services.annotators.HgncAnnotator;
import lcsb.mapviewer.common.IProgressUpdater;
......@@ -31,7 +33,6 @@ import lcsb.mapviewer.model.map.species.Rna;
import lcsb.mapviewer.model.map.statistics.SearchType;
import lcsb.mapviewer.services.interfaces.ISearchHistoryService;
import lcsb.mapviewer.services.search.db.DbSearchCriteria;
import lcsb.mapviewer.services.view.AnnotationView;
/**
* Implementation of the service that allows access information about drugs.
......@@ -73,12 +74,6 @@ public class DrugService implements IDrugService {
@Autowired
private PubmedParser pubmedParser;
/**
* Factory object used for creation of {@link DrugView} elements.
*/
@Autowired
private DrugViewFactory drugViewFactory;
/**
* Service that manages search history.
*/
......@@ -304,7 +299,6 @@ public class DrugService implements IDrugService {
removeUnknownOrganisms(drug, searchCriteria.getOrganisms());
}
Collections.sort(drugList, new DrugView.NameComparator());
return drugList;
}
......@@ -396,17 +390,17 @@ public class DrugService implements IDrugService {
try {
List<Drug> chemicalList = drugBankParser.getDrugListByTarget(md);
for (Drug chemical : chemicalList) {
drugViewFactory.create(chemical);
cacheMiriamData(chemical);
}
} catch (DrugSearchException e) {
} catch (DrugSearchException | AnnotatorException e) {
logger.error("Problem with accessing info about drugbank for target: " + md, e);
}
try {
List<Drug> chemicalList = chEMBLParser.getDrugListByTarget(md);
for (Drug chemical : chemicalList) {
drugViewFactory.create(chemical);
cacheMiriamData(chemical);
}
} catch (DrugSearchException e) {
} catch (DrugSearchException | AnnotatorException e) {
logger.error("Problem with accessing info about chembl for target: " + md, e);
}
counter += 1;
......@@ -416,4 +410,25 @@ public class DrugService implements IDrugService {
}
}
private void cacheMiriamData(TargettingStructure targettingStructure) throws AnnotatorException {
Set<MiriamData> result = new HashSet<>();
result.addAll(targettingStructure.getSources());
for (Target target : targettingStructure.getTargets()) {
result.addAll(target.getGenes());
result.addAll(target.getReferences());
}
for (MiriamData miriamData : result) {
if (miriamData.getDataType().equals(MiriamType.PUBMED)) {
try {
pubmedParser.getPubmedArticleById(Integer.valueOf(miriamData.getResource()));
} catch (NumberFormatException | PubmedSearchException e) {
throw new AnnotatorException(e);
}
}
}
}
}
package lcsb.mapviewer.services.search.db.drug;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import com.google.gson.Gson;
import lcsb.mapviewer.annotation.data.Drug;
import lcsb.mapviewer.annotation.data.Target;
import lcsb.mapviewer.annotation.data.TargetType;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.services.overlay.IconManager;
import lcsb.mapviewer.services.overlay.IconType;
import lcsb.mapviewer.services.search.SearchResultFactory;
import lcsb.mapviewer.services.search.data.ElementIdentifier;
import lcsb.mapviewer.services.search.db.DrugTargetViewVisibilityComparator;
import lcsb.mapviewer.services.search.db.GeneRow;
import lcsb.mapviewer.services.search.db.TargetView;
import lcsb.mapviewer.services.search.db.TargetViewFactory;
import lcsb.mapviewer.services.view.AnnotationView;
/**
* Factory class for {@link DrugView} class.
*
* @author Piotr Gawron
*
*/
public class DrugViewFactory extends SearchResultFactory<Drug, DrugView> {
/**
* Default class logger.
*/
private static Logger logger = Logger.getLogger(DrugViewFactory.class);
/**
* Factory object used for creation of {@link TargetView} elements.
*/
@Autowired
private TargetViewFactory targetViewFactory;
@Override
public DrugView create(Drug drug) {
return create(drug, null, 0);
}
/**
* Creates {@link DrugView}.
*
* @param drug
* original Drug from which the data will be initialized
* @param model
* model where the view will be presented
* @param set
* which set of icons should be used
*
* @return {@link DrugView} object for given drug. links are placed according
* to model given in the parameter.
*/
public DrugView create(Drug drug, Model model, int set) {
DrugView result = new DrugView(drug);
if (drug == null) {
return result;
}
result.setName(drug.getName());
for (MiriamData md : drug.getSources()) {
AnnotationView view = getAnnotationViewFactory().create(md);
result.addDrugLink(view);
}
result.setDescription(drug.getDescription());
result.setBloodBrainBarrier(drug.getBloodBrainBarrier());
result.addBrandNames(drug.getBrandNames());
result.addSynonyms(drug.getSynonyms());
int differentNames = 0;
for (Target target : drug.getTargets()) {
result.addTargetRow(targetViewFactory.create(target, model));
}
Collections.sort(result.getTargetRows(), new DrugTargetViewVisibilityComparator());
for (TargetView row : result.getTargetRows()) {
if (row.getVisible()) {
String icon;
icon = IconManager.getInstance().getIconForIndex(differentNames++, IconType.DRUG, set);
row.setIcon(icon);
if (row.getSelectable()) {
row.setSelected(true);
}
}
}
Map<String, Integer> dbEntries = new HashMap<String, Integer>();
for (AnnotationView dl : result.getDrugLinks()) {
Integer counter = dbEntries.get(dl.getName());
if (counter == null) {
counter = 1;
} else {
counter += 1;
}
dbEntries.put(dl.getName(), counter);
}
for (Entry<String, Integer> entry : dbEntries.entrySet()) {
if (entry.getValue() == 1) {
entry.setValue(null);
} else {
entry.setValue(0);
}
}
for (AnnotationView dl : result.getDrugLinks()) {
Integer counter = dbEntries.get(dl.getName());
if (counter != null) {
counter += 1;
dbEntries.put(dl.getName(), counter);
dl.setName(dl.getName() + " (" + counter + ")");
}
}
return result;
}
@Override
public String createGson(DrugView object) {
return new Gson().toJson(object);
}
/**
* Creates list of markers for the given geneName.
*
* @param annotation
* annotation of a gene (hgnc)
* @param icon
* what icon should be used by markers
* @param models
* list of models where we want to create markers
* @return list of markers to be presented on the client side for the given
* geneName
*/
protected List<ElementIdentifier> geneToOverlayList(AnnotationView annotation, String icon, List<Model> models) {
List<ElementIdentifier> result = new ArrayList<>();
for (Model m : models) {
List<Element> species = m.getElementsByName(annotation.getName());
for (Element species2 : species) {
result.add(new ElementIdentifier(species2, icon));
}
}
return result;
}
/**
* Creates list of markers for the given chembl target.
*
* @param complexTarget
* chembl target
* @param models
* list of models where we want to create markers
* @return list of markers to be presented on the client side for the given
* chembl target
*/
protected List<ElementIdentifier> complexToOverlayList(TargetView complexTarget, Collection<Model> models) {
List<ElementIdentifier> result = new ArrayList<>();
for (Model m : models) {
for (Element element : m.getElements()) {
if (elementMatch(complexTarget, element)) {
result.add(new ElementIdentifier(element, complexTarget.getIcon()));
}
}
}
return result;
}
@Override
public List<ElementIdentifier> searchResultToElementIdentifier(DrugView drug, Model model) {
List<ElementIdentifier> result = new ArrayList<>();
List<Model> models = new ArrayList<>();
models.add(model);
models.addAll(model.getSubmodels());
// to be more specific add markers for targets
for (TargetView row : drug.getTargetRows()) {
// check if target should be vizualized
if (row.getSelected()) {
// depending on the type vizualize:
if (row.getType().equals(TargetType.COMPLEX_PROTEIN.getCommonName())) {
// complex children
result.addAll(complexToOverlayList(row, models));
} else if (row.getType().equals(TargetType.SINGLE_PROTEIN.getCommonName())) {
// single protein
if (row.getProteins().size() > 0) {
result.addAll(geneToOverlayList(row.getProteins().get(0).getAnnotation(), row.getIcon(), models));
}
} else if (row.getType().equals(TargetType.PROTEIN_FAMILY.getCommonName())) {
// all proteins in the family
for (GeneRow geneRow : row.getProteins()) {
result.addAll(geneToOverlayList(geneRow.getAnnotation(), row.getIcon(), models));
}
} else {
logger.error("Unknown target type: " + row.getType());
}
} else { // if the whole target wasn't selected then maybe only some
// proteins within it were selected
for (GeneRow geneRow : row.getProteins()) {
if (geneRow.getSelected()) {
result.addAll(geneToOverlayList(geneRow.getAnnotation(), row.getIcon(), models));
}
}
}
}
return result;
}
}
......@@ -68,7 +68,6 @@
<bean id="FullReactionViewFactory" class="lcsb.mapviewer.services.search.data.FullReactionViewFactory"/>
<bean id="LightAliasViewFactory" class="lcsb.mapviewer.services.search.data.LightAliasViewFactory"/>
<bean id="DrugViewFactory" class="lcsb.mapviewer.services.search.db.drug.DrugViewFactory"/>
<bean id="MiRNAViewFactory" class="lcsb.mapviewer.services.search.db.mirna.MiRNAViewFactory"/>
<bean id="TargetViewFactory" class="lcsb.mapviewer.services.search.db.TargetViewFactory"/>
......
......@@ -6,7 +6,6 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ DrugServiceTest.class, //
DrugViewFactoryTest.class, //
DrugViewTest.class,//
})
......
......@@ -19,24 +19,16 @@ import org.junit.Test;
import lcsb.mapviewer.annotation.data.Drug;
import lcsb.mapviewer.annotation.data.Target;
import lcsb.mapviewer.annotation.services.TaxonomyBackend;
import lcsb.mapviewer.converter.ComplexZipConverter;
import lcsb.mapviewer.converter.ComplexZipConverterParams;
import lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser;
import lcsb.mapviewer.converter.zip.ModelZipEntryFile;
import lcsb.mapviewer.converter.zip.ZipEntryFile;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.MiriamType;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.model.ModelFullIndexed;
import lcsb.mapviewer.model.map.model.SubmodelType;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.GenericProtein;
import lcsb.mapviewer.model.map.species.Protein;
import lcsb.mapviewer.services.ServiceTestFunctions;
import lcsb.mapviewer.services.search.db.DbSearchCriteria;
import lcsb.mapviewer.services.search.db.GeneRow;
import lcsb.mapviewer.services.search.db.TargetView;
public class DrugServiceTest extends ServiceTestFunctions {
Logger logger = Logger.getLogger(DrugServiceTest.class);
......
package lcsb.mapviewer.services.search.db.drug;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import lcsb.mapviewer.annotation.data.Drug;
import lcsb.mapviewer.annotation.data.Target;
import lcsb.mapviewer.annotation.data.TargetType;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.services.ServiceTestFunctions;
import lcsb.mapviewer.services.overlay.IconManager;
import lcsb.mapviewer.services.search.data.ElementIdentifier;
import lcsb.mapviewer.services.search.db.TargetDetails;
import lcsb.mapviewer.services.search.db.TargetView;
import lcsb.mapviewer.services.search.db.TargetViewFactory;
import lcsb.mapviewer.services.view.AnnotationView;
import lcsb.mapviewer.services.view.AnnotationViewFactory;
public class DrugViewFactoryTest extends ServiceTestFunctions {
@Autowired
TargetViewFactory targetViewFactory;
Logger logger = Logger.getLogger(DrugViewFactoryTest.class);
@Autowired
AnnotationViewFactory avFactory;
@Autowired
DrugViewFactory dvf;
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testGeneToOverlayList() throws Exception {
try {
Model model = getModelForFile("testFiles/drug_target.xml", true);
model.setTileSize(256);
List<Model> models = new ArrayList<>();
models.add(model);
AnnotationView annotation = avFactory.create(new MiriamData());
annotation.setName("M");
List<ElementIdentifier> overlays = dvf.geneToOverlayList(annotation, IconManager.getInstance().getEmpyIcon(), models);
assertEquals(1, overlays.size());
annotation.setName("GRIN2B");
overlays = dvf.geneToOverlayList(annotation, IconManager.getInstance().getEmpyIcon(), models);
assertEquals(3, overlays.size());
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testComplexToOverlayList() throws Exception {
try {
Drug drug = chemblParser.findDrug("AMANTADINE");
Target exampleTarget = null;
assertTrue(drug.getTargets().size() > 0);
for (Target target : drug.getTargets()) {
if (target.getName().equals("Glutamate [NMDA] receptor")) {
target.setType(TargetType.COMPLEX_PROTEIN);
exampleTarget = target;
}
logger.debug(target);
}
assertNotNull(exampleTarget);
Model model = getModelForFile("testFiles/drug_target.xml", true);
model.setTileSize(256);
List<Model> models = new ArrayList<>();
models.add(model);
TargetView view = targetViewFactory.create(exampleTarget);
view.setIcon(IconManager.getInstance().getEmpyIcon());
List<ElementIdentifier> overlays = dvf.complexToOverlayList(view, models);
assertEquals(2, overlays.size());
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testgetElementInformationForResult() throws Exception {
try {
Drug drug = chemblParser.findDrug("AMANTADINE");
Model model = getModelForFile("testFiles/drug_target.xml", false);
Element alias = model.getElementByElementId("sa9");
alias.setName("M");
alias.setId(12);
model.removeElement(alias);
model.addElement(alias);
ElementIdentifier ei = new ElementIdentifier(alias, null);
model.setTileSize(256);
List<Model> models = new ArrayList<>();
models.add(model);
DrugView view = dvf.create(drug);
TargetDetails result = dvf.getElementInformationForResult(view.getTargetRows(), ei, model);
assertNotNull(result);
assertFalse(result.getDatabaseSource().equals(result.getReferences().get(0)));
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testCreateEmpty() throws Exception {
try {
Object object = dvf.create(null);
assertNotNull(object);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testCreateGson() throws Exception {
try {
DrugView object = dvf.create(null);
assertNotNull(dvf.createGson(object));
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}
......@@ -80,7 +80,6 @@ import lcsb.mapviewer.services.interfaces.IReferenceGenomeService;
import lcsb.mapviewer.services.interfaces.ISearchService;
import lcsb.mapviewer.services.interfaces.IUserService;
import lcsb.mapviewer.services.search.db.chemical.IChemicalService;
import lcsb.mapviewer.services.search.db.drug.DrugViewFactory;
import lcsb.mapviewer.services.search.db.drug.IDrugService;
import lcsb.mapviewer.services.view.AuthenticationToken;
import lcsb.mapviewer.services.view.ModelViewFactory;
......
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