Search in sources :

Example 1 with EntityNotFoundException

use of ubic.gemma.core.association.phenotype.PhenotypeExceptions.EntityNotFoundException in project Gemma by PavlidisLab.

the class PhenotypeAssociationManagerServiceImpl method findChildrenForEachPhenotype.

/**
 * Map query phenotypes given to the set of possible children phenotypes in the database so for example if the user
 * search for : cerebral palsy (parent phenotype), this will return all children associated with it that are also
 * present in the database(all children of phenotype) : Map<String(parent phenotype), Set<String>(all children of
 * phenotype)>
 *
 * @param usedPhenotypes       the URIs of all phenotypes actually used in the database.
 * @param phenotypesValuesUris URIs
 * @return map of terms to their children. The term itself is included.
 */
private Map<String, Set<String>> findChildrenForEachPhenotype(Collection<String> phenotypesValuesUris, Collection<String> usedPhenotypes) {
    // root corresponds to one value found in phenotypesValuesUri
    // root ---> root+children phenotypes
    Map<String, Set<String>> parentPheno = new HashMap<>();
    // determine all children terms for each other phenotypes
    for (String phenoRoot : phenotypesValuesUris) {
        if (phenoRoot.isEmpty()) {
            continue;
        }
        OntologyTerm ontologyTermFound;
        try {
            ontologyTermFound = this.ontologyHelper.findOntologyTermByUri(phenoRoot);
            if (ontologyTermFound == null)
                continue;
        } catch (EntityNotFoundException e) {
            if (!ontologyHelper.areOntologiesAllLoaded()) {
                throw new RuntimeException(PhenotypeAssociationManagerServiceImpl.ERROR_MSG_ONTOLOGIES_NOT_LOADED + " ( " + e.getMessage() + " )");
            } else {
                throw e;
            }
        }
        Collection<OntologyTerm> ontologyChildrenFound = ontologyTermFound.getChildren(false);
        Set<String> parentChildren = new HashSet<>();
        parentChildren.add(phenoRoot);
        for (OntologyTerm ot : ontologyChildrenFound) {
            if (usedPhenotypes.contains(ot.getUri())) {
                parentChildren.add(ot.getUri());
            }
        }
        parentPheno.put(phenoRoot, parentChildren);
    }
    return parentPheno;
}
Also used : OntologyTerm(ubic.basecode.ontology.model.OntologyTerm) EntityNotFoundException(ubic.gemma.core.association.phenotype.PhenotypeExceptions.EntityNotFoundException)

Example 2 with EntityNotFoundException

use of ubic.gemma.core.association.phenotype.PhenotypeExceptions.EntityNotFoundException in project Gemma by PavlidisLab.

the class EvidenceImporterCLI method doWork.

