Search in sources :

Example 1 with Subject

use of gov.nih.nci.ctd2.dashboard.model.Subject in project nci-ctd2-dashboard by CBIIT.

the class DashboardDaoImpl method createObservedSubjectInfo.

private List<SubjectItem> createObservedSubjectInfo(Integer observationId) {
    Session session1 = getSession();
    @SuppressWarnings("unchecked") org.hibernate.query.Query<Object[]> query1 = session1.createNativeQuery("SELECT d2.displayName AS role, observed_subject_role.displayText AS description, d1.displayName AS name, subject.id, columnName, stableURL" + " FROM observed_subject join subject on observed_subject.subject_id=subject.id" + " JOIN dashboard_entity d1 ON subject.id=d1.id" + " JOIN observed_subject_role ON observed_subject.observedSubjectRole_id = observed_subject_role.id" + " JOIN subject_role ON observed_subject_role.subjectRole_id=subject_role.id" + " JOIN dashboard_entity AS d2 ON subject_role.id=d2.id" + " where observation_id=" + observationId);
    List<Object[]> subjects = query1.list();
    List<SubjectItem> list = new ArrayList<SubjectItem>();
    for (Object[] obj : subjects) {
        String role = (String) obj[0];
        String description = (String) obj[1];
        String name = (String) obj[2];
        Integer subjectId = (Integer) obj[3];
        String columnName = (String) obj[4];
        String stableURL = (String) obj[5];
        @SuppressWarnings("unchecked") org.hibernate.query.Query<String> query2 = session1.createNativeQuery("SELECT displayName FROM subject_synonym_map " + " JOIN synonym ON subject_synonym_map.synonyms_id=synonym.id " + " JOIN dashboard_entity ON synonym.id = dashboard_entity.id" + " WHERE SubjectImpl_id=" + subjectId);
        List<String> synonyms = query2.list();
        @SuppressWarnings("unchecked") org.hibernate.query.Query<Object[]> query3 = session1.createNativeQuery("SELECT databaseId, databaseName FROM subject_xref_map" + " JOIN xref ON subject_xref_map.xrefs_id=xref.id " + " WHERE SubjectImpl_id=" + subjectId);
        List<Object[]> xrefs = query3.list();
        List<XRefItem> xrefItems = new ArrayList<XRefItem>();
        for (Object[] x : xrefs) {
            xrefItems.add(new XRefItem((String) x[1], (String) x[0]));
        }
        SubjectItem subjectItem = new SubjectItem(stableURL, role, description, name, synonyms.toArray(new String[0]), xrefItems.toArray(new XRefItem[0]), columnName);
        list.add(subjectItem);
    }
    session1.close();
    return list;
}
Also used : ArrayList(java.util.ArrayList) XRefItem(gov.nih.nci.ctd2.dashboard.api.XRefItem) SubjectItem(gov.nih.nci.ctd2.dashboard.api.SubjectItem) BigInteger(java.math.BigInteger) FullTextSession(org.hibernate.search.FullTextSession) Session(org.hibernate.Session)

Example 2 with Subject

use of gov.nih.nci.ctd2.dashboard.model.Subject in project nci-ctd2-dashboard by CBIIT.

the class DashboardDaoImpl method ontologySearch.

/*
     * To get observation 'search' results, i.e. the intersection concept, the
     * implmentation of ontology search will be much more complex. Ideally it would
     * be better to compeltely separate the observation part, but to avoid repeating
     * the actual hierarchical searching, embedding observations here is the best
     * choice. Although the previous implementation is better for searching
     * subjects, but to cover the observation parts, we have no choice but to
     * compromise the clarity here.
     * 
     * Other points of consideration for the purpose of observation 'search': (1)
     * the ontologySearch cover the original non-ontology subject results (in
     * principle) (2) the subjects other than TissueSample and ECO term are neither
     * affected or covered by ontology search so it is not consistent.
     */
