Search in sources :

Example 66 with OntologyTerm

use of ubic.basecode.ontology.model.OntologyTerm in project Gemma by PavlidisLab.

the class TreeCharacteristicValueObject method ontology2TreeCharacteristicValueObjects.

public static TreeCharacteristicValueObject ontology2TreeCharacteristicValueObjects(OntologyTerm ontologyTerm, Map<String, TreeCharacteristicValueObject> phenotypeFoundInTree) {
    Collection<OntologyTerm> directChildTerms = ontologyTerm.getChildren(true);
    TreeSet<TreeCharacteristicValueObject> children = new TreeSet<>();
    for (OntologyTerm ot : directChildTerms) {
        if (phenotypeFoundInTree.containsKey(ot.getUri())) {
            TreeCharacteristicValueObject child = phenotypeFoundInTree.get(ot.getUri());
            children.add(child);
        // See bug 4102. Removing wreaks havoc and I cannot see why it would be necessary.
        // treesPhenotypes.remove( child );
        } else {
            TreeCharacteristicValueObject tree = ontology2TreeCharacteristicValueObjects(ot, phenotypeFoundInTree);
            phenotypeFoundInTree.put(tree.getValueUri(), tree);
            children.add(tree);
        }
    }
    return new TreeCharacteristicValueObject(-1L, ontologyTerm.getLabel(), ontologyTerm.getUri(), children);
}
Also used : OntologyTerm(ubic.basecode.ontology.model.OntologyTerm)

Example 67 with OntologyTerm

use of ubic.basecode.ontology.model.OntologyTerm in project Gemma by PavlidisLab.

the class GeneOverlapEndpoint method invokeInternal.

/**
 * Reads the given <code>requestElement</code>, and sends a the response back.
 *
 * @param requestElement the contents of the SOAP message as DOM elements
 * @param document       a DOM document to be used for constructing <code>Node</code>s
 * @return the response element
 */
@Override
protected Element invokeInternal(Element requestElement, Document document) {
    StopWatch watch = new StopWatch();
    watch.start();
    setLocalName(LOCAL_NAME);
    String queryInput = "";
    Collection<String> query = getSingleNodeValue(requestElement, "query_gene_id");
    for (String gene_id : query) {
        queryInput = gene_id;
    }
    Collection<String> geneResult = getArrayValues(requestElement, "gene_ids");
    Collection<Long> geneIdLongs = new ArrayList<Long>();
    for (String gene_id : geneResult) {
        geneIdLongs.add(Long.parseLong(gene_id));
    }
    log.info("XML input read: query gene id, " + queryInput + ", against " + geneResult.size() + " other genes");
    // start building the wrapper
    // build xml manually for mapped result rather than use buildWrapper inherited from AbstractGemmeEndpoint
    // start building the wrapper
    // build xml manually for mapped result rather than use buildWrapper inherited from AbstractGemmeEndpoint
    String elementName1 = "gene";
    String elementName2 = "overlap_GO_terms";
    Element responseWrapper = document.createElementNS(NAMESPACE_URI, LOCAL_NAME);
    Element responseElement = document.createElementNS(NAMESPACE_URI, LOCAL_NAME + RESPONSE);
    responseWrapper.appendChild(responseElement);
    // get query gene object from query gene id
    Long queryId = Long.parseLong(queryInput);
    Gene queryGene = geneService.load(queryId);
    if (queryGene == null) {
        String msg = "No gene with ids, " + queryId + " can be found.";
        return buildBadResponse(document, msg);
    }
    Map<Long, Collection<OntologyTerm>> gene2Ot = geneOntologyService.calculateGoTermOverlap(queryGene, geneIdLongs);
    Collection<Long> geneCol = gene2Ot.keySet();
    // for each gene
    for (Long geneId : geneCol) {
        // get the labels and store them
        Collection<String> goTerms = new HashSet<String>();
        for (OntologyTerm ot : gene2Ot.get(geneId)) {
            goTerms.add(GeneOntologyServiceImpl.asRegularGoId(ot));
        }
        String elementString1 = geneId.toString();
        String elementString2 = encode(goTerms.toArray());
        Element e1 = document.createElement(elementName1);
        e1.appendChild(document.createTextNode(elementString1));
        responseElement.appendChild(e1);
        Element e2 = document.createElement(elementName2);
        e2.appendChild(document.createTextNode(elementString2));
        responseElement.appendChild(e2);
    }
    watch.stop();
    Long time = watch.getTime();
    log.info("XML response for gene overlap results built in " + time + "ms.");
    return responseWrapper;
}
Also used : Gene(ubic.gemma.model.genome.Gene) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Collection(java.util.Collection) OntologyTerm(ubic.basecode.ontology.model.OntologyTerm) StopWatch(org.apache.commons.lang3.time.StopWatch) HashSet(java.util.HashSet)