@Override
protected Exception doWork(String[] args) {
    Exception err = this.processCommandLine(args);
    if (err != null)
        return err;
    try {
        this.createWriteFolder();
        FileWriter fstream = new FileWriter(EvidenceImporterAbstractCLI.WRITE_FOLDER + File.separator + "EvidenceImporter.log");
        this.logFileWriter = new BufferedWriter(fstream);
        AbstractCLI.log.info("File: " + this.inputFile);
        AbstractCLI.log.info("Create in Database: " + this.createInDatabase);
        this.br = new BufferedReader(new FileReader(this.inputFile));
        String typeOfEvidence = this.findTypeOfEvidence();
        // take the file received and create the real objects from it
        Collection<EvidenceValueObject> evidenceValueObjects = this.file2Objects(typeOfEvidence);
        // make sure all pubmed exists
        if (!this.errorMessage.isEmpty()) {
            System.out.println(errorMessage);
            this.writeAllExceptions();
            this.logFileWriter.close();
            throw new Exception("check logs");
        }
        if (!this.warningMessage.isEmpty()) {
            AbstractCLI.log.info(this.warningMessage);
        }
        if (this.createInDatabase) {
            int i = 1;
            // for each evidence creates it in Neurocarta
            for (EvidenceValueObject e : evidenceValueObjects) {
                try {
                    // create the evidence in neurocarta
                    this.phenotypeAssociationService.makeEvidence(e);
                } catch (EntityNotFoundException ex) {
                    this.writeError("went into the exception");
                    // if a pubmed id was not found dont stop all processes and write to logs
                    if (ex.getMessage().contains("pubmed id")) {
                        this.writeError(ex.getMessage());
                    } else {
                        throw ex;
                    }
                } catch (RuntimeException ex1) {
                    AbstractCLI.log.error(ExceptionUtils.getStackTrace(ex1));
                    System.out.println(ex1.getMessage());
                    this.writeError("RuntimeException trying to make evidence again 1: " + e.getGeneNCBI());
                    System.out.println(e.toString());
                    try {
                        // something is wrong with the pubmed api, wait it out
                        AbstractCLI.log.info("tring again 1: waiting 10 sec");
                        Thread.sleep(10000);
                        // create the evidence in neurocarta
                        this.phenotypeAssociationService.makeEvidence(e);
                    } catch (RuntimeException ex2) {
                        AbstractCLI.log.error(ExceptionUtils.getStackTrace(ex2));
                        this.writeError("RuntimeException trying to make evidence again 2: " + e.getGeneNCBI());
                        // something is wrong with the pubmed api, wait it out more 5000 seconds, this often run at
                        // night so make it wait doesn't matter
                        AbstractCLI.log.info("tring again 2: waiting 100 sec");
                        Thread.sleep(100000);
                        try {
                            this.phenotypeAssociationService.makeEvidence(e);
                        } catch (RuntimeException ex3) {
                            AbstractCLI.log.error(ExceptionUtils.getStackTrace(ex3));
                            this.writeError("RuntimeException trying to make evidence again 3: " + e.getGeneNCBI());
                            // something is wrong with the pubmed api, wait it out more 5000 seconds, this often run
                            // at night so make it wait doesn't matter
                            AbstractCLI.log.info("tring again 3: waiting 1000 sec");
                            Thread.sleep(1000000);
                            try {
                                this.phenotypeAssociationService.makeEvidence(e);
                            } catch (RuntimeException ex4) {
                                AbstractCLI.log.error(ExceptionUtils.getStackTrace(ex4));
                                this.writeError("RuntimeException trying to make evidence again 4: " + e.getGeneNCBI());
                                // something is wrong with the pubmed api, wait it out more 4 hours, this often
                                // run at night so make it wait doesn't matter
                                AbstractCLI.log.info("tring again 4: waiting 15000 sec");
                                Thread.sleep(15000000);
                                this.phenotypeAssociationService.makeEvidence(e);
                            }
                        }
                    }
                }
                AbstractCLI.log.info("created evidence " + i++);
            }
        }
        this.logFileWriter.close();
        AbstractCLI.log.info("Import of evidence is finish");
    // when we import a file in production we keep a copy of the imported file and keep track of when the file
    // was imported in a log file
    // createImportLog( evidenceValueObjects.iterator().next() );
    } catch (Exception e) {
        return e;
    }
    System.exit(-1);
    return null;
}
Also used : EntityNotFoundException(ubic.gemma.core.association.phenotype.PhenotypeExceptions.EntityNotFoundException) EntityNotFoundException(ubic.gemma.core.association.phenotype.PhenotypeExceptions.EntityNotFoundException)

Example 3 with EntityNotFoundException

use of ubic.gemma.core.association.phenotype.PhenotypeExceptions.EntityNotFoundException in project Gemma by PavlidisLab.

the class EvidenceImporterCLI method verifyGeneIdExist.

/**
 * check that all gene exists in Gemma
 */
private Gene verifyGeneIdExist(String geneId, String geneName) throws IOException {
    System.out.println("Problem: gene id " + geneId + " gene name: " + geneName);
    Gene g = this.geneService.findByNCBIId(new Integer(geneId));
    // we found a gene
    if (g != null) {
        if (!g.getOfficialSymbol().equalsIgnoreCase(geneName)) {
            this.writeWarning("Different Gene name found: file=" + geneName + "      Gene name in Gemma=" + g.getOfficialSymbol());
        }
        if (!g.getTaxon().getCommonName().equals("human") && !g.getTaxon().getCommonName().equals("mouse") && !g.getTaxon().getCommonName().equals("rat") && !g.getTaxon().getCommonName().equals("fly") && !g.getTaxon().getCommonName().equals("worm") && !g.getTaxon().getCommonName().equals("zebrafish")) {
            String speciesFound = g.getTaxon().getCommonName();
            // lets try to map it to a human taxon using its symbol
            g = this.geneService.findByOfficialSymbol(geneName, taxonService.findByCommonName("human"));
            if (g != null) {
                this.writeWarning("We found species: " + speciesFound + " on geneId: " + geneId + " and changed to it to the human symbol: " + geneName);
            } else {
                throw new EntityNotFoundException("The geneId: " + geneId + " using species: " + speciesFound + " exist but couldnt be map to its human symbol using: " + geneName + ", this evidence wont be imported");
            }
        }
    } else {
        // lets try to map it to a human taxon using its symbol
        g = this.geneService.findByOfficialSymbol(geneName, taxonService.findByCommonName("human"));
        if (g != null) {
            this.writeWarning("We didnt found the geneId: " + geneId + " and changed it to the human symbol: " + geneName);
        } else {
            throw new EntityNotFoundException("The geneId:" + geneId + " symbol: " + geneName + " was not found in Gemma, this evidence wont be imported");
        }
    }
    return g;
}
Also used : Gene(ubic.gemma.model.genome.Gene) EntityNotFoundException(ubic.gemma.core.association.phenotype.PhenotypeExceptions.EntityNotFoundException)

