Search in sources :

Example 91 with EMPTY

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

the class GeneSearchServiceImpl method getGOGroupGenes.

@Override
public Collection<Gene> getGOGroupGenes(String goQuery, Taxon taxon) {
    StopWatch timer = new StopWatch();
    timer.start();
    Collection<Taxon> taxaForPhenotypeAssoc = new ArrayList<>();
    Collection<Gene> genes = new ArrayList<>();
    // if taxon isn't set, get go groups for each possible taxon
    if (taxon == null) {
        taxaForPhenotypeAssoc.addAll(taxonService.loadAllTaxaWithGenes());
    } else {
        taxaForPhenotypeAssoc.add(taxon);
    }
    for (Taxon taxonForPA : taxaForPhenotypeAssoc) {
        for (GeneSet geneSet : geneSetSearch.findByGoTermName(goQuery, taxonForPA, GeneSearchServiceImpl.MAX_GO_TERMS_TO_PROCESS, GeneSearchServiceImpl.MAX_GO_GROUP_SIZE)) {
            // don't bother adding empty groups
            if (geneSet.getMembers() != null && geneSet.getMembers().size() != 0) {
                for (GeneSetMember geneMember : geneSet.getMembers()) {
                    genes.add(geneMember.getGene());
                }
            }
        }
    }
    GeneSearchServiceImpl.log.info("GO search: " + timer.getTime() + "ms");
    return genes;
}
Also used : Gene(ubic.gemma.model.genome.Gene) Taxon(ubic.gemma.model.genome.Taxon) StopWatch(org.apache.commons.lang3.time.StopWatch)

Example 92 with EMPTY

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

the class ExperimentalDesignVisualizationServiceImpl method sortVectorDataByDesign.

