Search in sources :

Example 46 with NextProtException

use of org.nextprot.api.commons.exception.NextProtException in project nextprot-api by calipho-sib.

the class StatementAnnotationBuilder method buildAnnotationEvidences.

protected List<AnnotationEvidence> buildAnnotationEvidences(List<Statement> Statements) {
    // Ensures there is no repeated evidence!
    Set<AnnotationEvidence> evidencesSet = Statements.stream().map(s -> {
        AnnotationEvidence evidence = new AnnotationEvidence();
        // TODO to be checked with Amos and Lydie
        evidence.setResourceType("database");
        evidence.setResourceAssociationType("evidence");
        evidence.setQualityQualifier(s.getValue(StatementField.EVIDENCE_QUALITY));
        evidence.setResourceId(findPublicationId(s));
        AnnotationEvidenceProperty evidenceProperty = addPropertyIfPresent(s.getValue(StatementField.EVIDENCE_INTENSITY), "intensity");
        AnnotationEvidenceProperty expContextSubjectProteinOrigin = addPropertyIfPresent(s.getValue(StatementField.ANNOTATION_SUBJECT_SPECIES), "subject-protein-origin");
        AnnotationEvidenceProperty expContextObjectProteinOrigin = addPropertyIfPresent(s.getValue(StatementField.ANNOTATION_OBJECT_SPECIES), "object-protein-origin");
        // Set properties which are not null
        evidence.setProperties(Arrays.asList(evidenceProperty, expContextSubjectProteinOrigin, expContextObjectProteinOrigin).stream().filter(p -> p != null).collect(Collectors.toList()));
        String statementEvidenceCode = s.getValue(StatementField.EVIDENCE_CODE);
        evidence.setEvidenceCodeAC(statementEvidenceCode);
        evidence.setAssignedBy(s.getValue(StatementField.ASSIGNED_BY));
        evidence.setAssignmentMethod(s.getValue(StatementField.ASSIGMENT_METHOD));
        evidence.setResourceType(s.getValue(StatementField.RESOURCE_TYPE));
        evidence.setEvidenceCodeOntology("evidence-code-ontology-cv");
        evidence.setNegativeEvidence("true".equalsIgnoreCase(s.getValue(StatementField.IS_NEGATIVE)));
        if (statementEvidenceCode != null) {
            CvTerm term = terminologyService.findCvTermByAccession(statementEvidenceCode);
            if (term != null) {
                evidence.setEvidenceCodeName(term.getName());
            } else {
                throw new NextProtException("Not found " + statementEvidenceCode + " in the database");
            }
        }
        evidence.setNote(s.getValue(StatementField.EVIDENCE_NOTE));
        return evidence;
    }).collect(Collectors.toSet());
    // Ensures there is no repeated evidence!
    evidencesSet.forEach(e -> {
        long generatedEvidenceId = IdentifierOffset.EVIDENCE_ID_COUNTER_FOR_STATEMENTS.incrementAndGet();
        e.setEvidenceId(generatedEvidenceId);
    });
    List<AnnotationEvidence> evidencesFiltered = evidencesSet.stream().filter(e -> e.getResourceId() != -2).collect(Collectors.toList());
    if (evidencesFiltered.size() < evidencesSet.size()) {
        int total = evidencesSet.size();
        int removed = total - evidencesFiltered.size();
        LOGGER.debug("Removed " + removed + " evidence because no resource id from a total of " + total);
    }
    return new ArrayList<>(evidencesFiltered);
}
Also used : java.util(java.util) Supplier(com.google.common.base.Supplier) Annotation(org.nextprot.api.core.domain.annotation.Annotation) NextProtException(org.nextprot.api.commons.exception.NextProtException) StringUtils(org.nextprot.api.commons.utils.StringUtils) CvTerm(org.nextprot.api.core.domain.CvTerm) StatementField(org.nextprot.commons.statements.StatementField) Collectors(java.util.stream.Collectors) Statement(org.nextprot.commons.statements.Statement) BioType(org.nextprot.api.core.domain.BioObject.BioType) AnnotationEvidence(org.nextprot.api.core.domain.annotation.AnnotationEvidence) Logger(org.apache.log4j.Logger) Publication(org.nextprot.api.core.domain.Publication) AnnotationCategory(org.nextprot.api.commons.constants.AnnotationCategory) AnnotationUtils(org.nextprot.api.core.service.annotation.AnnotationUtils) AnnotationEvidenceProperty(org.nextprot.api.core.domain.annotation.AnnotationEvidenceProperty) IdentifierOffset(org.nextprot.api.commons.constants.IdentifierOffset) BioObject(org.nextprot.api.core.domain.BioObject) AnnotationVariant(org.nextprot.api.core.domain.annotation.AnnotationVariant) AnnotationEvidence(org.nextprot.api.core.domain.annotation.AnnotationEvidence) NextProtException(org.nextprot.api.commons.exception.NextProtException) CvTerm(org.nextprot.api.core.domain.CvTerm) AnnotationEvidenceProperty(org.nextprot.api.core.domain.annotation.AnnotationEvidenceProperty)

