Search in sources :

Example 1 with StringUtils.strip

use of org.apache.commons.lang3.StringUtils.strip in project Gemma by PavlidisLab.

the class GeneSetSearchImpl method findByPhenotypeName.

@Override
public Collection<GeneSetValueObject> findByPhenotypeName(String phenotypeQuery, Taxon taxon) {
    StopWatch timer = new StopWatch();
    timer.start();
    Collection<CharacteristicValueObject> phenotypes = phenotypeAssociationManagerService.searchOntologyForPhenotypes(StringUtils.strip(phenotypeQuery), null);
    Collection<GeneSetValueObject> results = new HashSet<>();
    if (phenotypes.isEmpty()) {
        return results;
    }
    if (timer.getTime() > 200) {
        GeneSetSearchImpl.log.info("Find phenotypes: " + timer.getTime() + "ms");
    }
    GeneSetSearchImpl.log.debug(" Converting CharacteristicValueObjects collection(size:" + phenotypes.size() + ") into GeneSets for  phenotype query " + phenotypeQuery);
    Map<String, CharacteristicValueObject> uris = new HashMap<>();
    for (CharacteristicValueObject cvo : phenotypes) {
        uris.put(cvo.getValueUri(), cvo);
    }
    Map<String, Collection<? extends GeneValueObject>> genes = phenotypeAssociationManagerService.findCandidateGenesForEach(uris.keySet(), taxon);
    if (timer.getTime() > 500) {
        GeneSetSearchImpl.log.info("Find phenotype genes done at " + timer.getTime() + "ms");
    }
    for (String uri : genes.keySet()) {
        Collection<? extends GeneValueObject> gvos = genes.get(uri);
        if (gvos.isEmpty())
            continue;
        Collection<Long> geneIds = EntityUtils.getIds(gvos);
        GeneSetValueObject transientGeneSet = new GeneSetValueObject();
        transientGeneSet.setName(this.uri2phenoID(uris.get(uri)));
        transientGeneSet.setDescription(uris.get(uri).getValue());
        transientGeneSet.setGeneIds(geneIds);
        transientGeneSet.setTaxonId(gvos.iterator().next().getTaxonId());
        transientGeneSet.setTaxonName(gvos.iterator().next().getTaxonCommonName());
        results.add(transientGeneSet);
    }
    if (timer.getTime() > 1000) {
        GeneSetSearchImpl.log.info("Loaded " + phenotypes.size() + " phenotype gene sets for query " + phenotypeQuery + " in " + timer.getTime() + "ms");
    }
    return results;
}
Also used : CharacteristicValueObject(ubic.gemma.model.genome.gene.phenotype.valueObject.CharacteristicValueObject) HashMap(java.util.HashMap) StopWatch(org.apache.commons.lang3.time.StopWatch) GeneValueObject(ubic.gemma.model.genome.gene.GeneValueObject) Collection(java.util.Collection) GeneSetValueObject(ubic.gemma.model.genome.gene.GeneSetValueObject) HashSet(java.util.HashSet)

Example 2 with StringUtils.strip

use of org.apache.commons.lang3.StringUtils.strip in project Gemma by PavlidisLab.

the class SearchServiceImpl method characteristicSearchWithChildren.

/**
 * Search for the query in ontologies, including items that are associated with children of matching query terms.
 * That is, 'brain' should return entities tagged as 'hippocampus'. This method will return results only up to
 * MAX_CHARACTERISTIC_SEARCH_RESULTS. It can handle AND in searches, so Parkinson's AND neuron finds items tagged
 * with both of those terms. The use of OR is handled by the caller.
 *
 * @param classes Classes of characteristic-bound entities. For example, to get matching characteristics of
 *                ExpressionExperiments, pass ExpressionExperiments.class in this collection parameter.
 * @return SearchResults of CharacteristicObjects. Typically to be useful one needs to retrieve the 'parents'
 * (entities which have been 'tagged' with the term) of those Characteristics
 */