@Override
public SearchResults ontologySearch(String queryString) {
    final String[] searchTerms = parseWords(queryString);
    Set<Integer> observationsIntersection = null;
    Set<SubjectResult> subject_result = null;
    final int termCount = searchTerms.length;
    if (termCount <= 1) {
        // prevent wasting time finding observations
        subject_result = new HashSet<SubjectResult>(ontologySearchOneTerm(searchTerms[0].replace("\"", ""), null));
    } else {
        boolean first = true;
        Map<SubjectResult, Integer> subjectResultMap = new HashMap<SubjectResult, Integer>();
        for (String oneTerm : searchTerms) {
            oneTerm = oneTerm.replace("\"", "");
            log.debug("ontology search term:" + oneTerm);
            Set<Integer> observations = new HashSet<Integer>();
            List<SubjectResult> oneTermList = ontologySearchOneTerm(oneTerm, observations);
            for (SubjectResult s : oneTermList) {
                Integer matchNumber = subjectResultMap.get(s);
                if (matchNumber != null) {
                    s.matchNumber = matchNumber + 1;
                }
                subjectResultMap.put(s, s.getMatchNumber());
            }
            if (first) {
                observationsIntersection = observations;
                first = false;
            } else {
                observationsIntersection.retainAll(observations);
            }
        }
        subject_result = subjectResultMap.keySet();
    }
    SearchResults searchResults = new SearchResults();
    if (subject_result.size() > maxNumberOfSearchResults) {
        searchResults.oversized = subject_result.size();
        searchResults.subject_result = subject_result.stream().sorted(new SearchResultComparator()).limit(maxNumberOfSearchResults).collect(Collectors.toList());
        log.debug("size after limiting: " + subject_result.size());
    } else {
        searchResults.subject_result = new ArrayList<SubjectResult>(subject_result);
    }
    if (observationsIntersection != null) {
        searchResults.observation_result = observationsIntersection.stream().map(id -> this.getEntityById(Observation.class, id)).collect(Collectors.toList());
        log.debug("size of observation intersection: " + observationsIntersection.size());
    }
    return searchResults;
}
Also used : HashMap(java.util.HashMap) SearchResults(gov.nih.nci.ctd2.dashboard.util.SearchResults) BigInteger(java.math.BigInteger) SubjectResult(gov.nih.nci.ctd2.dashboard.util.SubjectResult) Observation(gov.nih.nci.ctd2.dashboard.model.Observation) HashSet(java.util.HashSet)

Example 3 with Subject

use of gov.nih.nci.ctd2.dashboard.model.Subject in project nci-ctd2-dashboard by CBIIT.

the class DashboardDaoImpl method summarize.

@Override
public void summarize() {
    findEntities(Summary.class).forEach(s -> delete(s));
    Map<Class<?>, String> summaryClasses = new HashMap<Class<?>, String>();
    summaryClasses.put(AnimalModel.class, "Animal Models");
    summaryClasses.put(CellSample.class, "Cell Lines");
    summaryClasses.put(Compound.class, "Compounds");
    summaryClasses.put(Gene.class, "Genes");
    summaryClasses.put(ShRna.class, "shRNA");
    summaryClasses.put(TissueSample.class, "Disease Contexts (Tissues)");
    summaryClasses.forEach((clazz, label) -> {
        @SuppressWarnings("unchecked") Summary s = summarizePerSubject((Class<? extends Subject>) clazz, label);
        save(s);
    });
    Summary s = summarizeStories();
    save(s);
    Summary eco = summarizeECO();
    save(eco);
    Summary total = summarizeTotal();
    save(total);
}
Also used : HashMap(java.util.HashMap) Summary(gov.nih.nci.ctd2.dashboard.util.Summary)

Example 4 with Subject

use of gov.nih.nci.ctd2.dashboard.model.Subject in project nci-ctd2-dashboard by CBIIT.

the class DashboardDaoImpl method search.

