Commit 4c0e2b05 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

TargetViewFactory removed

parent dbc3e6c8
......@@ -8,7 +8,6 @@ import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import lcsb.mapviewer.annotation.data.Target;
import lcsb.mapviewer.annotation.data.TargetType;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.model.map.BioEntity;
import lcsb.mapviewer.model.map.MiriamData;
......@@ -19,8 +18,6 @@ import lcsb.mapviewer.model.map.species.Gene;
import lcsb.mapviewer.model.map.species.Protein;
import lcsb.mapviewer.model.map.species.Rna;
import lcsb.mapviewer.model.map.species.Species;
import lcsb.mapviewer.services.search.db.GeneRow;
import lcsb.mapviewer.services.search.db.TargetView;
import lcsb.mapviewer.services.view.AnnotationViewFactory;
public class ElementMatcher {
......@@ -37,92 +34,6 @@ public class ElementMatcher {
@Autowired
private AnnotationViewFactory annotationViewFactory;
/**
* Check if the target should be applied to the element.
*
* @param target
* drug target to check
* @param element
* {@link Element} to check
* @return <code>true</code> if drug target can target element,
* <code>false</code> otherwise
*/
public boolean elementMatch(TargetView target, BioEntity element) {
if (target.getType().equals(TargetType.COMPLEX_PROTEIN.getCommonName())) {
MiriamData targetAnnotation = annotationViewFactory.viewToObject(target.getAnnotation());
if (MiriamType.CHEMBL_TARGET.equals(targetAnnotation.getDataType())) {
if (element.getMiriamData().contains(targetAnnotation)) {
return true;
}
}
Collection<String> ids = new HashSet<String>();
for (GeneRow row : target.getProteins()) {
ids.add(row.getAnnotation().getName());
}
if (element instanceof Complex) {
Complex complex = (Complex) element;
Set<Species> speciesSet = complex.getAllSimpleChildren();
if (speciesSet.size() != ids.size()) {
return false;
}
for (Element id : speciesSet) {
if (!ids.contains(id.getName())) {
return false;
}
}
return true;
}
return false;
} else if (target.getType().equals(TargetType.SINGLE_PROTEIN.getCommonName())) {
MiriamData targetAnnotation = null;
if (target.getAnnotation() != null) {
targetAnnotation = annotationViewFactory.viewToObject(target.getAnnotation());
} else if (target.getProteins().size() > 0) {
targetAnnotation = annotationViewFactory.viewToObject(target.getProteins().get(0).getAnnotation());
} else {
logger.warn("Invalid target found: " + target);
}
if (targetAnnotation != null && element.getMiriamData().contains(targetAnnotation)) {
return true;
}
if (element instanceof Protein || element instanceof Rna || element instanceof Gene) {
if (target.getProteins().size() > 0) {
String hgncId = target.getProteins().get(0).getAnnotation().getName();
if (element.getName().equalsIgnoreCase(hgncId)) {
return true;
}
}
}
return false;
} else if (target.getType().equals(TargetType.PROTEIN_FAMILY.getCommonName())) {
if (element instanceof Protein || element instanceof Rna || element instanceof Gene) {
String hgncId = target.getProteins().get(0).getAnnotation().getName();
if (element.getName().equalsIgnoreCase(hgncId)) {
return true;
} else if (element.getMiriamData().contains(new MiriamData(MiriamType.HGNC_SYMBOL, hgncId))) {
return true;
}
for (GeneRow protein : target.getProteins()) {
hgncId = protein.getAnnotation().getName();
if (element.getName().equalsIgnoreCase(hgncId)) {
return true;
} else if (element.getMiriamData().contains(new MiriamData(MiriamType.HGNC_SYMBOL, hgncId))) {
return true;
}
}
}
return false;
} else if (target.getType().equals(TargetType.OTHER.getCommonName())) {
// in other case just compare names
String name = target.getProteins().get(0).getAnnotation().getName();
return element.getName().equalsIgnoreCase(name);
} else {
throw new InvalidArgumentException("Unknown drug target type: " + target.getType());
}
}
public boolean elementMatch(Target target, BioEntity element) {
MiriamData targetAnnotation = target.getSource();
switch (target.getType()) {
......
......@@ -58,57 +58,7 @@ public abstract class SearchResultFactory<T, S extends ISearchResultView> extend
@Autowired
private ElementMatcher elementMatcher;
/**
* Returns detailed information about element respective to the set of targets
* (which should be connected to single drug/mirna/chemical).
*
* @param element
* element for which we are looking for detailed information
* @param model
* {@link Model} on which element is located
* @param targets
* list of targets from which detailed information should be
* extracted
* @return detailed information for element in the specific drug,
* <code>null</code> if such information doesnt exist
*/
public TargetDetails getElementInformationForResult(Collection<TargetView> targets, ElementIdentifier element, Model model) {
if (ElementIdentifierType.REACTION.getJsName().equalsIgnoreCase(element.getType())) {
logger.warn("Detail information for reactions not available");
return null;
} else if (ElementIdentifierType.POINT.getJsName().equalsIgnoreCase(element.getType())) {
logger.warn("Detail information for points not available");
return null;
} else if (ElementIdentifierType.ALIAS.getJsName().equalsIgnoreCase(element.getType())) {
Model m = model.getSubmodelById(element.getModelId());
Element alias = m.getElementByDbId(Integer.valueOf(element.getObjectId()));
if (alias == null) {
logger.warn("Alias with object id = " + element.getObjectId() + " is unknown.");
return null;
}
for (TargetView row : targets) {
if (elementMatch(row, alias)) {
TargetDetails result = new TargetDetails();
result.setDatabaseSource(row.getAnnotation());
for (Article article : row.getReferences()) {
result.getReferences().add(annotationViewFactory.create(article));
}
for (GeneRow protein : row.getProteins()) {
result.getReferences().add(protein.getAnnotation());
}
return result;
}
}
} else {
throw new InvalidArgumentException("Unknown element type: " + element.getType());
}
return null;
}
protected boolean elementMatch(TargetView target, Element element) {
return elementMatcher.elementMatch(target, element);
}
protected boolean elementMatch(Target target, Element element) {
return elementMatcher.elementMatch(target, element);
}
......
package lcsb.mapviewer.services.search.db;
import java.util.Comparator;
import lcsb.mapviewer.common.comparator.StringComparator;
/**
* Comparator class for {@link TargetView} objects. It put first elements that
* are {@link TargetView#getSelectable() selectable} elements.
*
* @author Piotr Gawron
*
*/
public class DrugTargetViewVisibilityComparator implements Comparator<TargetView> {
/**
* Comparator class for Strings that handles nulls.
*/
private final StringComparator stringComparator = new StringComparator();
@Override
public int compare(TargetView arg0, TargetView arg1) {
if (arg0.getSelectable() && !arg1.getSelectable()) {
return -1;
}
if (!arg0.getSelectable() && arg1.getSelectable()) {
return 1;
}
return stringComparator.compare(arg0.getName(), arg1.getName());
}
}
package lcsb.mapviewer.services.search.db;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import com.google.gson.Gson;
import lcsb.mapviewer.annotation.data.Article;
import lcsb.mapviewer.annotation.data.ArticleCitationComparator;
import lcsb.mapviewer.annotation.data.Target;
import lcsb.mapviewer.annotation.data.TargetType;
import lcsb.mapviewer.annotation.services.PubmedParser;
import lcsb.mapviewer.annotation.services.PubmedSearchException;
import lcsb.mapviewer.common.exception.NotImplementedException;
import lcsb.mapviewer.model.map.BioEntity;
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.ModelSubmodelConnection;
import lcsb.mapviewer.services.view.AbstractViewFactory;
import lcsb.mapviewer.services.view.AnnotationViewFactory;
/**
* Factory class for {@link TargetView} class.
*
* @author Piotr Gawron
*
*/
public class TargetViewFactory extends AbstractViewFactory<Target, TargetView> {
/**
* Default class logger.
*/
private static Logger logger = Logger.getLogger(TargetViewFactory.class);
/**
* Factory object for {@link lcsb.mapviewer.services.view.AnnotationView}
* elements.
*/
@Autowired
private AnnotationViewFactory annotationViewFactory;
/**
* Service accessing
* <a href="http://europepmc.org/RestfulWebService">pubmed</a>.
*/
@Autowired
private PubmedParser pubmedParser;
@Override
public TargetView create(Target target) {
return create(target, null);
}
/**
* Creates {@link TargetView} element for given {@link Target} and places
* links on the parameter {@link Model}.
*
* @param target
* object for which {@link TargetView} is created
* @param model
* {@link Model} where links will be placed
* @return {@link TargetView} element for given {@link Target}
*/
public TargetView create(Target target, Model model) {
TargetView result = new TargetView();
if (target == null) {
return result;
}
result.setName(target.getName());
if (target.getType() == null) {
logger.warn("Unknown type of target: " + target.getName() + ", target name: " + target.getName());
} else {
result.setType(target.getType().getCommonName());
}
if (target.getType() == TargetType.SINGLE_PROTEIN || target.getType() == TargetType.COMPLEX_PROTEIN || target.getType() == TargetType.PROTEIN_FAMILY) {
result.setVisible(true);
result.setSelectable(false);
}
for (MiriamData md : target.getGenes()) {
GeneRow geneRow = new GeneRow();
geneRow.setAnnotation(annotationViewFactory.create(md));
if (model != null) {
Set<BioEntity> list = new HashSet<>();
list.addAll(model.getElementsByName(md.getResource()));
list.addAll(model.getElementsByAnnotation(md));
int countInTopModel = list.size();
for (ModelSubmodelConnection submodel : model.getSubmodelConnections()) {
list.addAll(submodel.getSubmodel().getModel().getElementsByName(md.getResource()));
list.addAll(submodel.getSubmodel().getModel().getElementsByAnnotation(md));
}
int counter = list.size();
geneRow.setSelectable(counter > 0);
if (counter > 0) {
geneRow.setSelectable(true);
if (target.getType() == TargetType.SINGLE_PROTEIN || target.getType() == TargetType.PROTEIN_FAMILY) {
result.setSelectable(true);
}
if (countInTopModel < counter) {
result.setAvailableInSubmodel(true);
}
}
}
result.addProtein(geneRow);
}
// this should be fixed
// if (target.getType() == TargetType.COMPLEX_PROTEIN) {
//
// if (complexToOverlayList(target, null).size() > 0) {
// row.setSelectable(true);
// }
// }
if (target.getSource() != null) {
result.setAnnotation(annotationViewFactory.create(target.getSource()));
}
for (MiriamData md : target.getReferences()) {
if (md.getDataType().equals(MiriamType.PUBMED)) {
try {
Integer id = Integer.parseInt(md.getResource());
Article reference = pubmedParser.getPubmedArticleById(id);
if (reference != null) {
result.addReference(reference);
}
} catch (NumberFormatException e) {
logger.warn("Invalid pubmed reference: " + md);
} catch (PubmedSearchException e) {
logger.warn("Problem with accessing info about pubmed: " + md, e);
}
} else {
logger.warn("Unknown reference type: " + md);
}
}
Collections.sort(result.getReferences(), new ArticleCitationComparator());
return result;
}
@Override
public String createGson(TargetView object) {
return new Gson().toJson(object);
}
@Override
public Target viewToObject(TargetView view) {
throw new NotImplementedException();
}
}
......@@ -68,8 +68,6 @@
<bean id="FullReactionViewFactory" class="lcsb.mapviewer.services.search.data.FullReactionViewFactory"/>
<bean id="LightAliasViewFactory" class="lcsb.mapviewer.services.search.data.LightAliasViewFactory"/>
<bean id="TargetViewFactory" class="lcsb.mapviewer.services.search.db.TargetViewFactory"/>
<bean id="ElementMatcher" class="lcsb.mapviewer.services.search.ElementMatcher"/>
......
......@@ -12,7 +12,6 @@ import lcsb.mapviewer.services.search.db.mirna.MiRNAServiceTest;
@SuiteClasses({ AllSearchDrugTests.class, //
ChemicalServiceTest.class, //
MiRNAServiceTest.class, //
TargetViewFactoryTest.class, //
TargetViewTest.class,//
})
public class AllSearchDbTests {
......
package lcsb.mapviewer.services.search.db;
import static org.junit.Assert.*;
import lcsb.mapviewer.services.ServiceTestFunctions;
import lcsb.mapviewer.services.search.db.TargetView;
import lcsb.mapviewer.services.search.db.TargetViewFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
public class TargetViewFactoryTest extends ServiceTestFunctions{
@Autowired
TargetViewFactory targetViewFactory;
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testCreateEmpty() throws Exception {
try {
Object object = targetViewFactory.create(null);
assertNotNull(object);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testCreateGson() throws Exception {
try {
TargetView object = targetViewFactory.create(null);
assertNotNull(targetViewFactory.createGson(object));
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}
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