Example 47 with NextProtException

use of org.nextprot.api.commons.exception.NextProtException in project nextprot-api by calipho-sib.

the class StatementAnnotationBuilder method buildAnnotationList.

public List<T> buildAnnotationList(String isoformName, List<Statement> flatStatements) {
    List<T> annotations = new ArrayList<>();
    Map<String, List<Statement>> flatStatementsByAnnotationHash = flatStatements.stream().collect(Collectors.groupingBy(rs -> rs.getValue(StatementField.ANNOTATION_ID)));
    flatStatementsByAnnotationHash.forEach((key, statements) -> {
        T annotation = get();
        Statement firstStatement = statements.get(0);
        annotation.setAnnotationHash(firstStatement.getValue(StatementField.ANNOTATION_ID));
        // annotation.setAnnotationName(firstStatement.getValue(StatementField.ANNOTATION_NAME));
        AnnotationCategory category = AnnotationCategory.getDecamelizedAnnotationTypeName(StringUtils.camelToKebabCase(firstStatement.getValue(StatementField.ANNOTATION_CATEGORY)));
        annotation.setAnnotationCategory(category);
        if (category.equals(AnnotationCategory.VARIANT) || category.equals(AnnotationCategory.MUTAGENESIS)) {
            setVariantAttributes(annotation, firstStatement);
        }
        setIsoformTargeting(annotation, firstStatement);
        setIsoformName(annotation, isoformName);
        annotation.setDescription(firstStatement.getValue(StatementField.ANNOT_DESCRIPTION));
        String cvTermAccession = firstStatement.getValue(StatementField.ANNOT_CV_TERM_ACCESSION);
        // Set the evidences if not Mammalian phenotype or Protein Property https://issues.isb-sib.ch/browse/BIOEDITOR-466
        if (!ANNOT_CATEGORIES_WITHOUT_EVIDENCES.contains(category)) {
            annotation.setEvidences(buildAnnotationEvidences(statements));
            // TODO Remove this when you are able to do XREFs
            if (((annotation.getEvidences() == null) || ((annotation.getEvidences().isEmpty()))) && (category.equals(AnnotationCategory.VARIANT) || category.equals(AnnotationCategory.MUTAGENESIS))) {
                // All variants from BED are GOLD, and this is a special case when we don't have evidences for VDs.
                annotation.setQualityQualifier("GOLD");
            } else {
                annotation.setQualityQualifier(AnnotationUtils.computeAnnotationQualityBasedOnEvidences(annotation.getEvidences()).name());
            }
        } else {
            // Case of Protein propert and mammalian phenotypes
            annotation.setEvidences(new ArrayList<AnnotationEvidence>());
            boolean foundGold = statements.stream().anyMatch(s -> s.getValue(StatementField.EVIDENCE_QUALITY).equalsIgnoreCase("GOLD"));
            if (foundGold) {
                annotation.setQualityQualifier("GOLD");
            } else {
                annotation.setQualityQualifier("SILVER");
            }
        }
        if (cvTermAccession != null && !cvTermAccession.isEmpty()) {
            annotation.setCvTermAccessionCode(cvTermAccession);
            CvTerm cvTerm = terminologyService.findCvTermByAccession(cvTermAccession);
            if (cvTerm != null) {
                annotation.setCvTermName(cvTerm.getName());
                annotation.setCvApiName(cvTerm.getOntology());
                annotation.setCvTermDescription(cvTerm.getDescription());
                if (category.equals(AnnotationCategory.PROTEIN_PROPERTY)) {
                    // according to https://issues.isb-sib.ch/browse/BIOEDITOR-466
                    annotation.setDescription(cvTerm.getDescription());
                } else if (category.equals(AnnotationCategory.MAMMALIAN_PHENOTYPE)) {
                    annotation.setDescription("Relative to modification-effect annotations");
                }
            } else {
                LOGGER.error("cv term was expected to be found " + cvTermAccession);
                annotation.setCvTermName(firstStatement.getValue(StatementField.ANNOT_CV_TERM_NAME));
                annotation.setCvApiName(firstStatement.getValue(StatementField.ANNOT_CV_TERM_TERMINOLOGY));
            }
        }
        annotation.setAnnotationHash(firstStatement.getValue(StatementField.ANNOTATION_ID));
        annotation.setAnnotationName(firstStatement.getValue(StatementField.ANNOTATION_NAME));
        // Check this with PAM (does it need to be a human readable stuff)
        // Does it need a name?
        annotation.setUniqueName(firstStatement.getValue(StatementField.ANNOTATION_ID));
        String bioObjectAnnotationHash = firstStatement.getValue(StatementField.OBJECT_ANNOTATION_IDS);
        String bioObjectAccession = firstStatement.getValue(StatementField.BIOLOGICAL_OBJECT_ACCESSION);
        String bot = firstStatement.getValue(StatementField.BIOLOGICAL_OBJECT_TYPE);
        if ((bioObjectAnnotationHash != null) && (bioObjectAnnotationHash.length() > 0) || (bioObjectAccession != null && (bioObjectAccession.length() > 0))) {
            BioObject bioObject;
            if (AnnotationCategory.BINARY_INTERACTION.equals(annotation.getAPICategory())) {
                if (bioObjectAccession.startsWith("NX_") && BioType.PROTEIN.name().equalsIgnoreCase(bot)) {
                    // note that if we handle BioType.PROTEIN_ISOFORM in the future, we should
                    // add the property isoformName as well, see how it's done in BinaryInteraction2Annotation.newBioObject()
                    bioObject = BioObject.internal(BioType.PROTEIN);
                    // // TODO: REMOVE THIS HACK WHEN ISSUE https://issues.isb-sib.ch/browse/NEXTPROT-1513 will be fixed
                    // // NX_P62158 was split in 3 accessions: NX_P0DP23/CALM1, NX_P0DP24/CALM2 and NX_P0DP25/CALM3
                    // // BEGIN DIRTY HACK
                    /*
                        if (bioObjectAccession.equals("NX_P62158")) {
                            switch (isoformName) {
                                case "NX_P35499":
                                    bioObjectAccession = "NX_P0DP23";
                                    break;
                                case "NX_Q99250":
                                    bioObjectAccession = "NX_P0DP23";
                                    break;
                                case "NX_Q9UQD0":
                                    bioObjectAccession = "NX_P0DP23";
                                    break;
                                case "NX_Q9Y5Y9":
                                    bioObjectAccession = "NX_P0DP23";
                                    break;
                            }
                        }
                        */
                    // /// END OF HACK
                    bioObject.setAccession(bioObjectAccession);
                    bioObject.putPropertyNameValue("geneName", firstStatement.getValue(StatementField.BIOLOGICAL_OBJECT_NAME));
                    String proteinName = mainNamesService.findIsoformOrEntryMainName(bioObjectAccession).orElseThrow(() -> new NextProtException("Cannot create a binary interaction with " + isoformName + ": unknown protein accession " + bioObject.getAccession())).getName();
                    bioObject.putPropertyNameValue("proteinName", proteinName);
                    bioObject.putPropertyNameValue("url", "https://www.nextprot.org/entry/" + bioObjectAccession + "/interactions");
                } else {
                    throw new NextProtException("Binary Interaction only expects to be a nextprot entry NX_ and found " + bioObjectAccession + " with type " + bot);
                }
            } else if (AnnotationCategory.PHENOTYPIC_VARIATION.equals(annotation.getAPICategory())) {
                bioObject = BioObject.internal(BioType.ENTRY_ANNOTATION);
                bioObject.setAnnotationHash(bioObjectAnnotationHash);
            } else {
                throw new NextProtException("Category not expected for bioobject " + annotation.getAPICategory());
            }
            annotation.setBioObject(bioObject);
        }
        annotations.add(annotation);
    });
    return annotations;
}
Also used : java.util(java.util) Supplier(com.google.common.base.Supplier) Annotation(org.nextprot.api.core.domain.annotation.Annotation) NextProtException(org.nextprot.api.commons.exception.NextProtException) StringUtils(org.nextprot.api.commons.utils.StringUtils) CvTerm(org.nextprot.api.core.domain.CvTerm) StatementField(org.nextprot.commons.statements.StatementField) Collectors(java.util.stream.Collectors) Statement(org.nextprot.commons.statements.Statement) BioType(org.nextprot.api.core.domain.BioObject.BioType) AnnotationEvidence(org.nextprot.api.core.domain.annotation.AnnotationEvidence) Logger(org.apache.log4j.Logger) Publication(org.nextprot.api.core.domain.Publication) AnnotationCategory(org.nextprot.api.commons.constants.AnnotationCategory) AnnotationUtils(org.nextprot.api.core.service.annotation.AnnotationUtils) AnnotationEvidenceProperty(org.nextprot.api.core.domain.annotation.AnnotationEvidenceProperty) IdentifierOffset(org.nextprot.api.commons.constants.IdentifierOffset) BioObject(org.nextprot.api.core.domain.BioObject) AnnotationVariant(org.nextprot.api.core.domain.annotation.AnnotationVariant) CvTerm(org.nextprot.api.core.domain.CvTerm) Statement(org.nextprot.commons.statements.Statement) AnnotationCategory(org.nextprot.api.commons.constants.AnnotationCategory) AnnotationEvidence(org.nextprot.api.core.domain.annotation.AnnotationEvidence) NextProtException(org.nextprot.api.commons.exception.NextProtException) BioObject(org.nextprot.api.core.domain.BioObject)