@Override
@Cacheable(value = "searchCache")
public SearchResults search(String queryString) {
    queryString = queryString.trim();
    final String[] searchTerms = parseWords(queryString);
    log.debug("search terms: " + String.join(",", searchTerms));
    Map<Subject, Integer> subjects = new HashMap<Subject, Integer>();
    Map<Submission, Integer> submissions = new HashMap<Submission, Integer>();
    for (String singleTerm : searchTerms) {
        searchSingleTerm(singleTerm, subjects, submissions);
    }
    SearchResults searchResults = new SearchResults();
    searchResults.submission_result = submissions.keySet().stream().map(submission -> {
        ObservationTemplate template = submission.getObservationTemplate();
        return new SearchResults.SubmissionResult(submission.getStableURL(), submission.getSubmissionDate(), template.getDescription(), template.getTier(), template.getSubmissionCenter().getDisplayName(), submission.getId(), findObservationsBySubmission(submission).size(), template.getIsSubmissionStory());
    }).collect(Collectors.toList());
    Map<String, Set<Observation>> observationMap = new HashMap<String, Set<Observation>>();
    List<SubjectResult> subject_result = new ArrayList<SubjectResult>();
    for (Subject subject : subjects.keySet()) {
        Set<Observation> observations = new HashSet<Observation>();
        Set<SubmissionCenter> submissionCenters = new HashSet<SubmissionCenter>();
        Set<String> roles = new HashSet<String>();
        for (ObservedSubject observedSubject : findObservedSubjectBySubject(subject)) {
            Observation observation = observedSubject.getObservation();
            observations.add(observation);
            ObservationTemplate observationTemplate = observation.getSubmission().getObservationTemplate();
            submissionCenters.add(observationTemplate.getSubmissionCenter());
            roles.add(observedSubject.getObservedSubjectRole().getSubjectRole().getDisplayName());
        }
        SubjectResult x = new SubjectResult(subject, observations.size(), submissionCenters.size(), subjects.get(subject), roles);
        Arrays.stream(searchTerms).filter(term -> matchSubject(term, subject)).forEach(term -> {
            Set<Observation> obset = observationMap.get(term);
            if (obset == null) {
                obset = new HashSet<Observation>();
            }
            obset.addAll(observations);
            observationMap.put(term, obset);
        });
        subject_result.add(x);
    }
    /* search ECO terms */
    List<ECOTerm> ecoterms = findECOTerms(queryString);
    for (ECOTerm ecoterm : ecoterms) {
        List<Integer> observationIds = observationIdsForEcoCode(ecoterm.getCode());
        int observationNumber = observationIds.size();
        if (observationNumber == 0)
            continue;
        SubjectResult entity = new SubjectResult(ecoterm, observationNumber, centerCount(ecoterm.getCode()), null, // no matchNumber, no roles
        null);
        subject_result.add(entity);
        Set<Observation> observations = new HashSet<Observation>();
        observationIds.forEach(obid -> observations.add(getEntityById(Observation.class, obid)));
        Arrays.stream(searchTerms).filter(term -> ecoterm.containsTerm(term)).forEach(term -> {
            Set<Observation> obset = observationMap.get(term);
            if (obset == null) {
                obset = new HashSet<Observation>();
            }
            obset.addAll(observations);
            observationMap.put(term, obset);
        });
    }
    /*
         * Limit the size. This should be done more efficiently during the process of
         * builing up of the list.
         * Because the limit needs to be based on 'match number' ranking, which depends
         * on all terms, an efficient algorithm is not obvious.
         * Unfortunately, we also have to do this after processing all results because
         * we need (in fact more often) observation numbers as well in ranking. TODO
         */
    if (subject_result.size() > maxNumberOfSearchResults) {
        searchResults.oversized = subject_result.size();
        subject_result = subject_result.stream().sorted(new SearchResultComparator()).limit(maxNumberOfSearchResults).collect(Collectors.toList());
        log.debug("size after limiting: " + subject_result.size());
    }
    searchResults.subject_result = subject_result;
    if (searchTerms.length <= 1) {
        return searchResults;
    }
    // add intersection of observations
    Set<Observation> set0 = observationMap.get(searchTerms[0]);
    if (set0 == null) {
        log.debug("no observation for " + searchTerms[0]);
        return searchResults;
    }
    log.debug("set0 size=" + set0.size());
    for (int i = 1; i < searchTerms.length; i++) {
        Set<Observation> obset = observationMap.get(searchTerms[i]);
        if (obset == null) {
            log.debug("... no observation for " + searchTerms[i]);
            return searchResults;
        }
        log.debug("set " + i + " size=" + obset.size());
        set0.retainAll(obset);
    }
    // set0 is now the intersection
    if (set0.size() == 0) {
        log.debug("no intersection of observations");
    }
    if (set0.size() > maxNumberOfSearchResults) {
        searchResults.oversized_observations = set0.size();
        // no particular ranking is enforced when limiting
        set0 = set0.stream().limit(maxNumberOfSearchResults).collect(Collectors.toSet());
        log.debug("observation results count after limiting: " + set0.size());
    }
    searchResults.observation_result = new ArrayList<Observation>(set0);
    return searchResults;
}
Also used : Query(org.apache.lucene.search.Query) ObservedEvidenceRole(gov.nih.nci.ctd2.dashboard.model.ObservedEvidenceRole) Transcript(gov.nih.nci.ctd2.dashboard.model.Transcript) Arrays(java.util.Arrays) DashboardDao(gov.nih.nci.ctd2.dashboard.dao.DashboardDao) ObservedSubjectRole(gov.nih.nci.ctd2.dashboard.model.ObservedSubjectRole) Cacheable(org.springframework.cache.annotation.Cacheable) NoResultException(javax.persistence.NoResultException) XRefItem(gov.nih.nci.ctd2.dashboard.api.XRefItem) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) SubmissionCenter(gov.nih.nci.ctd2.dashboard.model.SubmissionCenter) Matcher(java.util.regex.Matcher) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) Map(java.util.Map) DashboardEntity(gov.nih.nci.ctd2.dashboard.model.DashboardEntity) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) BigInteger(java.math.BigInteger) TissueSample(gov.nih.nci.ctd2.dashboard.model.TissueSample) Organism(gov.nih.nci.ctd2.dashboard.model.Organism) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) ScrollableResults(org.hibernate.ScrollableResults) SearchResults(gov.nih.nci.ctd2.dashboard.util.SearchResults) SubjectResult(gov.nih.nci.ctd2.dashboard.util.SubjectResult) Collection(java.util.Collection) SessionFactory(org.hibernate.SessionFactory) Set(java.util.Set) FullTextQuery(org.hibernate.search.FullTextQuery) CellSample(gov.nih.nci.ctd2.dashboard.model.CellSample) Compound(gov.nih.nci.ctd2.dashboard.model.Compound) ECOTerm(gov.nih.nci.ctd2.dashboard.model.ECOTerm) SubjectWithOrganism(gov.nih.nci.ctd2.dashboard.model.SubjectWithOrganism) Collectors(java.util.stream.Collectors) TissueSampleImpl(gov.nih.nci.ctd2.dashboard.impl.TissueSampleImpl) ObservationItem(gov.nih.nci.ctd2.dashboard.api.ObservationItem) List(java.util.List) Xref(gov.nih.nci.ctd2.dashboard.model.Xref) EcoBrowse(gov.nih.nci.ctd2.dashboard.util.EcoBrowse) CompoundImpl(gov.nih.nci.ctd2.dashboard.impl.CompoundImpl) ScrollMode(org.hibernate.ScrollMode) ObservedSubject(gov.nih.nci.ctd2.dashboard.model.ObservedSubject) DashboardEntityImpl(gov.nih.nci.ctd2.dashboard.impl.DashboardEntityImpl) Gene(gov.nih.nci.ctd2.dashboard.model.Gene) Pattern(java.util.regex.Pattern) LogFactory(org.apache.commons.logging.LogFactory) ShRna(gov.nih.nci.ctd2.dashboard.model.ShRna) Observation(gov.nih.nci.ctd2.dashboard.model.Observation) ParseException(org.apache.lucene.queryparser.classic.ParseException) FullTextSession(org.hibernate.search.FullTextSession) SubmissionImpl(gov.nih.nci.ctd2.dashboard.impl.SubmissionImpl) Subject(gov.nih.nci.ctd2.dashboard.model.Subject) Submission(gov.nih.nci.ctd2.dashboard.model.Submission) Session(org.hibernate.Session) HashMap(java.util.HashMap) EvidenceItem(gov.nih.nci.ctd2.dashboard.api.EvidenceItem) Evidence(gov.nih.nci.ctd2.dashboard.model.Evidence) TypedQuery(javax.persistence.TypedQuery) ObservationTemplate(gov.nih.nci.ctd2.dashboard.model.ObservationTemplate) SubjectWithSummaries(gov.nih.nci.ctd2.dashboard.util.SubjectWithSummaries) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Search(org.hibernate.search.Search) Summary(gov.nih.nci.ctd2.dashboard.util.Summary) SubjectImpl(gov.nih.nci.ctd2.dashboard.impl.SubjectImpl) AnimalModel(gov.nih.nci.ctd2.dashboard.model.AnimalModel) ObservedEvidence(gov.nih.nci.ctd2.dashboard.model.ObservedEvidence) WordCloudEntry(gov.nih.nci.ctd2.dashboard.util.WordCloudEntry) SubjectWithOrganismImpl(gov.nih.nci.ctd2.dashboard.impl.SubjectWithOrganismImpl) ObservationURIsAndTiers(gov.nih.nci.ctd2.dashboard.util.ObservationURIsAndTiers) FlushMode(org.hibernate.FlushMode) SubjectItem(gov.nih.nci.ctd2.dashboard.api.SubjectItem) Hierarchy(gov.nih.nci.ctd2.dashboard.util.Hierarchy) Annotation(gov.nih.nci.ctd2.dashboard.model.Annotation) ObservationTemplateImpl(gov.nih.nci.ctd2.dashboard.impl.ObservationTemplateImpl) Synonym(gov.nih.nci.ctd2.dashboard.model.Synonym) Protein(gov.nih.nci.ctd2.dashboard.model.Protein) Log(org.apache.commons.logging.Log) DashboardFactory(gov.nih.nci.ctd2.dashboard.model.DashboardFactory) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SearchResults(gov.nih.nci.ctd2.dashboard.util.SearchResults) ObservationTemplate(gov.nih.nci.ctd2.dashboard.model.ObservationTemplate) ECOTerm(gov.nih.nci.ctd2.dashboard.model.ECOTerm) HashSet(java.util.HashSet) Submission(gov.nih.nci.ctd2.dashboard.model.Submission) ObservedSubject(gov.nih.nci.ctd2.dashboard.model.ObservedSubject) Subject(gov.nih.nci.ctd2.dashboard.model.Subject) BigInteger(java.math.BigInteger) SubmissionCenter(gov.nih.nci.ctd2.dashboard.model.SubmissionCenter) SubjectResult(gov.nih.nci.ctd2.dashboard.util.SubjectResult) Observation(gov.nih.nci.ctd2.dashboard.model.Observation) ObservedSubject(gov.nih.nci.ctd2.dashboard.model.ObservedSubject) Cacheable(org.springframework.cache.annotation.Cacheable)