Example 68 with OntologyTerm

use of ubic.basecode.ontology.model.OntologyTerm in project Gemma by PavlidisLab.

the class PhenotypeAssociationManagerServiceImpl method findParentRoot.

/**
 * Recursively build the full trees of the Ontology with the given branches
 *
 * @param finalTreesWithRoots  final trees with roots
 * @param phenotypeFoundInTree phenotypes found in tree
 * @param tc                   tree characteristic value object
 */
private void findParentRoot(TreeCharacteristicValueObject tc, TreeSet<TreeCharacteristicValueObject> finalTreesWithRoots, Map<String, TreeCharacteristicValueObject> phenotypeFoundInTree) {
    if (tc == null || tc.getValueUri() == null) {
        // the next line here.
        return;
    }
    OntologyTerm ontologyTerm = this.ontologyHelper.findOntologyTermByUri(tc.getValueUri());
    Collection<OntologyTerm> ontologyParents = ontologyTerm.getParents(true);
    if (!ontologyParents.isEmpty()) {
        for (OntologyTerm onTerm : ontologyParents) {
            // see if the node is already in the tree
            TreeCharacteristicValueObject alreadyOnTree = phenotypeFoundInTree.get(onTerm.getUri());
            if (alreadyOnTree != null) {
                alreadyOnTree.getChildren().add(tc);
            } else {
                TreeCharacteristicValueObject tree = new TreeCharacteristicValueObject(-1L, onTerm.getLabel(), onTerm.getUri());
                // add children to the parent
                tree.getChildren().add(tc);
                // put node in the hashmap for fast access
                phenotypeFoundInTree.put(tree.getValueUri(), tree);
                this.findParentRoot(tree, finalTreesWithRoots, phenotypeFoundInTree);
            }
        }
    } else {
        // found a root, no more parents
        if (tc.getValue() != null && !tc.getValue().equals("")) {
            finalTreesWithRoots.add(tc);
        }
    }
}
Also used : OntologyTerm(ubic.basecode.ontology.model.OntologyTerm)

Example 69 with OntologyTerm

use of ubic.basecode.ontology.model.OntologyTerm in project Gemma by PavlidisLab.

the class PhenotypeAssociationManagerServiceImpl method findAllPhenotypesByTree.

/**
 * Using all the phenotypes in the database, builds a tree structure using the Ontology
 *
 * @param isAdmin,                can see everything
 * @param noElectronicAnnotation, if true don't include evidence code IEA
 * @param evidenceFilter          evidence filter
 * @return Collection<TreeCharacteristicValueObject> list of all phenotypes in gemma represented as trees
 */