Example 4 with EntityNotFoundException

use of ubic.gemma.core.association.phenotype.PhenotypeExceptions.EntityNotFoundException in project Gemma by PavlidisLab.

the class PhenotypeAssociationManagerServiceImpl method findAllPhenotypesByTree.

/**
 * Using all the phenotypes in the database, builds a tree structure using the Ontology
 *
 * @param isAdmin,                can see everything
 * @param noElectronicAnnotation, if true don't include evidence code IEA
 * @param evidenceFilter          evidence filter
 * @return Collection<TreeCharacteristicValueObject> list of all phenotypes in gemma represented as trees
 */
private Collection<TreeCharacteristicValueObject> findAllPhenotypesByTree(EvidenceFilter evidenceFilter, boolean isAdmin, boolean noElectronicAnnotation) {
    StopWatch sw = new StopWatch();
    sw.start();
    Taxon taxon = null;
    boolean showOnlyEditable = false;
    Collection<Long> externalDatabaseIds = null;
    if (evidenceFilter != null) {
        taxon = this.checkAndGetTaxon(evidenceFilter);
        showOnlyEditable = evidenceFilter.isShowOnlyEditable();
        externalDatabaseIds = evidenceFilter.getExternalDatabaseIds();
    }
    Map<String, Set<Integer>> publicPhenotypesGenesAssociations = new HashMap<>();
    Map<String, Set<Integer>> privatePhenotypesGenesAssociations = new HashMap<>();
    Set<String> allPhenotypesGenesAssociations = new HashSet<>();
    boolean isUserLoggedIn = SecurityUtil.isUserLoggedIn();
    String userName;
    Collection<String> groups;
    PhenotypeAssociationManagerServiceImpl.log.info(// note we can't cache this because varies per user.
    "Starting loading phenotype tree");
    if (isUserLoggedIn) {
        userName = this.userManager.getCurrentUsername();
        // groups the user belong to
        groups = this.userManager.findGroupsForUser(userName);
        // show only my annotation was chosen
        if (showOnlyEditable) {
            // show public owned by the user
            publicPhenotypesGenesAssociations = this.phenoAssocService.findPublicPhenotypesGenesAssociations(taxon, null, userName, groups, true, externalDatabaseIds, noElectronicAnnotation);
            // show all private owned by the user or shared by a group
            privatePhenotypesGenesAssociations = this.phenoAssocService.findPrivatePhenotypesGenesAssociations(taxon, null, userName, groups, true, externalDatabaseIds, noElectronicAnnotation);
        } else {
            // logged in, but not filtered. all public evidences
            publicPhenotypesGenesAssociations = this.phenoAssocService.findPublicPhenotypesGenesAssociations(taxon, null, null, groups, false, externalDatabaseIds, noElectronicAnnotation);
            if (isAdmin) {
                // show all private since admin
                privatePhenotypesGenesAssociations = this.phenoAssocService.findPrivatePhenotypesGenesAssociations(taxon, null, null, null, false, externalDatabaseIds, noElectronicAnnotation);
            } else {
                // show all private owned by the user or shared by a group
                privatePhenotypesGenesAssociations = this.phenoAssocService.findPrivatePhenotypesGenesAssociations(taxon, null, userName, groups, false, externalDatabaseIds, noElectronicAnnotation);
            }
        }
    } else if (!showOnlyEditable) {
        // anonymous user
        publicPhenotypesGenesAssociations = this.phenoAssocService.findPublicPhenotypesGenesAssociations(taxon, null, null, null, false, externalDatabaseIds, noElectronicAnnotation);
    }
    allPhenotypesGenesAssociations.addAll(privatePhenotypesGenesAssociations.keySet());
    allPhenotypesGenesAssociations.addAll(publicPhenotypesGenesAssociations.keySet());
    // map to help the placement of elements in the tree, used to find quickly the position to add subtrees
    Map<String, TreeCharacteristicValueObject> phenotypeFoundInTree = new HashMap<>();
    // represents each phenotype and children found in the Ontology, TreeSet used to order trees [why do we need a
    // TreeSet? Can't we just sort at the end?]
    TreeSet<TreeCharacteristicValueObject> treesPhenotypes = new TreeSet<>();
    // creates the tree structure
    for (String valueUri : allPhenotypesGenesAssociations) {
        // don't create the tree if it is already present in an other [another what?]
        if (phenotypeFoundInTree.containsKey(valueUri)) {
            // flag the node as phenotype found in database [when does this happen? It seems useless, since we don't
            // use this]
            phenotypeFoundInTree.get(valueUri).setDbPhenotype(true);
        } else {
            try {
                // find the ontology term using the valueURI
                OntologyTerm ontologyTerm = this.ontologyHelper.findOntologyTermByUri(valueUri);
                // we don't show obsolete terms
                if (ontologyTerm.isTermObsolete()) {
                    PhenotypeAssociationManagerServiceImpl.log.warn("A valueUri found in the database is obsolete: " + valueUri);
                } else {
                    // transform an OntologyTerm and his children to a TreeCharacteristicValueObject
                    TreeCharacteristicValueObject treeCharacteristicValueObject = TreeCharacteristicValueObject.ontology2TreeCharacteristicValueObjects(ontologyTerm, phenotypeFoundInTree);
                    // set flag that this node represents a phenotype used in the database
                    treeCharacteristicValueObject.setDbPhenotype(true);
                    // add tree to the phenotypes found in ontology
                    phenotypeFoundInTree.put(ontologyTerm.getUri(), treeCharacteristicValueObject);
                    if (!treesPhenotypes.add(treeCharacteristicValueObject)) {
                        throw new IllegalStateException("Add failed for " + ontologyTerm);
                    }
                    if (PhenotypeAssociationManagerServiceImpl.log.isDebugEnabled())
                        PhenotypeAssociationManagerServiceImpl.log.debug("Added: " + ontologyTerm);
                }
            } catch (EntityNotFoundException entityNotFoundException) {
                if (this.ontologyHelper.areOntologiesAllLoaded()) {
                    if (PhenotypeAssociationManagerServiceImpl.log.isDebugEnabled())
                        PhenotypeAssociationManagerServiceImpl.log.debug(// this ends up being pretty verbose.
                        "A valueUri in the database was not found in the ontology; DB out of date?; valueUri: " + valueUri);
                } else {
                    throw new RuntimeException(PhenotypeAssociationManagerServiceImpl.ERROR_MSG_ONTOLOGIES_NOT_LOADED + " ( " + entityNotFoundException.getMessage() + " )");
                }
            }
        }
    }
    TreeSet<TreeCharacteristicValueObject> finalTreesWithRoots = new TreeSet<>();
    for (TreeCharacteristicValueObject tc : treesPhenotypes) {
        this.findParentRoot(tc, finalTreesWithRoots, phenotypeFoundInTree);
    }
    treesPhenotypes = finalTreesWithRoots;
    // set the public count
    for (TreeCharacteristicValueObject tc : treesPhenotypes) {
        tc.countPublicGeneForEachNode(publicPhenotypesGenesAssociations);
        tc.countPrivateGeneForEachNode(privatePhenotypesGenesAssociations);
        tc.removeUnusedPhenotypes();
    }
    PhenotypeAssociationManagerServiceImpl.log.info("Done total time=" + sw.getTime() + "ms");
    return treesPhenotypes;
}
Also used : Taxon(ubic.gemma.model.genome.Taxon) OntologyTerm(ubic.basecode.ontology.model.OntologyTerm) EntityNotFoundException(ubic.gemma.core.association.phenotype.PhenotypeExceptions.EntityNotFoundException) StopWatch(org.apache.commons.lang3.time.StopWatch)