@Override
public Map<Long, LinkedHashMap<BioAssayValueObject, LinkedHashMap<ExperimentalFactor, Double>>> sortVectorDataByDesign(Collection<DoubleVectorValueObject> dedVs) {
    if (dedVs == null) {
        return new HashMap<>(0);
    }
    Map<Long, LinkedHashMap<BioAssayValueObject, LinkedHashMap<ExperimentalFactor, Double>>> returnedLayouts = new HashMap<>(dedVs.size());
    StopWatch timer = new StopWatch();
    timer.start();
    /*
         * This is shared across experiments that might show up in the dedVs; this should be okay...saves computation.
         * This is the only slow part.
         */
    this.prepare(dedVs);
    /*
         * This loop is not a performance issue.
         */
    Map<DoubleVectorValueObject, List<BioAssayValueObject>> newOrderingsForBioAssayDimensions = new HashMap<>();
    for (DoubleVectorValueObject vec : dedVs) {
        if (vec.isReorganized()) {
            continue;
        }
        assert !vec.getBioAssays().isEmpty();
        LinkedHashMap<BioAssayValueObject, LinkedHashMap<ExperimentalFactor, Double>> layout = null;
        if (cachedLayouts.containsKey(vec.getExpressionExperiment().getId())) {
            layout = cachedLayouts.get(vec.getExpressionExperiment().getId());
        } else if (vec.getExpressionExperiment().getClass().isInstance(ExpressionExperimentSubsetValueObject.class)) {
            // subset.
            layout = cachedLayouts.get(((ExpressionExperimentSubsetValueObject) vec.getExpressionExperiment()).getSourceExperiment());
        }
        if (layout == null || layout.isEmpty()) {
            log.error("Did not find cached layout for " + vec.getId());
            continue;
        }
        List<BioAssayValueObject> newOrdering = new ArrayList<>(layout.keySet());
        newOrdering.retainAll(vec.getBioAssays());
        /*
             * This can happen if the vectors are out of whack with the bioassays - e.g. two platforms were used but
             * merging is not done. See bug 3775. Skipping the ordering is not the right thing to do.
             */
        if (newOrdering.isEmpty()) {
            boolean allNaN = this.allNaN(vec);
            if (allNaN) {
                // reordering will have no effect.
                continue;
            }
            /*
                 * Add to the layout.
                 */
            layout = this.extendLayout(vec, vec.getExpressionExperiment().getId());
            newOrdering = new ArrayList<>(layout.keySet());
            newOrdering.retainAll(vec.getBioAssays());
            assert !newOrdering.isEmpty();
        }
        newOrderingsForBioAssayDimensions.put(vec, newOrdering);
        Map<BioAssayValueObject, Integer> ordering = this.getOrdering(newOrdering);
        Long eeId;
        // might be subset id.
        eeId = vec.getExpressionExperiment().getId();
        if (!returnedLayouts.containsKey(eeId)) {
            if (vec.isSliced()) {
                LinkedHashMap<BioAssayValueObject, LinkedHashMap<ExperimentalFactor, Double>> trimmedLayout = new LinkedHashMap<>();
                for (BioAssayValueObject baVo : newOrdering) {
                    trimmedLayout.put(baVo, layout.get(baVo));
                }
                returnedLayouts.put(eeId, trimmedLayout);
            } else {
                returnedLayouts.put(eeId, layout);
            }
        }
        /*
             * Might be a faster way.
             */
        double[] data = vec.getData();
        double[] dol = ArrayUtils.clone(data);
        // assert ordering.size() == data.length : "got " + ordering.size() + " expected " + data.length;
        List<BioAssayValueObject> oldOrdering = vec.getBioAssayDimension().getBioAssays();
        int j = 0;
        if (log.isTraceEnabled())
            log.trace("Old order: " + StringUtils.join(ArrayUtils.toObject(data), ","));
        for (BioAssayValueObject ba : oldOrdering) {
            if (ordering.get(ba) == null) {
                assert Double.isNaN(dol[j]);
                j++;
                continue;
            }
            assert ordering.containsKey(ba);
            assert ordering.get(ba) != null;
            Integer targetIndex = ordering.get(ba);
            data[targetIndex] = dol[j++];
        }
        if (log.isTraceEnabled())
            log.trace("New order: " + StringUtils.join(ArrayUtils.toObject(data), ","));
        vec.setReorganized(true);
    }
    for (DoubleVectorValueObject vec : dedVs) {
        if (vec.getBioAssayDimension().isReordered())
            continue;
        List<BioAssayValueObject> newOrdering = newOrderingsForBioAssayDimensions.get(vec);
        if (newOrdering == null)
            // data was empty, etc.
            continue;
        vec.getBioAssayDimension().reorder(newOrdering);
    }
    if (timer.getTime() > 1500) {
        log.info("Sort vectors by design: " + timer.getTime() + "ms");
    }
    return returnedLayouts;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StopWatch(org.apache.commons.lang3.time.StopWatch) BioAssayValueObject(ubic.gemma.model.expression.bioAssay.BioAssayValueObject) List(java.util.List) DoubleVectorValueObject(ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject)

Example 93 with EMPTY

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

the class CoexpressionDaoImpl method getCoexpressionFromDbViaGenes2.

/**
 * Alternative method: query twice, once to get the coexpression basics and then again to get the support details,
 * instead of using a join.
 *
 * @param populateTestedInDetails populate tested in details
 * @param stringency              stringency
 * @param geneIds                 gene IDs
 * @param t                       taxon
 */
private Map<Long, List<CoexpressionValueObject>> getCoexpressionFromDbViaGenes2(Collection<Long> geneIds, Taxon t, int stringency, boolean populateTestedInDetails) {
    StopWatch timer = new StopWatch();
    timer.start();
    List<Object[]> q1results = this.getRawCoexpressionFromDbViaGenes(geneIds, t, stringency);
    CoexpressionDaoImpl.log.debug(q1results.size() + " raw coexpression results for " + geneIds.size() + " genes at support>=" + stringency + " " + timer.getTime() + "ms");
    if (q1results.isEmpty()) {
        return new HashMap<>();
    }
    List<Object[]> supportDetails = new ArrayList<>();
    /*
         * Because we are not trimming the results at all here, this can be a lot of data to iterate over, even at
         * high stringencies. For example, for 20 genes at a stringency of 5, because the query above does not
         * constrain to data sets, there can be >500 per gene, or >100k links in total. Fetching the support details
         * here is rather wasteful if we are not retaining the results, but we don't know that until we know which
         * data sets are supporting.
         */
    BatchIterator<Object[]> batches = BatchIterator.batches(q1results, CoexpressionDaoImpl.BATCH_SIZE);
    int n = 1;
    for (Collection<Object[]> batch : batches) {
        StopWatch timer2 = new StopWatch();
        timer2.start();
        List<Long> supportDetailsIds = new ArrayList<>();
        for (Object[] oa : batch) {
            Long supportDetailsId = ((BigInteger) oa[5]).longValue();
            supportDetailsIds.add(supportDetailsId);
        }
        // Note: should never be empty
        String sqlQuery2 = "select ID,BYTES from " + CoexpressionQueryUtils.getSupportDetailsTableName(t) + " where ID in (:ids)";
        SQLQuery query2 = this.getSessionFactory().getCurrentSession().createSQLQuery(sqlQuery2);
        query2.setParameterList("ids", supportDetailsIds.toArray());
        supportDetails.addAll(query2.list());
        if (timer2.getTime() > 1000) {
            CoexpressionDaoImpl.log.debug("Fetch batch " + n + " of support details: " + timer2.getTime() + "ms");
        }
        n++;
    }
    CoexpressionDaoImpl.log.debug("Fetched details for " + supportDetails.size() + " coexpressions, " + n + " batches");
    if (timer.getTime() > 5000) {
        CoexpressionDaoImpl.log.info("Coexpression query: " + geneIds.size() + " genes took " + timer.getTime() + "ms: " + q1results.size() + " results");
    }
    timer.reset();
    timer.start();
    // it might be better to do this in the loop above, incrementally per batch.
    Map<Long, List<CoexpressionValueObject>> results = this.convertToValueObjects(q1results, supportDetails, geneIds);
    if (timer.getTime() > 100) {
        CoexpressionDaoImpl.log.info("Convert to value objects " + q1results.size() + " results: " + timer.getTime() + "ms");
    }
    timer.reset();
    timer.start();
    for (Long g : results.keySet()) {
        List<CoexpressionValueObject> gc = results.get(g);
        Collections.sort(gc);
        if (populateTestedInDetails) {
            this.populateTestedInDetails(gc);
        }
    }
    if (timer.getTime() > 100) {
        CoexpressionDaoImpl.log.info("Filter, sort and finish " + q1results.size() + " results: " + timer.getTime() + "ms");
    }
    return results;
}
Also used : StopWatch(org.apache.commons.lang3.time.StopWatch) BigInteger(java.math.BigInteger) IdArrayValueObject(ubic.gemma.model.analysis.expression.coexpression.IdArrayValueObject) GeneCoexpressionNodeDegreeValueObject(ubic.gemma.model.association.coexpression.GeneCoexpressionNodeDegreeValueObject)

Example 94 with EMPTY

use of org.apache.commons.lang3.StringUtils.EMPTY in project molgenis by molgenis.

the class SortaController method retrieveSortaJobResults.

@PostMapping("/match/retrieve")
@ResponseBody
public EntityCollectionResponse retrieveSortaJobResults(@RequestBody SortaServiceRequest sortaServiceRequest) {
    List<Map<String, Object>> entityMaps = new ArrayList<>();
    String sortaJobExecutionId = sortaServiceRequest.getSortaJobExecutionId();
    String filterQuery = sortaServiceRequest.getFilterQuery();
    String ontologyIri = sortaServiceRequest.getOntologyIri();
    EntityPager entityPager = sortaServiceRequest.getEntityPager();
    SortaJobExecution sortaJobExecution = findSortaJobExecution(sortaJobExecutionId);
    String resultEntityName = sortaJobExecution.getResultEntityName();
    double threshold = sortaJobExecution.getThreshold();
    boolean isMatched = sortaServiceRequest.isMatched();
    QueryRule queryRuleInputEntities = new QueryRule(Arrays.asList(new QueryRule(VALIDATED, EQUALS, isMatched), new QueryRule(isMatched ? OR : AND), new QueryRule(SCORE, isMatched ? GREATER_EQUAL : LESS, threshold)));
    List<QueryRule> queryRuleInputEntitiesInOneMatchingTask = singletonList(queryRuleInputEntities);
    // Add filter to the query if query string is not empty
    if (isNotEmpty(filterQuery)) {
        Iterable<String> filteredInputTermIds = dataService.findAll(sortaJobExecution.getSourceEntityName(), new QueryImpl<>().search(filterQuery)).map(inputEntity -> inputEntity.getString(SortaServiceImpl.DEFAULT_MATCHING_IDENTIFIER)).collect(Collectors.toList());
        QueryRule previousQueryRule = new QueryRule(queryRuleInputEntitiesInOneMatchingTask);
        QueryRule queryRuleFilterInput = new QueryRule(MatchingTaskContentMetaData.INPUT_TERM, Operator.IN, filteredInputTermIds);
        queryRuleInputEntitiesInOneMatchingTask = Arrays.asList(previousQueryRule, new QueryRule(Operator.AND), queryRuleFilterInput);
    }
    Query<Entity> query = new QueryImpl<>(queryRuleInputEntitiesInOneMatchingTask);
    long count = dataService.count(resultEntityName, query);
    int start = entityPager.getStart();
    int num = entityPager.getNum();
    Stream<Entity> findAll = dataService.findAll(sortaJobExecution.getResultEntityName(), query.offset(start).pageSize(num).sort(new Sort().on(VALIDATED, DESC).on(SCORE, DESC)));
    findAll.forEach(mappingEntity -> {
        Map<String, Object> outputEntity = new HashMap<>();
        outputEntity.put("inputTerm", getEntityAsMap(mappingEntity.getEntity(INPUT_TERM)));
        outputEntity.put("matchedTerm", getEntityAsMap(mappingEntity));
        Object matchedTerm = mappingEntity.get(MATCHED_TERM);
        if (matchedTerm != null) {
            outputEntity.put("ontologyTerm", SortaServiceUtil.getEntityAsMap(sortaService.getOntologyTermEntity(matchedTerm.toString(), ontologyIri)));
        }
        entityMaps.add(outputEntity);
    });
    EntityPager pager = new EntityPager(start, num, count, null);
    return new EntityCollectionResponse(pager, entityMaps, "/match/retrieve", ontologyTermMetaData, permissionService, dataService);
}
Also used : PluginController(org.molgenis.web.PluginController) DEEP_COPY_ATTRS(org.molgenis.data.meta.model.EntityType.AttributeCopyMode.DEEP_COPY_ATTRS) MenuReaderService(org.molgenis.core.ui.menu.MenuReaderService) IdGenerator(org.molgenis.data.populate.IdGenerator) Operator(org.molgenis.data.QueryRule.Operator) LoggerFactory(org.slf4j.LoggerFactory) PermissionSystemService(org.molgenis.data.security.permission.PermissionSystemService) StringUtils(org.apache.commons.lang3.StringUtils) Attribute(org.molgenis.data.meta.model.Attribute) FileStore(org.molgenis.data.file.FileStore) Collections.singletonList(java.util.Collections.singletonList) JobExecutionMetaData(org.molgenis.jobs.model.JobExecutionMetaData) User(org.molgenis.data.security.auth.User) Model(org.springframework.ui.Model) ByteArrayInputStream(java.io.ByteArrayInputStream) SORTA_JOB_EXECUTION(org.molgenis.ontology.sorta.meta.SortaJobExecutionMetaData.SORTA_JOB_EXECUTION) Arrays.asList(java.util.Arrays.asList) RunAsSystemAspect(org.molgenis.security.core.runas.RunAsSystemAspect) EntityTypeIdentity(org.molgenis.data.security.EntityTypeIdentity) StringUtils.isEmpty(org.apache.commons.lang3.StringUtils.isEmpty) EntityCollectionResponse(org.molgenis.data.rest.EntityCollectionResponse) AttributeFactory(org.molgenis.data.meta.model.AttributeFactory) org.molgenis.data(org.molgenis.data) ImmutableMap(com.google.common.collect.ImmutableMap) XREF(org.molgenis.data.meta.AttributeType.XREF) UserAccountService(org.molgenis.security.user.UserAccountService) EntityType(org.molgenis.data.meta.model.EntityType) Collectors(java.util.stream.Collectors) Stream(java.util.stream.Stream) UserPermissionEvaluator(org.molgenis.security.core.UserPermissionEvaluator) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) URI(org.molgenis.ontology.sorta.controller.SortaController.URI) CsvWriter(org.molgenis.data.csv.CsvWriter) SortaJobExecutionMetaData(org.molgenis.ontology.sorta.meta.SortaJobExecutionMetaData) SortaService(org.molgenis.ontology.sorta.service.SortaService) EntityPager(org.molgenis.data.rest.EntityPager) java.util(java.util) SortaServiceImpl(org.molgenis.ontology.sorta.service.impl.SortaServiceImpl) SimpleDateFormat(java.text.SimpleDateFormat) QueryImpl(org.molgenis.data.support.QueryImpl) SortaCsvRepository(org.molgenis.ontology.sorta.repo.SortaCsvRepository) Controller(org.springframework.stereotype.Controller) EntityTypePermission(org.molgenis.data.security.EntityTypePermission) OntologyTermMetaData(org.molgenis.ontology.core.meta.OntologyTermMetaData) NumberFormat(java.text.NumberFormat) StringUtils.isNotEmpty(org.apache.commons.lang3.StringUtils.isNotEmpty) HttpServletRequest(javax.servlet.http.HttpServletRequest) DynamicEntity(org.molgenis.data.support.DynamicEntity) SortaJobImpl(org.molgenis.ontology.sorta.job.SortaJobImpl) SortaServiceRequest(org.molgenis.ontology.sorta.request.SortaServiceRequest) Objects.requireNonNull(java.util.Objects.requireNonNull) EntityTypeFactory(org.molgenis.data.meta.model.EntityTypeFactory) StreamSupport(java.util.stream.StreamSupport) MatchingTaskContentMetaData(org.molgenis.ontology.sorta.meta.MatchingTaskContentMetaData) OntologyService(org.molgenis.ontology.core.service.OntologyService) ExecutorService(java.util.concurrent.ExecutorService) AttributeType(org.molgenis.data.meta.AttributeType) Logger(org.slf4j.Logger) SortaJobFactory(org.molgenis.ontology.sorta.job.SortaJobFactory) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) SortaServiceUtil(org.molgenis.ontology.utils.SortaServiceUtil) File(java.io.File) SortaServiceResponse(org.molgenis.ontology.sorta.request.SortaServiceResponse) HttpStatus(org.springframework.http.HttpStatus) DESC(org.molgenis.data.Sort.Direction.DESC) SortaServiceUtil.getEntityAsMap(org.molgenis.ontology.utils.SortaServiceUtil.getEntityAsMap) SortaJobExecution(org.molgenis.ontology.sorta.job.SortaJobExecution) MultipartFile(org.springframework.web.multipart.MultipartFile) SortaJobExecutionFactory(org.molgenis.ontology.sorta.job.SortaJobExecutionFactory) InputStream(java.io.InputStream) DynamicEntity(org.molgenis.data.support.DynamicEntity) EntityCollectionResponse(org.molgenis.data.rest.EntityCollectionResponse) SortaJobExecution(org.molgenis.ontology.sorta.job.SortaJobExecution) EntityPager(org.molgenis.data.rest.EntityPager) QueryImpl(org.molgenis.data.support.QueryImpl) ImmutableMap(com.google.common.collect.ImmutableMap) SortaServiceUtil.getEntityAsMap(org.molgenis.ontology.utils.SortaServiceUtil.getEntityAsMap)