Example 48 with NextProtException

use of org.nextprot.api.commons.exception.NextProtException in project nextprot-api by calipho-sib.

the class AnnotationUpdater method updateDestAnnotationHash.

@Override
protected void updateDestAnnotationHash(Annotation dest, Annotation source) {
    String annotationHash = source.getAnnotationHash();
    if (annotationHash == null || annotationHash.isEmpty())
        throw new NextProtException("annotation hash was not computed for source " + source.getUniqueName());
    dest.setAnnotationHash(annotationHash);
}
Also used : NextProtException(org.nextprot.api.commons.exception.NextProtException)

Example 49 with NextProtException

use of org.nextprot.api.commons.exception.NextProtException in project nextprot-api by calipho-sib.

the class EntryPartExporterImpl method setExperimentalContextRowValues.

private void setExperimentalContextRowValues(Row row, Entry entry, AnnotationEvidence evidence) {
    ExperimentalContext ec = entry.getExperimentalContext(evidence.getExperimentalContextId()).orElseThrow(() -> new NextProtException("missing experimental context for " + evidence.getEvidenceCodeAC()));
    if (ec.getDevelopmentalStage() != null) {
        setRowValue(row, STAGE_ACCESSION, ec.getDevelopmentalStageAC());
        setRowValue(row, STAGE_NAME, ec.getDevelopmentalStage().getName());
    }
    if (ec.getCellLine() != null) {
        setRowValue(row, CELL_LINE_ACCESSION, ec.getCellLineAC());
        setRowValue(row, CELL_LINE_NAME, ec.getCellLine().getName());
    }
    if (ec.getDisease() != null) {
        setRowValue(row, DISEASE_ACCESSION, ec.getDiseaseAC());
        setRowValue(row, DISEASE_NAME, ec.getDisease().getName());
    }
    if (ec.getOrganelle() != null) {
        setRowValue(row, ORGANELLE_ACCESSION, ec.getOrganelleAC());
        setRowValue(row, ORGANELLE_NAME, ec.getOrganelle().getName());
    }
}
Also used : NextProtException(org.nextprot.api.commons.exception.NextProtException) ExperimentalContext(org.nextprot.api.core.domain.ExperimentalContext)