Example 5 with Subject

use of gov.nih.nci.ctd2.dashboard.model.Subject in project nci-ctd2-dashboard by CBIIT.

the class DashboardDaoImpl method searchSingleTerm.

private void searchSingleTerm(final String singleTerm, final Map<Subject, Integer> subjects, final Map<Submission, Integer> submissions) {
    FullTextSession fullTextSession = Search.getFullTextSession(getSession());
    MultiFieldQueryParser multiFieldQueryParser = new MultiFieldQueryParser(defaultSearchFields, new KeywordAnalyzer());
    Query luceneQuery = null;
    try {
        luceneQuery = multiFieldQueryParser.parse(singleTerm);
        log.debug(luceneQuery);
    } catch (ParseException e) {
        e.printStackTrace();
        return;
    }
    FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery, searchableClasses);
    fullTextQuery.setReadOnly(true);
    List<?> list = fullTextQuery.list();
    fullTextSession.close();
    Integer numberOfSearchResults = getMaxNumberOfSearchResults();
    if (numberOfSearchResults > 0 && list.size() > numberOfSearchResults) {
        // if lte 0, the maximum number is ignored
        log.warn("search result number " + list.size() + " is larger than the maximum expected, " + numberOfSearchResults);
    }
    for (Object o : list) {
        if (o instanceof ObservationTemplate) {
            List<Submission> submissionList = queryWithClass("select o from SubmissionImpl as o where o.observationTemplate = :ot", "ot", (ObservationTemplate) o);
            for (Submission submission : submissionList) {
                if (submissions.containsKey(submission)) {
                    submissions.put(submission, submissions.get(submission) + 1);
                } else {
                    submissions.put(submission, 1);
                }
            }
        } else if (o instanceof Subject) {
            Subject s = (Subject) o;
            if (subjects.containsKey(s)) {
                subjects.put(s, subjects.get(s) + 1);
            } else {
                subjects.put(s, 1);
            }
        } else {
            log.warn("unexpected type returned by searching: " + o.getClass().getName());
        }
    }
}
Also used : KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) FullTextSession(org.hibernate.search.FullTextSession) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) Query(org.apache.lucene.search.Query) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) FullTextQuery(org.hibernate.search.FullTextQuery) TypedQuery(javax.persistence.TypedQuery) Submission(gov.nih.nci.ctd2.dashboard.model.Submission) ObservedSubject(gov.nih.nci.ctd2.dashboard.model.ObservedSubject) Subject(gov.nih.nci.ctd2.dashboard.model.Subject) BigInteger(java.math.BigInteger) ObservationTemplate(gov.nih.nci.ctd2.dashboard.model.ObservationTemplate) ParseException(org.apache.lucene.queryparser.classic.ParseException) FullTextQuery(org.hibernate.search.FullTextQuery)