private Collection<SearchResult> characteristicSearchWithChildren(Collection<Class<?>> classes, String query) {
    StopWatch timer = this.startTiming();
    /*
         * The tricky part here is if the user has entered a boolean query. If they put in Parkinson's disease AND neuron,
         * then we want to eventually return entities that are associated with both. We don't expect to find single
         * characteristics that match both.
         *
         * But if they put in Parkinson's disease we don't want to do two queries.
         */
    List<String> subparts = Arrays.asList(query.split(" AND "));
    // we would have to first deal with the separate queries, and then apply the logic.
    Collection<SearchResult> allResults = new HashSet<>();
    SearchServiceImpl.log.info("Starting characteristic search: " + query + " for type=" + StringUtils.join(classes, ","));
    for (String rawTerm : subparts) {
        String trimmed = StringUtils.strip(rawTerm);
        if (StringUtils.isBlank(trimmed)) {
            continue;
        }
        Collection<SearchResult> subqueryResults = this.characteristicSearchTerm(classes, trimmed);
        if (allResults.isEmpty()) {
            allResults.addAll(subqueryResults);
        } else {
            // this is our Intersection operation.
            allResults.retainAll(subqueryResults);
            // aggregate the highlighted text.
            Map<SearchResult, String> highlights = new HashMap<>();
            for (SearchResult sqr : subqueryResults) {
                highlights.put(sqr, sqr.getHighlightedText());
            }
            for (SearchResult ar : allResults) {
                String k = highlights.get(ar);
                if (StringUtils.isNotBlank(k)) {
                    String highlightedText = ar.getHighlightedText();
                    if (StringUtils.isBlank(highlightedText)) {
                        ar.setHighlightedText(k);
                    } else {
                        ar.setHighlightedText(highlightedText + "," + k);
                    }
                }
            }
        }
        if (timer.getTime() > 1000) {
            SearchServiceImpl.log.info("Characteristic search for '" + rawTerm + "': " + allResults.size() + " hits retained so far; " + timer.getTime() + "ms");
            timer.reset();
            timer.start();
        }
    }
    return allResults;
}
Also used : StopWatch(org.apache.commons.lang3.time.StopWatch)

Example 3 with StringUtils.strip

use of org.apache.commons.lang3.StringUtils.strip in project Gemma by PavlidisLab.

the class CharacteristicUpdateTaskImpl method doUpdate.

private TaskResult doUpdate() {
    Collection<AnnotationValueObject> avos = taskCommand.getAnnotationValueObjects();
    if (avos.size() == 0)
        return new TaskResult(taskCommand, false);
    log.info("Updating " + avos.size() + " characteristics or uncharacterized factor values...");
    StopWatch timer = new StopWatch();
    timer.start();
    Collection<Characteristic> asChars = convertToCharacteristic(avos);
    Collection<FactorValue> factorValues = convertToFactorValuesWithCharacteristics(avos);
    if (asChars.size() == 0 && factorValues.size() == 0) {
        log.info("Nothing to update");
        return new TaskResult(taskCommand, false);
    }
    for (FactorValue factorValue : factorValues) {
        factorValueService.update(factorValue);
    }
    if (asChars.size() == 0)
        return new TaskResult(taskCommand, true);
    Map<Characteristic, Object> charToParent = characteristicService.getParents(asChars);
    for (Characteristic cFromClient : asChars) {
        Long characteristicId = cFromClient.getId();
        if (characteristicId == null) {
            continue;
        }
        Characteristic cFromDatabase = characteristicService.load(characteristicId);
        if (cFromDatabase == null) {
            log.warn("No such characteristic with id=" + characteristicId);
            continue;
        }
        VocabCharacteristic vcFromClient = (cFromClient instanceof VocabCharacteristic) ? (VocabCharacteristic) cFromClient : null;
        VocabCharacteristic vcFromDatabase = (cFromDatabase instanceof VocabCharacteristic) ? (VocabCharacteristic) cFromDatabase : null;
        /*
             * if one of the characteristics is a VocabCharacteristic and the other is not, we have to change the
             * characteristic in the database so that it matches the one from the client; since we can't change the
             * class of the object, we have to remove the old characteristic and make a new one of the appropriate
             * class.
             */
        Object parent = charToParent.get(cFromDatabase);
        /*
             * Check needed because Characteristics are not securable.
             */
        if (parent != null && Securable.class.isAssignableFrom(parent.getClass()) && !securityService.isEditable((Securable) parent)) {
            throw new AccessDeniedException("Access is denied");
        }
        if (vcFromClient != null && vcFromDatabase == null) {
            VocabCharacteristic vc = VocabCharacteristic.Factory.newInstance();
            vc.setValue(StringUtils.strip(cFromDatabase.getValue()));
            vc.setEvidenceCode(cFromDatabase.getEvidenceCode());
            vc.setDescription(cFromDatabase.getDescription());
            vc.setCategory(cFromDatabase.getCategory());
            vc.setName(cFromDatabase.getName());
            vcFromDatabase = (VocabCharacteristic) characteristicService.create(vc);
            removeFromParent(cFromDatabase, parent);
            characteristicService.remove(cFromDatabase);
            addToParent(vcFromDatabase, parent);
            cFromDatabase = vcFromDatabase;
        } else if (vcFromClient == null && vcFromDatabase != null) {
            // don't copy AuditTrail or Status to avoid cascade error... vcFromDatabase.getAuditTrail()
            cFromDatabase = characteristicService.create(Characteristic.Factory.newInstance(vcFromDatabase.getName(), vcFromDatabase.getDescription(), null, StringUtils.strip(vcFromDatabase.getValue()), vcFromDatabase.getCategory(), vcFromDatabase.getCategoryUri(), vcFromDatabase.getEvidenceCode()));
            removeFromParent(vcFromDatabase, parent);
            characteristicService.remove(vcFromDatabase);
            addToParent(cFromDatabase, parent);
        }
        /*
             * at this point, cFromDatabase points to the class-corrected characteristic in the database that must be
             * updated with the information coming from the client.
             */
        assert cFromDatabase != null;
        cFromDatabase.setValue(cFromClient.getValue());
        cFromDatabase.setCategory(cFromClient.getCategory());
        if (cFromDatabase instanceof VocabCharacteristic) {
            vcFromDatabase = (VocabCharacteristic) cFromDatabase;
            if (vcFromClient != null) {
                if (vcFromDatabase.getValueUri() == null || vcFromDatabase.getValueUri() == null || !vcFromDatabase.getValueUri().equals(vcFromClient.getValueUri())) {
                    log.info("Characteristic value update: " + vcFromDatabase + " " + vcFromDatabase.getValueUri() + " -> " + vcFromClient.getValueUri() + " associated with " + parent);
                    vcFromDatabase.setValueUri(vcFromClient.getValueUri());
                }
                if (vcFromDatabase.getCategory() == null || vcFromDatabase.getCategoryUri() == null || !vcFromDatabase.getCategoryUri().equals(vcFromClient.getCategoryUri())) {
                    log.info("Characteristic category update: " + vcFromDatabase + " " + vcFromDatabase.getCategoryUri() + " -> " + vcFromClient.getCategoryUri() + " associated with " + parent);
                    vcFromDatabase.setCategoryUri(vcFromClient.getCategoryUri());
                }
            }
        }
        if (cFromClient.getEvidenceCode() == null) {
            // characteristic has been manually updated
            cFromDatabase.setEvidenceCode(GOEvidenceCode.IC);
        } else {
            if (!cFromDatabase.getEvidenceCode().equals(cFromClient.getEvidenceCode())) {
                log.info("Characteristic evidence code update: " + cFromDatabase + " " + cFromDatabase.getEvidenceCode() + " -> " + cFromClient.getEvidenceCode());
            }
            // let them change it.
            cFromDatabase.setEvidenceCode(cFromClient.getEvidenceCode());
        }
        characteristicService.update(cFromDatabase);
    }
    timer.stop();
    if (timer.getTime() > 1000) {
        log.info("Update took: " + timer.getTime());
    }
    return new TaskResult(taskCommand, true);
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) FactorValue(ubic.gemma.model.expression.experiment.FactorValue) Characteristic(ubic.gemma.model.common.description.Characteristic) VocabCharacteristic(ubic.gemma.model.common.description.VocabCharacteristic) AnnotationValueObject(ubic.gemma.model.common.description.AnnotationValueObject) VocabCharacteristic(ubic.gemma.model.common.description.VocabCharacteristic) StopWatch(org.apache.commons.lang3.time.StopWatch) TaskResult(ubic.gemma.core.job.TaskResult) AnnotationValueObject(ubic.gemma.model.common.description.AnnotationValueObject)