Example 50 with NextProtException

use of org.nextprot.api.commons.exception.NextProtException in project nextprot-api by calipho-sib.

the class ExportUtils method printOutput.

public static void printOutput(Queue<Future<File>> futuresQueue, HttpServletResponse response) {
    boolean userHasCanceled = false;
    OutputStream out = null;
    Future<File> currentFuture = null;
    while (!futuresQueue.isEmpty()) {
        currentFuture = futuresQueue.remove();
        InputStream currentInputStream = null;
        try {
            if (out == null) {
                out = response.getOutputStream();
            }
            File f = currentFuture.get();
            currentInputStream = new FileInputStream(f);
            LOGGER.info("Content of " + f.getName() + " is being streamed to the client");
            int read = 0;
            byte[] bytes = new byte[BYTES_DOWNLOAD];
            while ((read = currentInputStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            currentInputStream.close();
            out.flush();
        } catch (EOFException e) {
            LOGGER.info("User has cancelled the request");
            cancelFutures(currentFuture, futuresQueue);
            userHasCanceled = true;
        } catch (Exception e) {
            LOGGER.error("Something when wrong when streaming the data" + e.getMessage());
            e.printStackTrace();
            cancelFutures(currentFuture, futuresQueue);
            closeStream(out);
            throw new NextProtException(e.getClass().getName() + ": " + e.getMessage());
        } finally {
            closeStream(currentInputStream);
        }
    }
    // No need to close stream when the user has canceled because it is already closed
    if (!userHasCanceled) {
        closeStream(out);
    }
}
Also used : NextProtException(org.nextprot.api.commons.exception.NextProtException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) EOFException(java.io.EOFException) File(java.io.File) FileInputStream(java.io.FileInputStream) NextProtException(org.nextprot.api.commons.exception.NextProtException) IOException(java.io.IOException) EOFException(java.io.EOFException)

Aggregations

NextProtException (org.nextprot.api.commons.exception.NextProtException)68 IOException (java.io.IOException)30 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)9 ApiMethod (org.jsondoc.core.annotation.ApiMethod)8 OutputStream (java.io.OutputStream)7 NamedParameterJdbcTemplate (org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate)7 Cacheable (org.springframework.cache.annotation.Cacheable)6 Isoform (org.nextprot.api.core.domain.Isoform)5 SolrServerException (org.apache.solr.client.solrj.SolrServerException)4 Annotation (org.nextprot.api.core.domain.annotation.Annotation)4 NextprotMediaType (org.nextprot.api.core.service.export.format.NextprotMediaType)4 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)4 java.util (java.util)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 CvTerm (org.nextprot.api.core.domain.CvTerm)3 AnnotationVariant (org.nextprot.api.core.domain.annotation.AnnotationVariant)3 Query (org.nextprot.api.solr.Query)3 SearchResult (org.nextprot.api.solr.SearchResult)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2