Search in sources :

Example 1 with SubjectResponse

use of gov.nih.nci.ctd2.dashboard.api.SubjectResponse in project nci-ctd2-dashboard by CBIIT.

the class BrowseAPI method getSubmission.

@Transactional
@RequestMapping(value = "{subjectClass}/{subjectName}", method = { RequestMethod.GET }, headers = "Accept=application/json")
public ResponseEntity<String> getSubmission(@PathVariable String subjectClass, @PathVariable String subjectName, @RequestParam(value = "center", required = false, defaultValue = "") String center, @RequestParam(value = "role", required = false, defaultValue = "") String role, @RequestParam(value = "tier", required = false, defaultValue = "") String tiers, @RequestParam(value = "maximum", required = false, defaultValue = "") String maximum) {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json; charset=utf-8");
    SubjectResponse.Filter filter = SubjectResponse.createFilter(center, role, tiers, maximum);
    DashboardEntity subject = null;
    if (subjectClass.equalsIgnoreCase("evidence") || subjectClass.equalsIgnoreCase("eco")) {
        /* API spec asks for Evidence but stable URL uses eco */
        var obj = dashboardDao.getEntityByStableURL("eco", "eco/" + subjectName);
        if (obj instanceof ECOTerm) {
            subject = (ECOTerm) obj;
        } else {
            log.error("unexpected subject type:" + obj.getClass().getName());
        }
    } else if (subjectClass.equalsIgnoreCase("gene")) {
        List<Gene> genes = dashboardDao.findGenesBySymbol(subjectName);
        if (genes.size() > 0) {
            Gene gene = genes.get(0);
            List<Protein> p = dashboardDao.findProteinByGene(gene);
            Xref xref = new XrefImpl();
            xref.setDatabaseId(p.get(0).getUniprotId());
            xref.setDatabaseName("UniProt");
            gene.getXrefs().add(xref);
            subject = gene;
        }
    } else {
        var obj = dashboardDao.getEntityByStableURL(subjectClass, subjectClass + "/" + subjectName);
        if (obj instanceof Subject) {
            subject = (Subject) obj;
        } else {
            log.error("unexpected subject type: " + (obj == null ? null : obj.getClass().getName()));
        }
    }
    if (subject == null) {
        return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
    }
    SubjectResponse subjectResponse = SubjectResponse.createInstance(subject, filter, dashboardDao);
    log.debug("ready to serialize");
    JSONSerializer jsonSerializer = CTD2Serializer.createJSONSerializer();
    String json = "{}";
    try {
        json = jsonSerializer.deepSerialize(subjectResponse);
    } catch (Exception e) {
        e.printStackTrace();
        return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity<String>(json, headers, HttpStatus.OK);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) SubjectResponse(gov.nih.nci.ctd2.dashboard.api.SubjectResponse) Subject(gov.nih.nci.ctd2.dashboard.model.Subject) Xref(gov.nih.nci.ctd2.dashboard.model.Xref) ResponseEntity(org.springframework.http.ResponseEntity) DashboardEntity(gov.nih.nci.ctd2.dashboard.model.DashboardEntity) Gene(gov.nih.nci.ctd2.dashboard.model.Gene) List(java.util.List) XrefImpl(gov.nih.nci.ctd2.dashboard.impl.XrefImpl) ECOTerm(gov.nih.nci.ctd2.dashboard.model.ECOTerm) JSONSerializer(flexjson.JSONSerializer) Transactional(org.springframework.transaction.annotation.Transactional) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with SubjectResponse

use of gov.nih.nci.ctd2.dashboard.api.SubjectResponse in project nci-ctd2-dashboard by CBIIT.

the class SubjectResponse method createInstance.

private static SubjectResponse createInstance(final Subject subject, final Filter filter, DashboardDao dashboardDao) {
    int[] tierCount = new int[3];
    Set<Integer> set = new HashSet<Integer>();
    for (ObservedSubject observedSubject : dashboardDao.findObservedSubjectBySubject(subject)) {
        ObservedSubjectRole observedSubjectRole = observedSubject.getObservedSubjectRole();
        String subjectRole = observedSubjectRole.getSubjectRole().getDisplayName();
        if (filter.rolesIncluded.size() > 0 && !filter.rolesIncluded.contains(subjectRole))
            continue;
        ObservationTemplate observatinoTemplate = observedSubject.getObservation().getSubmission().getObservationTemplate();
        Integer observationTier = observatinoTemplate.getTier();
        String centerNameBrief = observatinoTemplate.getSubmissionCenter().getStableURL().substring(7);
        if (filter.centerIncluded.size() > 0 && !filter.centerIncluded.contains(centerNameBrief))
            continue;
        if ((Arrays.asList(filter.tiersIncluded).contains(observationTier))) {
            set.add(observedSubject.getObservation().getId());
            tierCount[observationTier.intValue() - 1]++;
            if (filter.limit > 0 && set.size() >= filter.limit) {
                break;
            }
        }
    }
    List<ObservationItem> observations = dashboardDao.findObservationInfo(new ArrayList<Integer>(set));
    Set<String> roles = new TreeSet<String>();
    for (int i = 0; i < observations.size(); i++) {
        for (SubjectItem sub : observations.get(i).subject_list) {
            if (sub.getName().equals(subject.getDisplayName())) {
                roles.add(sub.getRole());
                break;
            }
        }
    }
    String[] uris = observations.stream().map(x -> x.uri).toArray(String[]::new);
    SubjectResponse subjectResponse = new SubjectResponse(subject, uris, roles.toArray(new String[0]), tierCount);
    return subjectResponse;
}
Also used : Arrays(java.util.Arrays) DashboardDao(gov.nih.nci.ctd2.dashboard.dao.DashboardDao) ObservedSubjectRole(gov.nih.nci.ctd2.dashboard.model.ObservedSubjectRole) Subject(gov.nih.nci.ctd2.dashboard.model.Subject) ObservationURIsAndTiers(gov.nih.nci.ctd2.dashboard.util.ObservationURIsAndTiers) Set(java.util.Set) ECOTerm(gov.nih.nci.ctd2.dashboard.model.ECOTerm) ObservationTemplate(gov.nih.nci.ctd2.dashboard.model.ObservationTemplate) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) Xref(gov.nih.nci.ctd2.dashboard.model.Xref) Synonym(gov.nih.nci.ctd2.dashboard.model.Synonym) DashboardEntity(gov.nih.nci.ctd2.dashboard.model.DashboardEntity) ObservedSubject(gov.nih.nci.ctd2.dashboard.model.ObservedSubject) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) TreeSet(java.util.TreeSet) ObservedSubjectRole(gov.nih.nci.ctd2.dashboard.model.ObservedSubjectRole) ObservationTemplate(gov.nih.nci.ctd2.dashboard.model.ObservationTemplate) ObservedSubject(gov.nih.nci.ctd2.dashboard.model.ObservedSubject) HashSet(java.util.HashSet)