Example 4 with StringUtils.strip

use of org.apache.commons.lang3.StringUtils.strip in project syncope by apache.

the class SearchCondVisitor method addresses.

private SearchCond addresses(final String operator, final String left, final String right, final List<SCIMUserAddressConf> items) {
    if (left.endsWith(".type") && "eq".equals(operator)) {
        Optional<SCIMUserAddressConf> item = items.stream().filter(object -> object.getType().name().equals(StringUtils.strip(right, "\""))).findFirst();
        if (item.isPresent()) {
            AttributeCond attributeCond = new AttributeCond();
            attributeCond.setSchema(item.get().getFormatted());
            attributeCond.setType(AttributeCond.Type.ISNOTNULL);
            return SearchCond.getLeafCond(attributeCond);
        }
    } else if (!conf.getUserConf().getEmails().isEmpty() && (MULTIVALUE.contains(left) || left.endsWith(".value"))) {
        List<SearchCond> orConds = new ArrayList<>();
        items.forEach(item -> {
            AttributeCond cond = new AttributeCond();
            cond.setSchema(item.getFormatted());
            cond.setExpression(StringUtils.strip(right, "\""));
            orConds.add(setOperator(cond, operator));
        });
        if (!orConds.isEmpty()) {
            return SearchCond.getOrCond(orConds);
        }
    }
    return null;
}
Also used : Arrays(java.util.Arrays) SCIMUserConf(org.apache.syncope.common.lib.scim.SCIMUserConf) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) SCIMUserAddressConf(org.apache.syncope.common.lib.scim.SCIMUserAddressConf) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) List(java.util.List) AttributeCond(org.apache.syncope.core.persistence.api.dao.search.AttributeCond) Resource(org.apache.syncope.ext.scimv2.api.type.Resource) SCIMComplexConf(org.apache.syncope.common.lib.scim.SCIMComplexConf) SCIMConf(org.apache.syncope.common.lib.scim.SCIMConf) Map(java.util.Map) Optional(java.util.Optional) AnyCond(org.apache.syncope.core.persistence.api.dao.search.AnyCond) SCIMUserAddressConf(org.apache.syncope.common.lib.scim.SCIMUserAddressConf) AttributeCond(org.apache.syncope.core.persistence.api.dao.search.AttributeCond) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with StringUtils.strip

