use of gov.nih.nci.ctd2.dashboard.model.ObservationTemplate 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;
}
use of gov.nih.nci.ctd2.dashboard.model.ObservationTemplate 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());
}
}
}
use of gov.nih.nci.ctd2.dashboard.model.ObservationTemplate in project nci-ctd2-dashboard by CBIIT.
the class DashboardDaoImpl method findObservedSubjectRole.
@Override
public ObservedSubjectRole findObservedSubjectRole(String templateName, String columnName) {
List<ObservedSubjectRole> list = new ArrayList<ObservedSubjectRole>();
// first grab observation template name
List<ObservationTemplate> otList = queryWithClass("from ObservationTemplateImpl where displayName = :templateName", "templateName", templateName);
for (ObservationTemplate ot : otList) {
List<ObservedSubjectRole> osrList = query2ParamsWithClass("from ObservedSubjectRoleImpl as osr where columnName = :columnName and " + "osr.observationTemplate = :ot", "columnName", columnName, "ot", ot);
for (ObservedSubjectRole o : osrList) {
list.add(o);
}
}
assert list.size() <= 1;
return (list.size() == 1) ? list.iterator().next() : null;
}
use of gov.nih.nci.ctd2.dashboard.model.ObservationTemplate in project nci-ctd2-dashboard by CBIIT.
the class ControlledVocabularyPerColumnFieldSetMapper method mapFieldSet.
public ControlledVocabulary mapFieldSet(FieldSet fieldSet) throws BindException {
String templateName = fieldSet.readString(TEMPLATE_NAME);
ObservationTemplate observationTemplate = observationTemplateNameMap.get(templateName);
if (observationTemplate == null)
return new ControlledVocabulary(null, null, null);
if (subjectRoleCache == null)
subjectRoleCache = new HashMap<String, SubjectRole>();
if (evidenceRoleCache == null)
evidenceRoleCache = new HashMap<String, EvidenceRole>();
String subject = fieldSet.readString(SUBJECT);
String evidence = fieldSet.readString(EVIDENCE);
if (subject.length() > 0) {
String subjectRoleStr = fieldSet.readString(ROLE);
SubjectRole subjectRole = subjectRoleCache.get(subjectRoleStr);
if (subjectRole == null) {
subjectRole = dashboardFactory.create(SubjectRole.class);
subjectRole.setDisplayName(subjectRoleStr);
subjectRoleCache.put(subjectRoleStr, subjectRole);
}
ObservedSubjectRole observedSubjectRole = dashboardFactory.create(ObservedSubjectRole.class);
observedSubjectRole.setSubjectRole(subjectRole);
observedSubjectRole.setColumnName(fieldSet.readString(COLUMN_NAME));
observedSubjectRole.setDisplayText(fieldSet.readString(DISPLAY_TEXT));
observedSubjectRole.setObservationTemplate(observationTemplate);
return new ControlledVocabulary(observationTemplate, subjectRole, observedSubjectRole);
} else if (evidence.length() > 0) {
String evidenceRoleStr = fieldSet.readString(ROLE);
EvidenceRole evidenceRole = evidenceRoleCache.get(evidenceRoleStr);
if (evidenceRole == null) {
evidenceRole = dashboardFactory.create(EvidenceRole.class);
evidenceRole.setDisplayName(evidenceRoleStr);
evidenceRoleCache.put(evidenceRoleStr, evidenceRole);
}
ObservedEvidenceRole observedEvidenceRole = dashboardFactory.create(ObservedEvidenceRole.class);
observedEvidenceRole.setEvidenceRole(evidenceRole);
observedEvidenceRole.setColumnName(fieldSet.readString(COLUMN_NAME));
observedEvidenceRole.setDisplayText(fieldSet.readString(DISPLAY_TEXT));
observedEvidenceRole.setObservationTemplate(observationTemplate);
observedEvidenceRole.setAttribute(getObservedEvidenceRoleAttribute(fieldSet.readString(MIME_TYPE), fieldSet.readString(NUMERIC_UNITS)));
return new ControlledVocabulary(observationTemplate, evidenceRole, observedEvidenceRole);
}
return new ControlledVocabulary(null, null, null);
}
use of gov.nih.nci.ctd2.dashboard.model.ObservationTemplate in project nci-ctd2-dashboard by CBIIT.
the class SubjectScorer method scoreAllRoles.
@Transactional
public void scoreAllRoles() {
log.info("Removing all role-based scores...");
List<SubjectWithSummaries> oldEntities = dashboardDao.findEntities(SubjectWithSummaries.class);
for (SubjectWithSummaries subjectWithSummaries : oldEntities) {
dashboardDao.delete(subjectWithSummaries);
}
log.info("Removed " + oldEntities.size() + " old scores.");
log.info("Re-scoring all roles...");
List<SubjectWithSummaries> subjectWithSummariesList = new ArrayList<SubjectWithSummaries>();
List<SubjectRole> entities = dashboardDao.findEntities(SubjectRole.class);
for (SubjectRole subjectRole : entities) {
String keyword = subjectRole.getDisplayName();
log.info("Scoring subject with role: " + keyword);
HashMap<Subject, SubjectWithSummaries> subjectToSummaries = new HashMap<Subject, SubjectWithSummaries>();
HashMap<Subject, HashSet<SubmissionCenter>> subjectToCenters = new HashMap<Subject, HashSet<SubmissionCenter>>();
HashMap<Subject, HashMap<SubmissionCenter, Integer>> centerBasedScores = new HashMap<Subject, HashMap<SubmissionCenter, Integer>>();
for (ObservedSubject observedSubject : dashboardDao.findObservedSubjectByRole(keyword)) {
Subject subject = observedSubject.getSubject();
SubjectWithSummaries withSummaries = subjectToSummaries.get(subject);
ObservationTemplate observationTemplate = observedSubject.getObservation().getSubmission().getObservationTemplate();
SubmissionCenter submissionCenter = observationTemplate.getSubmissionCenter();
Integer tier = observationTemplate.getTier();
if (withSummaries == null) {
withSummaries = new SubjectWithSummaries();
withSummaries.setRole(keyword);
withSummaries.setSubject(subject);
withSummaries.setMaxTier(tier);
withSummaries.setNumberOfObservations(1);
HashSet<SubmissionCenter> centers = new HashSet<SubmissionCenter>();
centers.add(submissionCenter);
withSummaries.setNumberOfSubmissionCenters(1);
withSummaries.addSubmission(tier, submissionCenter.getId());
subjectToCenters.put(subject, centers);
subjectToSummaries.put(subject, withSummaries);
HashMap<SubmissionCenter, Integer> cScores = new HashMap<>();
cScores.put(submissionCenter, tier);
centerBasedScores.put(subject, cScores);
} else {
withSummaries.setMaxTier(Math.max(withSummaries.getMaxTier(), tier));
withSummaries.setNumberOfObservations(withSummaries.getNumberOfObservations() + 1);
HashSet<SubmissionCenter> submissionCenters = subjectToCenters.get(subject);
submissionCenters.add(submissionCenter);
withSummaries.setNumberOfSubmissionCenters(submissionCenters.size());
withSummaries.addSubmission(tier, submissionCenter.getId());
HashMap<SubmissionCenter, Integer> cScores = centerBasedScores.get(subject);
Integer previousScore = cScores.get(submissionCenter);
cScores.put(submissionCenter, previousScore == null ? tier : Math.max(tier, previousScore));
}
}
Collection<SubjectWithSummaries> perRole = subjectToSummaries.values();
for (SubjectWithSummaries subjectWithSummaries : perRole) {
Integer totalScore = 0;
for (Integer aScore : centerBasedScores.get(subjectWithSummaries.getSubject()).values()) {
totalScore += aScore;
}
subjectWithSummaries.setScore(totalScore);
}
subjectWithSummariesList.addAll(perRole);
log.info("Done scoring role: " + keyword);
}
dashboardDao.batchSave(subjectWithSummariesList, 0);
log.info("Done scoring all roles...");
}
Aggregations