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;
}
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;
}
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);
}
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;
}
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;
}
Aggregations