private Collection<TreeCharacteristicValueObject> findAllPhenotypesByTree(EvidenceFilter evidenceFilter, boolean isAdmin, boolean noElectronicAnnotation) {
    StopWatch sw = new StopWatch();
    sw.start();
    Taxon taxon = null;
    boolean showOnlyEditable = false;
    Collection<Long> externalDatabaseIds = null;
    if (evidenceFilter != null) {
        taxon = this.checkAndGetTaxon(evidenceFilter);
        showOnlyEditable = evidenceFilter.isShowOnlyEditable();
        externalDatabaseIds = evidenceFilter.getExternalDatabaseIds();
    }
    Map<String, Set<Integer>> publicPhenotypesGenesAssociations = new HashMap<>();
    Map<String, Set<Integer>> privatePhenotypesGenesAssociations = new HashMap<>();
    Set<String> allPhenotypesGenesAssociations = new HashSet<>();
    boolean isUserLoggedIn = SecurityUtil.isUserLoggedIn();
    String userName;
    Collection<String> groups;
    PhenotypeAssociationManagerServiceImpl.log.info(// note we can't cache this because varies per user.
    "Starting loading phenotype tree");
    if (isUserLoggedIn) {
        userName = this.userManager.getCurrentUsername();
        // groups the user belong to
        groups = this.userManager.findGroupsForUser(userName);
        // show only my annotation was chosen
        if (showOnlyEditable) {
            // show public owned by the user
            publicPhenotypesGenesAssociations = this.phenoAssocService.findPublicPhenotypesGenesAssociations(taxon, null, userName, groups, true, externalDatabaseIds, noElectronicAnnotation);
            // show all private owned by the user or shared by a group
            privatePhenotypesGenesAssociations = this.phenoAssocService.findPrivatePhenotypesGenesAssociations(taxon, null, userName, groups, true, externalDatabaseIds, noElectronicAnnotation);
        } else {
            // logged in, but not filtered. all public evidences
            publicPhenotypesGenesAssociations = this.phenoAssocService.findPublicPhenotypesGenesAssociations(taxon, null, null, groups, false, externalDatabaseIds, noElectronicAnnotation);
            if (isAdmin) {
                // show all private since admin
                privatePhenotypesGenesAssociations = this.phenoAssocService.findPrivatePhenotypesGenesAssociations(taxon, null, null, null, false, externalDatabaseIds, noElectronicAnnotation);
            } else {
                // show all private owned by the user or shared by a group
                privatePhenotypesGenesAssociations = this.phenoAssocService.findPrivatePhenotypesGenesAssociations(taxon, null, userName, groups, false, externalDatabaseIds, noElectronicAnnotation);
            }
        }
    } else if (!showOnlyEditable) {
        // anonymous user
        publicPhenotypesGenesAssociations = this.phenoAssocService.findPublicPhenotypesGenesAssociations(taxon, null, null, null, false, externalDatabaseIds, noElectronicAnnotation);
    }
    allPhenotypesGenesAssociations.addAll(privatePhenotypesGenesAssociations.keySet());
    allPhenotypesGenesAssociations.addAll(publicPhenotypesGenesAssociations.keySet());
    // map to help the placement of elements in the tree, used to find quickly the position to add subtrees
    Map<String, TreeCharacteristicValueObject> phenotypeFoundInTree = new HashMap<>();
    // represents each phenotype and children found in the Ontology, TreeSet used to order trees [why do we need a
    // TreeSet? Can't we just sort at the end?]
    TreeSet<TreeCharacteristicValueObject> treesPhenotypes = new TreeSet<>();
    // creates the tree structure
    for (String valueUri : allPhenotypesGenesAssociations) {
        // don't create the tree if it is already present in an other [another what?]
        if (phenotypeFoundInTree.containsKey(valueUri)) {
            // flag the node as phenotype found in database [when does this happen? It seems useless, since we don't
            // use this]
            phenotypeFoundInTree.get(valueUri).setDbPhenotype(true);
        } else {
            try {
                // find the ontology term using the valueURI
                OntologyTerm ontologyTerm = this.ontologyHelper.findOntologyTermByUri(valueUri);
                // we don't show obsolete terms
                if (ontologyTerm.isTermObsolete()) {
                    PhenotypeAssociationManagerServiceImpl.log.warn("A valueUri found in the database is obsolete: " + valueUri);
                } else {
                    // transform an OntologyTerm and his children to a TreeCharacteristicValueObject
                    TreeCharacteristicValueObject treeCharacteristicValueObject = TreeCharacteristicValueObject.ontology2TreeCharacteristicValueObjects(ontologyTerm, phenotypeFoundInTree);
                    // set flag that this node represents a phenotype used in the database
                    treeCharacteristicValueObject.setDbPhenotype(true);
                    // add tree to the phenotypes found in ontology
                    phenotypeFoundInTree.put(ontologyTerm.getUri(), treeCharacteristicValueObject);
                    if (!treesPhenotypes.add(treeCharacteristicValueObject)) {
                        throw new IllegalStateException("Add failed for " + ontologyTerm);
                    }
                    if (PhenotypeAssociationManagerServiceImpl.log.isDebugEnabled())
                        PhenotypeAssociationManagerServiceImpl.log.debug("Added: " + ontologyTerm);
                }
            } catch (EntityNotFoundException entityNotFoundException) {
                if (this.ontologyHelper.areOntologiesAllLoaded()) {
                    if (PhenotypeAssociationManagerServiceImpl.log.isDebugEnabled())
                        PhenotypeAssociationManagerServiceImpl.log.debug(// this ends up being pretty verbose.
                        "A valueUri in the database was not found in the ontology; DB out of date?; valueUri: " + valueUri);
                } else {
                    throw new RuntimeException(PhenotypeAssociationManagerServiceImpl.ERROR_MSG_ONTOLOGIES_NOT_LOADED + " ( " + entityNotFoundException.getMessage() + " )");
                }
            }
        }
    }
    TreeSet<TreeCharacteristicValueObject> finalTreesWithRoots = new TreeSet<>();
    for (TreeCharacteristicValueObject tc : treesPhenotypes) {
        this.findParentRoot(tc, finalTreesWithRoots, phenotypeFoundInTree);
    }
    treesPhenotypes = finalTreesWithRoots;
    // set the public count
    for (TreeCharacteristicValueObject tc : treesPhenotypes) {
        tc.countPublicGeneForEachNode(publicPhenotypesGenesAssociations);
        tc.countPrivateGeneForEachNode(privatePhenotypesGenesAssociations);
        tc.removeUnusedPhenotypes();
    }
    PhenotypeAssociationManagerServiceImpl.log.info("Done total time=" + sw.getTime() + "ms");
    return treesPhenotypes;
}
Also used : Taxon(ubic.gemma.model.genome.Taxon) OntologyTerm(ubic.basecode.ontology.model.OntologyTerm) EntityNotFoundException(ubic.gemma.core.association.phenotype.PhenotypeExceptions.EntityNotFoundException) StopWatch(org.apache.commons.lang3.time.StopWatch)