Example 95 with EMPTY

use of org.apache.commons.lang3.StringUtils.EMPTY in project xwiki-platform by xwiki.

the class ExecutionContextCopier method copy.

@Override
public ExecutionContext copy(ExecutionContext originalExecutionContext) throws CloneFailedException {
    try {
        ExecutionContext clonedExecutionContext = this.executionContextManager.clone(originalExecutionContext);
        // XWikiContext
        // The above clone just creates and initializes an empty XWiki Context, so it needs special handling.
        XWikiContext xwikiContext = (XWikiContext) originalExecutionContext.getProperty(XWikiContext.EXECUTIONCONTEXT_KEY);
        XWikiContext clonedXWikiContext = xwikiContextCloner.copy(xwikiContext);
        clonedExecutionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, clonedXWikiContext);
        // VelocityContext
        // Reset the VelocityContext from the EC by removing it and calling the Velocity ECInitializer which is
        // normally called by the execution of the ECInitializers by ECManager.clone(). This ensures a clean new
        // VC is created. It'll get filled when VelocityContextManager.getVelocityContext() is called by whatever
        // code need the VC.
        clonedExecutionContext.removeProperty(VelocityExecutionContextInitializer.VELOCITY_CONTEXT_ID);
        this.velocityExecutionContextInitializer.initialize(clonedExecutionContext);
        return clonedExecutionContext;
    } catch (Exception e) {
        throw new CloneFailedException(String.format("Failed to clone [%s]", originalExecutionContext), e);
    }
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) XWikiContext(com.xpn.xwiki.XWikiContext) CloneFailedException(org.apache.commons.lang3.exception.CloneFailedException) CloneFailedException(org.apache.commons.lang3.exception.CloneFailedException)

Aggregations

List (java.util.List)44 Map (java.util.Map)42 ArrayList (java.util.ArrayList)41 StringUtils (org.apache.commons.lang3.StringUtils)38 Collectors (java.util.stream.Collectors)37 HashMap (java.util.HashMap)33 IOException (java.io.IOException)27 Set (java.util.Set)25 HashSet (java.util.HashSet)22 LoggerFactory (org.slf4j.LoggerFactory)22 Pair (org.apache.commons.lang3.tuple.Pair)20 Logger (org.slf4j.Logger)20 Optional (java.util.Optional)19 Collections (java.util.Collections)17 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)17 java.util (java.util)15 Arrays.asList (java.util.Arrays.asList)14 Collection (java.util.Collection)14 Stream (java.util.stream.Stream)14 Arrays (java.util.Arrays)12