Example 5 with EntityNotFoundException

use of ubic.gemma.core.association.phenotype.PhenotypeExceptions.EntityNotFoundException in project Gemma by PavlidisLab.

the class PhenotypeAssoOntologyHelperImpl method valueUri2Characteristic.

@Override
public Characteristic valueUri2Characteristic(String valueUri) {
    try {
        OntologyTerm o = findOntologyTermByUri(valueUri);
        if (o == null)
            return null;
        VocabCharacteristic myPhenotype = VocabCharacteristic.Factory.newInstance();
        myPhenotype.setValueUri(o.getUri());
        myPhenotype.setValue(o.getLabel());
        myPhenotype.setCategory(PhenotypeAssociationConstants.PHENOTYPE);
        myPhenotype.setCategoryUri(PhenotypeAssociationConstants.PHENOTYPE_CATEGORY_URI);
        return myPhenotype;
    } catch (EntityNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : VocabCharacteristic(ubic.gemma.model.common.description.VocabCharacteristic) OntologyTerm(ubic.basecode.ontology.model.OntologyTerm) EntityNotFoundException(ubic.gemma.core.association.phenotype.PhenotypeExceptions.EntityNotFoundException)

Aggregations

EntityNotFoundException (ubic.gemma.core.association.phenotype.PhenotypeExceptions.EntityNotFoundException)5 OntologyTerm (ubic.basecode.ontology.model.OntologyTerm)3 StopWatch (org.apache.commons.lang3.time.StopWatch)1 VocabCharacteristic (ubic.gemma.model.common.description.VocabCharacteristic)1 Gene (ubic.gemma.model.genome.Gene)1 Taxon (ubic.gemma.model.genome.Taxon)1