Search in sources :

Example 1 with UnknownIdentifierException

use of org.geneontology.minerva.MolecularModelManager.UnknownIdentifierException in project minerva by geneontology.

the class M3ExpressionParser method parse.

OWLClassExpression parse(MinervaOWLGraphWrapper g, JsonOwlObject expression, ExternalLookupService externalLookupService) throws MissingParameterException, UnknownIdentifierException, OWLException {
    if (expression == null) {
        throw new MissingParameterException("Missing expression: null is not a valid expression.");
    }
    if (expression.type == null) {
        throw new MissingParameterException("An expression type is required.");
    }
    if (JsonOwlObjectType.Class == expression.type) {
        if (expression.id == null) {
            throw new MissingParameterException("Missing literal for expression of type 'class'");
        }
        if (StringUtils.containsWhitespace(expression.id)) {
            throw new UnknownIdentifierException("Identifiers may not contain whitespaces: '" + expression.id + "'");
        }
        IRI clsIRI = curieHandler.getIRI(expression.id);
        OWLClass cls;
        if (checkLiteralIds) {
            cls = g.getOWLClass(clsIRI);
            if (cls == null && externalLookupService != null) {
                List<LookupEntry> lookup = externalLookupService.lookup(clsIRI);
                if (lookup == null || lookup.isEmpty()) {
                    throw new UnknownIdentifierException("Could not validate the id: " + expression.id);
                }
                cls = createClass(clsIRI, g);
            }
            if (cls == null) {
                throw new UnknownIdentifierException("Could not retrieve a class for id: " + expression.id);
            }
        } else {
            cls = createClass(clsIRI, g);
        }
        return cls;
    } else if (JsonOwlObjectType.SomeValueFrom == expression.type) {
        if (expression.property == null) {
            throw new MissingParameterException("Missing property for expression of type 'svf'");
        }
        if (expression.property.id == null) {
            throw new MissingParameterException("Missing property id for expression of type 'svf'");
        }
        if (expression.property.type != JsonOwlObjectType.ObjectProperty) {
            throw new MissingParameterException("Unexpected type for property in 'svf': " + expression.property.type);
        }
        IRI propIRI = curieHandler.getIRI(expression.property.id);
        OWLObjectProperty p = g.getOWLObjectProperty(propIRI);
        if (p == null) {
            throw new UnknownIdentifierException("Could not find a property for: " + expression.property);
        }
        if (expression.filler != null) {
            OWLClassExpression ce = parse(g, expression.filler, externalLookupService);
            return g.getDataFactory().getOWLObjectSomeValuesFrom(p, ce);
        } else {
            throw new MissingParameterException("Missing literal or expression for expression of type 'svf'.");
        }
    } else if (JsonOwlObjectType.IntersectionOf == expression.type) {
        return parse(g, expression.expressions, externalLookupService, JsonOwlObjectType.IntersectionOf);
    } else if (JsonOwlObjectType.UnionOf == expression.type) {
        return parse(g, expression.expressions, externalLookupService, JsonOwlObjectType.UnionOf);
    } else if (JsonOwlObjectType.ComplementOf == expression.type) {
        if (expression.filler == null) {
            throw new MissingParameterException("Missing filler for expression of type 'complement'");
        }
        OWLClassExpression filler = parse(g, expression.filler, externalLookupService);
        return g.getDataFactory().getOWLObjectComplementOf(filler);
    } else {
        throw new UnknownIdentifierException("Unknown expression type: " + expression.type);
    }
}
Also used : UnknownIdentifierException(org.geneontology.minerva.MolecularModelManager.UnknownIdentifierException) LookupEntry(org.geneontology.minerva.lookup.ExternalLookupService.LookupEntry) MissingParameterException(org.geneontology.minerva.server.handler.OperationsTools.MissingParameterException)

Example 2 with UnknownIdentifierException

use of org.geneontology.minerva.MolecularModelManager.UnknownIdentifierException in project minerva by geneontology.

the class DefaultCurieHandlerTest method testConversions.