Example 70 with OntologyTerm

use of ubic.basecode.ontology.model.OntologyTerm in project Gemma by PavlidisLab.

the class PhenotypeAssoOntologyHelperImpl method valueUri2Characteristic.

@Override
public Characteristic valueUri2Characteristic(String valueUri) {
    try {
        OntologyTerm o = findOntologyTermByUri(valueUri);
        if (o == null)
            return null;
        VocabCharacteristic myPhenotype = VocabCharacteristic.Factory.newInstance();
        myPhenotype.setValueUri(o.getUri());
        myPhenotype.setValue(o.getLabel());
        myPhenotype.setCategory(PhenotypeAssociationConstants.PHENOTYPE);
        myPhenotype.setCategoryUri(PhenotypeAssociationConstants.PHENOTYPE_CATEGORY_URI);
        return myPhenotype;
    } catch (EntityNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : VocabCharacteristic(ubic.gemma.model.common.description.VocabCharacteristic) OntologyTerm(ubic.basecode.ontology.model.OntologyTerm) EntityNotFoundException(ubic.gemma.core.association.phenotype.PhenotypeExceptions.EntityNotFoundException)

Aggregations

OntologyTerm (ubic.basecode.ontology.model.OntologyTerm)73 Test (org.junit.Test)13 VocabCharacteristic (ubic.gemma.model.common.description.VocabCharacteristic)13 Gene (ubic.gemma.model.genome.Gene)11 OntologyResource (ubic.basecode.ontology.model.OntologyResource)8 HashSet (java.util.HashSet)6 StopWatch (org.apache.commons.lang3.time.StopWatch)6 CharacteristicValueObject (ubic.gemma.model.genome.gene.phenotype.valueObject.CharacteristicValueObject)6 BufferedReader (java.io.BufferedReader)3 ConcurrentHashSet (org.compass.core.util.concurrent.ConcurrentHashSet)3 Element (org.w3c.dom.Element)3 OntologyIndividual (ubic.basecode.ontology.model.OntologyIndividual)3 EntityNotFoundException (ubic.gemma.core.association.phenotype.PhenotypeExceptions.EntityNotFoundException)3 Characteristic (ubic.gemma.model.common.description.Characteristic)3 GeneEvidenceValueObject (ubic.gemma.model.genome.gene.phenotype.valueObject.GeneEvidenceValueObject)3 Resource (com.hp.hpl.jena.rdf.model.Resource)2 FileReader (java.io.FileReader)2 SocketException (java.net.SocketException)2 Collection (java.util.Collection)2 BaseSpringContextTest (ubic.gemma.core.testing.BaseSpringContextTest)2