Example 3 with SubjectResponse

use of gov.nih.nci.ctd2.dashboard.api.SubjectResponse in project nci-ctd2-dashboard by CBIIT.

the class SearchAPI method getSubmission.

@Transactional
@RequestMapping(value = "{term}", method = { RequestMethod.GET }, headers = "Accept=application/json")
public ResponseEntity<String> getSubmission(@PathVariable String term, @RequestParam(value = "center", required = false, defaultValue = "") String center, @RequestParam(value = "role", required = false, defaultValue = "") String role, @RequestParam(value = "tier", required = false, defaultValue = "") String tiers, @RequestParam(value = "maximum", required = false, defaultValue = "") String maximum) {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json; charset=utf-8");
    SubjectResponse.Filter filter = SubjectResponse.createFilter(center, role, tiers, maximum);
    List<SubjectResponse> allSubjects = new ArrayList<SubjectResponse>();
    List<SubjectResult> results = dashboardDao.search(term.toLowerCase()).subject_result;
    for (SubjectResult subjectResult : results) {
        try {
            Class<? extends DashboardEntity> clazz = Class.forName("gov.nih.nci.ctd2.dashboard.model." + subjectResult.className).asSubclass(DashboardEntity.class);
            ;
            DashboardEntity result = dashboardDao.getEntityById(clazz, subjectResult.id);
            SubjectResponse subjectResponse = SubjectResponse.createInstance(result, filter, dashboardDao);
            if (subjectResponse == null) {
                continue;
            }
            allSubjects.add(subjectResponse);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            continue;
        }
    }
    log.debug("ready to serialize");
    JSONSerializer jsonSerializer = CTD2Serializer.createJSONSerializer();
    String json = "{}";
    try {
        json = jsonSerializer.deepSerialize(allSubjects);
    } catch (Exception e) {
        e.printStackTrace();
        return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity<String>(json, headers, HttpStatus.OK);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) SubjectResponse(gov.nih.nci.ctd2.dashboard.api.SubjectResponse) ArrayList(java.util.ArrayList) ResponseEntity(org.springframework.http.ResponseEntity) SubjectResult(gov.nih.nci.ctd2.dashboard.util.SubjectResult) DashboardEntity(gov.nih.nci.ctd2.dashboard.model.DashboardEntity) JSONSerializer(flexjson.JSONSerializer) Transactional(org.springframework.transaction.annotation.Transactional) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

DashboardEntity (gov.nih.nci.ctd2.dashboard.model.DashboardEntity)3 JSONSerializer (flexjson.JSONSerializer)2 SubjectResponse (gov.nih.nci.ctd2.dashboard.api.SubjectResponse)2 ECOTerm (gov.nih.nci.ctd2.dashboard.model.ECOTerm)2 Subject (gov.nih.nci.ctd2.dashboard.model.Subject)2 Xref (gov.nih.nci.ctd2.dashboard.model.Xref)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 HttpHeaders (org.springframework.http.HttpHeaders)2 ResponseEntity (org.springframework.http.ResponseEntity)2 Transactional (org.springframework.transaction.annotation.Transactional)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 DashboardDao (gov.nih.nci.ctd2.dashboard.dao.DashboardDao)1 XrefImpl (gov.nih.nci.ctd2.dashboard.impl.XrefImpl)1 Gene (gov.nih.nci.ctd2.dashboard.model.Gene)1 ObservationTemplate (gov.nih.nci.ctd2.dashboard.model.ObservationTemplate)1 ObservedSubject (gov.nih.nci.ctd2.dashboard.model.ObservedSubject)1 ObservedSubjectRole (gov.nih.nci.ctd2.dashboard.model.ObservedSubjectRole)1 Synonym (gov.nih.nci.ctd2.dashboard.model.Synonym)1 ObservationURIsAndTiers (gov.nih.nci.ctd2.dashboard.util.ObservationURIsAndTiers)1