@Test
public void testConversions() throws UnknownIdentifierException {
    CurieHandler handler = DefaultCurieHandler.getDefaultHandler();
    final OWLDataFactory f = OWLManager.createOWLOntologyManager().getOWLDataFactory();
    IRI longBFO = IRI.create("http://purl.obolibrary.org/obo/BFO_0000050");
    assertEquals(longBFO, handler.getIRI("BFO:0000050"));
    assertEquals("BFO:0000050", handler.getCuri(f.getOWLAnnotationProperty(longBFO)));
    IRI longEco = IRI.create("http://purl.obolibrary.org/obo/ECO_0000217");
    assertEquals(longEco, handler.getIRI("ECO:0000217"));
    assertEquals("ECO:0000217", handler.getCuri(f.getOWLClass(longEco)));
    IRI longPmid = IRI.create("http://www.ncbi.nlm.nih.gov/pubmed/0000");
    assertEquals(longPmid, handler.getIRI("PMID:0000"));
    assertEquals("PMID:0000", handler.getCuri(f.getOWLClass(longPmid)));
    // test failure for non existing prefix
    try {
        handler.getIRI("BLABLA:000001");
        fail("Expected an UnknownIdentifierException to be thrown");
    } catch (UnknownIdentifierException e) {
    }
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) UnknownIdentifierException(org.geneontology.minerva.MolecularModelManager.UnknownIdentifierException) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) Test(org.junit.Test)

Example 3 with UnknownIdentifierException

use of org.geneontology.minerva.MolecularModelManager.UnknownIdentifierException in project minerva by geneontology.

the class M3ExpressionParser method parse.

private OWLClassExpression parse(MinervaOWLGraphWrapper g, JsonOwlObject[] expressions, ExternalLookupService externalLookupService, JsonOwlObjectType type) throws MissingParameterException, UnknownIdentifierException, OWLException {
    if (expressions.length == 0) {
        throw new MissingParameterException("Missing expressions: empty expression list is not allowed.");
    }
    if (expressions.length == 1) {
        return parse(g, expressions[0], externalLookupService);
    }
    Set<OWLClassExpression> clsExpressions = new HashSet<OWLClassExpression>();
    for (JsonOwlObject m3Expression : expressions) {
        OWLClassExpression ce = parse(g, m3Expression, externalLookupService);
        clsExpressions.add(ce);
    }
    if (type == JsonOwlObjectType.UnionOf) {
        return g.getDataFactory().getOWLObjectUnionOf(clsExpressions);
    } else if (type == JsonOwlObjectType.IntersectionOf) {
        return g.getDataFactory().getOWLObjectIntersectionOf(clsExpressions);
    } else {
        throw new UnknownIdentifierException("Unsupported expression type: " + type);
    }
}
Also used : UnknownIdentifierException(org.geneontology.minerva.MolecularModelManager.UnknownIdentifierException) MissingParameterException(org.geneontology.minerva.server.handler.OperationsTools.MissingParameterException) JsonOwlObject(org.geneontology.minerva.json.JsonOwlObject) HashSet(java.util.HashSet)

Example 4 with UnknownIdentifierException

use of org.geneontology.minerva.MolecularModelManager.UnknownIdentifierException in project minerva by geneontology.

the class ModelSearchHandler method search.