use of org.apache.commons.lang3.StringUtils.strip in project knime-core by knime.

the class StringManipulationSettings method createJavaScriptingSettings.

/**
 * Create settings to be used by {@link ColumnCalculator} in order
 * to execute the expression.
 *
 * @return settings java scripting settings
 * @throws InvalidSettingsException when settings are not correct
 * @since 3.3
 */
public JavaScriptingSettings createJavaScriptingSettings() throws InvalidSettingsException {
    // determine return type
    m_returnType = null == m_returnType ? determineReturnType(StringUtils.strip(m_expression)) : m_returnType;
    JavaScriptingSettings s = new JavaScriptingSettings(null);
    s.setArrayReturn(false);
    s.setColName(this.getColName());
    s.setExpression("return " + this.getExpression() + ";");
    s.setExpressionVersion(Expression.VERSION_2X);
    s.setHeader("");
    s.setInsertMissingAsNull(this.isInsertMissingAsNull());
    Bundle bundle = FrameworkUtil.getBundle(this.getClass());
    try {
        List<String> includes = new ArrayList<String>();
        URL snippetIncURL = FileLocator.find(bundle, new Path("/lib/snippet_inc"), null);
        File includeDir = new File(FileLocator.toFileURL(snippetIncURL).getPath());
        for (File includeJar : includeDir.listFiles()) {
            if (includeJar.isFile() && includeJar.getName().endsWith(".jar")) {
                includes.add(includeJar.getPath());
                LOGGER.debug("Include jar file: " + includeJar.getPath());
            }
        }
        StringManipulatorProvider provider = StringManipulatorProvider.getDefault();
        includes.add(provider.getJarFile().getAbsolutePath());
        includes.add(FileLocator.getBundleFile(FrameworkUtil.getBundle(StringUtils.class)).getAbsolutePath());
        s.setJarFiles(includes.toArray(new String[includes.size()]));
    } catch (IOException e) {
        throw new IllegalStateException("Cannot locate necessary libraries due to I/O problem: " + e.getMessage(), e);
    }
    s.setReplace(this.isReplace());
    s.setReturnType(m_returnType.getName());
    s.setTestCompilationOnDialogClose(this.isTestCompilationOnDialogClose());
    List<String> imports = new ArrayList<String>();
    // Use defaults imports
    imports.addAll(Arrays.asList(Expression.getDefaultImports()));
    StringManipulatorProvider provider = StringManipulatorProvider.getDefault();
    // Add StringManipulators to the imports
    Collection<Manipulator> manipulators = provider.getManipulators(ManipulatorProvider.ALL_CATEGORY);
    for (Manipulator manipulator : manipulators) {
        String toImport = manipulator.getClass().getName();
        imports.add("static " + toImport + ".*");
    }
    s.setImports(imports.toArray(new String[imports.size()]));
    return s;
}
Also used : Path(org.eclipse.core.runtime.Path) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JavaScriptingSettings(org.knime.ext.sun.nodes.script.settings.JavaScriptingSettings) URL(java.net.URL) Manipulator(org.knime.base.node.preproc.stringmanipulation.manipulator.Manipulator) StringUtils(org.apache.commons.lang3.StringUtils) File(java.io.File)

Aggregations

ArrayList (java.util.ArrayList)3 StringUtils (org.apache.commons.lang3.StringUtils)3 StopWatch (org.apache.commons.lang3.time.StopWatch)3 Arrays (java.util.Arrays)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 SCIMComplexConf (org.apache.syncope.common.lib.scim.SCIMComplexConf)2 SCIMConf (org.apache.syncope.common.lib.scim.SCIMConf)2 SCIMUserAddressConf (org.apache.syncope.common.lib.scim.SCIMUserAddressConf)2 SCIMUserConf (org.apache.syncope.common.lib.scim.SCIMUserConf)2 AnyCond (org.apache.syncope.core.persistence.api.dao.search.AnyCond)2 AttributeCond (org.apache.syncope.core.persistence.api.dao.search.AttributeCond)2 SearchCond (org.apache.syncope.core.persistence.api.dao.search.SearchCond)2 Resource (org.apache.syncope.ext.scimv2.api.type.Resource)2 File (java.io.File)1 IOException (java.io.IOException)1 URL (java.net.URL)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1