Aggregations

Subject (gov.nih.nci.ctd2.dashboard.model.Subject)15 ArrayList (java.util.ArrayList)14 ObservedSubject (gov.nih.nci.ctd2.dashboard.model.ObservedSubject)12 BigInteger (java.math.BigInteger)12 FullTextSession (org.hibernate.search.FullTextSession)11 Session (org.hibernate.Session)10 HashSet (java.util.HashSet)9 JSONSerializer (flexjson.JSONSerializer)8 ObservationTemplate (gov.nih.nci.ctd2.dashboard.model.ObservationTemplate)8 HashMap (java.util.HashMap)8 HttpHeaders (org.springframework.http.HttpHeaders)8 ResponseEntity (org.springframework.http.ResponseEntity)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 Xref (gov.nih.nci.ctd2.dashboard.model.Xref)7 Transactional (org.springframework.transaction.annotation.Transactional)7 Submission (gov.nih.nci.ctd2.dashboard.model.Submission)6 WordCloudEntry (gov.nih.nci.ctd2.dashboard.util.WordCloudEntry)6 DashboardEntity (gov.nih.nci.ctd2.dashboard.model.DashboardEntity)5 Observation (gov.nih.nci.ctd2.dashboard.model.Observation)5 ObservedSubjectRole (gov.nih.nci.ctd2.dashboard.model.ObservedSubjectRole)5