// examples
// http://127.0.0.1:6800/search/?
// ?gp=http://identifiers.org/uniprot/P15822-3
// ?term=http://purl.obolibrary.org/obo/GO_0003677
// 
// 
// ?gp=http://identifiers.org/mgi/MGI:1328355
// &gp=http://identifiers.org/mgi/MGI:87986
// &term=http://purl.obolibrary.org/obo/GO_0030968
// &title=mouse
// &pmid=PMID:19911006
// &state=development&state=review {development, production, closed, review, delete} or operator
// &count
// 127.0.0.1:6800/search/?contributor=http://orcid.org/0000-0002-1706-4196
public ModelSearchResult search(Set<String> taxa, Set<String> gene_product_ids, Set<String> terms, String expand, Set<String> pmids, String title_search, Set<String> state_search, Set<String> contributor_search, Set<String> group_search, String exactdate, String date_search, String datend, int offset, int limit, String count, String debug, Set<String> id) {
    ModelSearchResult r = new ModelSearchResult();
    Set<String> go_type_ids = new HashSet<String>();
    Set<String> gene_type_ids = new HashSet<String>();
    if (gene_product_ids != null) {
        gene_type_ids.addAll(gene_product_ids);
    }
    if (terms != null) {
        go_type_ids.addAll(terms);
    }
    CurieHandler curie_handler = m3.getCuriHandler();
    Set<String> go_type_uris = new HashSet<String>();
    Set<String> gene_type_uris = new HashSet<String>();
    for (String curi : go_type_ids) {
        if (curi.startsWith("http")) {
            go_type_uris.add(curi);
        } else {
            try {
                IRI iri = curie_handler.getIRI(curi);
                if (iri != null) {
                    go_type_uris.add(iri.toString());
                }
            } catch (UnknownIdentifierException e) {
                r.error += e.getMessage() + " \n ";
                e.printStackTrace();
                return r;
            }
        }
    }
    for (String curi : gene_type_ids) {
        if (curi.startsWith("http")) {
            gene_type_uris.add(curi);
        } else {
            try {
                IRI iri = curie_handler.getIRI(curi);
                if (iri != null) {
                    gene_type_uris.add(iri.toString());
                }
            } catch (UnknownIdentifierException e) {
                r.error += e.getMessage() + " \n ";
                e.printStackTrace();
                return r;
            }
        }
    }
    Map<String, ModelMeta> id_model = new LinkedHashMap<String, ModelMeta>();
    String sparql = "";
    try {
        sparql = IOUtils.toString(ModelSearchHandler.class.getResourceAsStream("/ModelSearchQueryTemplate.rq"), StandardCharsets.UTF_8);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Map<String, String> ind_return = new HashMap<String, String>();
    // <ind_return_list>
    String ind_return_list = "";
    // <types>
    String types = "";
    int n = 0;
    for (String type_uri : gene_type_uris) {
        n++;
        ind_return.put("?ind" + n, type_uri);
        ind_return_list = ind_return_list + " (GROUP_CONCAT(?ind" + n + " ; separator=\" \") AS ?inds" + n + ")";
        types = types + "?ind" + n + " rdf:type <" + type_uri + "> . \n";
    }
    if (expand != null) {
        for (String go_type_uri : go_type_uris) {
            n++;
            ind_return.put("?ind" + n, go_type_uri);
            ind_return_list = ind_return_list + " (GROUP_CONCAT(?ind" + n + " ; separator=\" \") AS ?inds" + n + ")";
            String expansion = "VALUES ?term" + n + " { ";
            try {
                Set<String> subclasses = go_lego.getAllSubClasses(go_type_uri);
                for (String sub : subclasses) {
                    expansion += "<" + sub + "> \n";
                }
                expansion += "} . \n";
                types = types + " " + expansion + " ?ind" + n + " rdf:type ?term" + n + " . \n";
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    } else {
        for (String go_type_uri : go_type_uris) {
            n++;
            ind_return.put("?ind" + n, go_type_uri);
            ind_return_list = ind_return_list + " (GROUP_CONCAT(?ind" + n + " ; separator=\" \") AS ?inds" + n + ")";
            types = types + "?ind" + n + " rdf:type <" + go_type_uri + "> . \n";
        }
    }
    String id_constraint = "";
    if (id != null && id.size() > 0) {
        String id_list = "";
        for (String mid : id) {
            if (!mid.contains("http")) {
                String[] curie = mid.split(":");
                if (curie != null && curie.length == 2) {
                    mid = "http://model.geneontology.org/" + curie[1];
                }
            // TODO figure this out and add it to standard curie collection
            // try {
            // IRI iri = curie_handler.getIRI(id);
            // id = iri.toString();
            // } catch (UnknownIdentifierException e) {
            // // TODO Auto-generated catch block
            // e.printStackTrace();
            // }
            }
            id_list += "<" + mid + "> ";
        }
        id_constraint = " values ?id { " + id_list + " } ";
    }
    // <pmid_constraints>
    String pmid_constraints = "";
    if (pmids != null) {
        for (String pmid : pmids) {
            n++;
            ind_return.put("?ind" + n, pmid);
            ind_return_list = ind_return_list + " (GROUP_CONCAT(?ind" + n + " ; separator=\" \") AS ?inds" + n + ")";
            pmid_constraints = pmid_constraints + "?ind" + n + " <http://purl.org/dc/elements/1.1/source> ?pmid FILTER (?pmid=\"" + pmid + "\"^^xsd:string) .\n";
        }
    }
    String taxa_constraint = "";
    if (taxa != null && !taxa.isEmpty()) {
        for (String taxon : taxa) {
            if (taxon.startsWith("NCBITaxon:")) {
                taxon = taxon.replace(":", "_");
                taxon = "http://purl.obolibrary.org/obo/" + taxon;
            } else if (!taxon.startsWith("http://purl.obolibrary.org/obo/NCBITaxon_")) {
                taxon = "http://purl.obolibrary.org/obo/NCBITaxon_" + taxon;
            }
            taxa_constraint += "?id <" + BlazegraphOntologyManager.in_taxon_uri + "> <" + taxon + "> . \n";
        }
    }
    // if(taxa!=null&&!taxa.isEmpty()) {
    // String model_filter =  " VALUES ?id { \n";
    // for(String taxon : taxa) {
    // if(taxon.startsWith("NCBITaxon:")) {
    // taxon = taxon.replace(":", "_");
    // taxon = "http://purl.obolibrary.org/obo/"+taxon;
    // }
    // else if(!taxon.startsWith("http://purl.obolibrary.org/obo/NCBITaxon_")) {
    // taxon = "http://purl.obolibrary.org/obo/NCBITaxon_"+taxon;
    // }
    // Set<String> models = taxon_models.get(taxon);
    // if(models!=null) {
    // for(String model : models) {
    // model_filter+="<"+model+"> \n";
    // }
    // }
    // }
    // model_filter += "} . \n";
    // taxa_constraint = model_filter;
    // }
    String title_search_constraint = "";
    if (title_search != null) {
        title_search_constraint = "?title <http://www.bigdata.com/rdf/search#search> \"" + title_search + "\" .\n";
        if (!title_search.contains("*")) {
            title_search_constraint += " ?title <http://www.bigdata.com/rdf/search#matchAllTerms> \"" + "true" + "\" . \n";
        }
    // if(exact_match) {
    // title_search_constraint+=" ?title <http://www.bigdata.com/rdf/search#matchExact>  \""+"true"+"\" . \n";
    // }
    }
    String state_search_constraint = "";
    if (state_search != null && state_search.size() > 0) {
        String allowed_states = "";
        int c = 0;
        for (String s : state_search) {
            c++;
            allowed_states += "\"" + s + "\"";
            if (c < state_search.size()) {
                allowed_states += ",";
            }
        }
        // FILTER (?state IN ("production", , "development", "review", "closed", "delete" ))
        state_search_constraint = "FILTER (?state IN (" + allowed_states + ")) . \n";
    }
    String contributor_search_constraint = "";
    if (contributor_search != null && contributor_search.size() > 0) {
        String allowed_contributors = "";
        int c = 0;
        for (String contributor : contributor_search) {
            c++;
            allowed_contributors += "\"" + contributor + "\"";
            if (c < contributor_search.size()) {
                allowed_contributors += ",";
            }
        }
        contributor_search_constraint = " ?id <http://purl.org/dc/elements/1.1/contributor> ?test_contributor . \n" + " FILTER (?test_contributor IN (" + allowed_contributors + ")) . \n";
    }
    String group_search_constraint = "";
    if (group_search != null && group_search.size() > 0) {
        String allowed_group = "";
        int c = 0;
        for (String group : group_search) {
            c++;
            allowed_group += "\"" + group + "\"";
            if (c < group_search.size()) {
                allowed_group += ",";
            }
        }
        group_search_constraint = " ?id <http://purl.org/pav/providedBy> ?test_group . \n" + "FILTER (?test_group IN (" + allowed_group + ")) . \n";
    }
    String date_constraint = "";
    if (exactdate != null && exactdate.length() == 10) {
        date_constraint = "FILTER (?date = '" + exactdate + "') \n";
    } else if (date_search != null && date_search.length() == 10) {
        // e.g. 2019-06-26
        date_constraint = "FILTER (?date > '" + date_search + "') \n";
        if (datend != null && datend.length() == 10) {
            date_constraint = "FILTER (?date > '" + date_search + "' && ?date < '" + datend + "') \n";
        }
    }
    String offset_constraint = "";
    if (offset != 0) {
        offset_constraint = "OFFSET " + offset + "\n";
    }
    String limit_constraint = "";
    if (limit != 0) {
        limit_constraint = "LIMIT " + limit + "\n";
    }
    if (offset == 0 && limit == 0) {
        limit_constraint = "LIMIT 1000\n";
    }
    // default group by
    String group_by_constraint = "GROUP BY ?id";
    // default return block
    // TODO investigate need to add DISTINCT to GROUP_CONCAT here
    String return_block = "?id (MIN(?date) AS ?mindate) (MIN(?title) AS ?mintitle) (MIN(?state) AS ?minstate) <ind_return_list> (GROUP_CONCAT(DISTINCT ?contributor;separator=\";\") AS ?contributors) (GROUP_CONCAT(DISTINCT ?group;separator=\";\") AS ?groups)";
    if (count != null) {
        return_block = "(count(distinct ?id) as ?count)";
        limit_constraint = "";
        offset_constraint = "";
        group_by_constraint = "";
    }
    sparql = sparql.replaceAll("<return_block>", return_block);
    sparql = sparql.replaceAll("<id_constraint>", id_constraint);
    sparql = sparql.replaceAll("<group_by_constraint>", group_by_constraint);
    sparql = sparql.replaceAll("<ind_return_list>", ind_return_list);
    sparql = sparql.replaceAll("<types>", types);
    sparql = sparql.replaceAll("<pmid_constraints>", pmid_constraints);
    sparql = sparql.replaceAll("<title_constraint>", title_search_constraint);
    sparql = sparql.replaceAll("<state_constraint>", state_search_constraint);
    sparql = sparql.replaceAll("<contributor_constraint>", contributor_search_constraint);
    sparql = sparql.replaceAll("<group_constraint>", group_search_constraint);
    sparql = sparql.replaceAll("<date_constraint>", date_constraint);
    sparql = sparql.replaceAll("<limit_constraint>", limit_constraint);
    sparql = sparql.replaceAll("<offset_constraint>", offset_constraint);
    sparql = sparql.replaceAll("<taxa_constraint>", taxa_constraint);
    if (debug != null) {
        r.sparql = sparql;
    } else {
        r.sparql = "add 'debug' parameter to see sparql request";
    }
    TupleQueryResult result;
    try {
        result = (TupleQueryResult) m3.executeSPARQLQuery(sparql, 1000);
    } catch (MalformedQueryException | QueryEvaluationException | RepositoryException e) {
        if (e instanceof MalformedQueryException) {
            r.message = "Malformed Query";
        } else if (e instanceof QueryEvaluationException) {
            r.message = "Query Evaluation Problem - probably a time out";
        } else if (e instanceof RepositoryException) {
            r.message = "Repository Exception";
        }
        r.error = e.getMessage();
        e.printStackTrace();
        return r;
    }
    String n_count = null;
    try {
        while (result.hasNext()) {
            BindingSet bs = result.next();
            if (count != null) {
                n_count = bs.getBinding("count").getValue().stringValue();
            } else {
                // model meta
                String model_iri_string = bs.getBinding("id").getValue().stringValue();
                IRI model_iri = IRI.create(model_iri_string);
                String model_curie = null;
                try {
                    model_curie = curie_handler.getCuri(IRI.create(model_iri_string));
                    if (model_curie == null) {
                        model_curie = model_iri_string;
                    }
                } catch (Exception e) {
                    r.error += e.getMessage() + " \n ";
                    e.printStackTrace();
                    return r;
                }
                String date = bs.getBinding("mindate").getValue().stringValue();
                String title = bs.getBinding("mintitle").getValue().stringValue();
                String contribs = bs.getBinding("contributors").getValue().stringValue();
                // optional values (some are empty)
                Binding state_binding = bs.getBinding("minstate");
                String state = "";
                if (state_binding != null) {
                    state = state_binding.getValue().stringValue();
                }
                Binding group_binding = bs.getBinding("groups");
                String groups_ = "";
                if (group_binding != null) {
                    groups_ = group_binding.getValue().stringValue();
                }
                Set<String> contributors = new HashSet<String>(Arrays.asList(contribs.split(";")));
                Set<String> groups = new HashSet<String>();
                if (groups_ != null) {
                    groups.addAll(Arrays.asList(groups_.split(";")));
                }
                ModelMeta mm = id_model.get(model_curie);
                if (mm == null) {
                    // look up model in in-memory cache to check edit state
                    boolean is_modified = m3.isModelModified(model_iri);
                    mm = new ModelMeta(model_curie, date, title, state, contributors, groups, is_modified);
                }
                // matching
                for (String ind : ind_return.keySet()) {
                    String bindingName = ind.replace("?ind", "inds");
                    String[] ind_class_matches = bs.getBinding(bindingName).getValue().stringValue().split(" ", -1);
                    for (String ind_class_match : ind_class_matches) {
                        Set<String> matching_inds = mm.query_match.get(ind_return.get(ind));
                        if (matching_inds == null) {
                            matching_inds = new HashSet<String>();
                        }
                        matching_inds.add(ind_class_match);
                        mm.query_match.put(ind_return.get(ind), matching_inds);
                    }
                }
                id_model.put(model_curie, mm);
            }
        }
    } catch (QueryEvaluationException e) {
        r.message = "Query Evaluation Problem - probably a time out";
        r.error = e.getMessage();
        e.printStackTrace();
        return r;
    }
    if (n_count != null) {
        r.n = Integer.parseInt(n_count);
    } else {
        r.n = id_model.size();
        r.models = new LinkedHashSet<ModelMeta>(id_model.values());
    }
    try {
        result.close();
    } catch (QueryEvaluationException e) {
        r.message = "Query Evaluation Problem - can't close result set";
        r.error = e.getMessage();
        e.printStackTrace();
        return r;
    }
    // http://127.0.0.1:6800/modelsearch/?query=bla
    return r;
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) CurieHandler(org.geneontology.minerva.curie.CurieHandler) RepositoryException(org.openrdf.repository.RepositoryException) IOException(java.io.IOException) IOException(java.io.IOException) RepositoryException(org.openrdf.repository.RepositoryException) UnknownIdentifierException(org.geneontology.minerva.MolecularModelManager.UnknownIdentifierException) UnknownIdentifierException(org.geneontology.minerva.MolecularModelManager.UnknownIdentifierException)

Aggregations

UnknownIdentifierException (org.geneontology.minerva.MolecularModelManager.UnknownIdentifierException)4 MissingParameterException (org.geneontology.minerva.server.handler.OperationsTools.MissingParameterException)2 IRI (org.semanticweb.owlapi.model.IRI)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 CurieHandler (org.geneontology.minerva.curie.CurieHandler)1 JsonOwlObject (org.geneontology.minerva.json.JsonOwlObject)1 LookupEntry (org.geneontology.minerva.lookup.ExternalLookupService.LookupEntry)1 Test (org.junit.Test)1 RepositoryException (org.openrdf.repository.RepositoryException)1 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)1