Example usage for org.apache.commons.collections CollectionUtils transform

List of usage examples for org.apache.commons.collections CollectionUtils transform

Introduction

In this page you can find the example usage for org.apache.commons.collections CollectionUtils transform.

Prototype

public static void transform(Collection collection, Transformer transformer) 

Source Link

Document

Transform the collection by applying a Transformer to each element.

Usage

From source file:info.magnolia.cms.util.ContentUtilTest.java

@Test
public void testOrderBeforeMidNodeVariation1() throws RepositoryException, IOException {
    MockHierarchyManager hm = MockUtil.createHierarchyManager(
            "/node/a\n" + "/node/b\n" + "/node/c\n" + "/node/d\n" + "/node/e\n" + "/node/f\n");
    Content node = hm.getContent("/node");
    Content c = node.getContent("b");
    ContentUtil.orderBefore(c, "e");
    Collection<Content> result = node.getChildren();
    CollectionUtils.transform(result, new Transformer() {
        @Override/*from   ww w .j  a v a 2s  . c  o  m*/
        public Object transform(Object childObj) {
            return ((Content) childObj).getName();
        }
    });
    assertEquals(asList("a", "c", "d", "b", "e", "f"), result);
}

From source file:chibi.gemmaanalysis.BatchDiffExCli.java

@Override
protected void processExperiment(ExpressionExperiment ee) {
    String fileprefix = ee.getId() + "." + ee.getShortName().replaceAll("[\\W\\s]+", "_");

    try (Writer detailFile = initOutputFile("batch.proc.detail." + fileprefix + ".txt");) {

        Collection<ExperimentalFactor> experimentalFactors = ee.getExperimentalDesign()
                .getExperimentalFactors();

        ExperimentalFactor batchFactor = expressionExperimentBatchCorrectionService.getBatchFactor(ee);

        if (null == batchFactor) {
            this.errorObjects.add("No batch factor: " + ee.getShortName());
            return;
        }/*from w ww  .  j a  va2s.  co  m*/

        if (experimentalFactors.size() < 2) {
            // need at least two factors, one of which has to be the batch.
            this.errorObjects.add("Too few factors: " + ee.getShortName());
            return;
        }

        if (ee.getBioAssays().size() < 8) {
            this.errorObjects.add("Too small (" + ee.getBioAssays().size() + " samples): " + ee.getShortName());
            return;
        }

        if (experimentalFactors.size() > 10) {
            /*
             * This could be modified to select just a few factors, at random ... but that's probably
             */
            this.errorObjects.add("Too many factors (" + experimentalFactors.size()
                    + " factors, including 'batch'): " + ee.getShortName());
            return;
        }

        /* TODO use this, or skip it... we have this information elsewhere already */
        // expressionExperimentBatchCorrectionService.checkBatchEffectSeverity( ee );

        boolean correctable = expressionExperimentBatchCorrectionService.checkCorrectability(ee);
        if (!correctable) {

            /*
             * Note that later on we can still end up with a model that is not of full rank, so combat will fail.
             * This can sometimes be ameliorated by dropping covariates.
             */
            this.errorObjects
                    .add("Batch effect is not correctable; possibly contains batches with only one sample: "
                            + ee.getShortName());

            return;
        }

        log.info("Processing: " + ee);

        /*
         * Extract data
         */
        Collection<ProcessedExpressionDataVector> vectos = processedExpressionDataVectorService
                .getProcessedDataVectors(ee);

        ExpressionDataDoubleMatrix mat = new ExpressionDataDoubleMatrix(vectos);

        /*
         * TODO for some data sets we should re-normalize?
         */

        StringBuilder summaryBuf = new StringBuilder();

        /*
         * first do an analysis without batch; this is our baseline. Let's ignore interactions to keep things
         * simple.
         */
        Collection<ExperimentalFactor> factors2 = new HashSet<ExperimentalFactor>();
        for (ExperimentalFactor ef : experimentalFactors) {
            if (ef.equals(batchFactor))
                continue;
            factors2.add(ef);
        }
        int j = 0;
        DifferentialExpressionAnalysisConfig configWithoutBatch = new DifferentialExpressionAnalysisConfig();
        configWithoutBatch.setQvalueThreshold(null);
        configWithoutBatch.setFactorsToInclude(factors2);
        DifferentialExpressionAnalysis beforeResults = lma.run(ee, mat, configWithoutBatch).iterator().next();
        Map<CompositeSequence, Map<ExperimentalFactor, Double>> beforeResultDetails = new HashMap<CompositeSequence, Map<ExperimentalFactor, Double>>();
        for (ExpressionAnalysisResultSet brs : beforeResults.getResultSets()) {
            assert brs.getExperimentalFactors().size() == 1;
            ExperimentalFactor ef = brs.getExperimentalFactors().iterator().next();
            Collection<DifferentialExpressionAnalysisResult> results = brs.getResults();
            int c = 0;
            for (DifferentialExpressionAnalysisResult r : results) {
                c = tally(beforeResultDetails, ef, r, c);
                if (++j % LOGGING_FREQ == 0) {
                    log.info(j + " processed");
                }
            }
            summaryBuf.append("Before\t" + ee.getId() + "\t" + ee.getShortName() + "\t" + ef.getId() + "\t"
                    + ef.getName() + "\t" + results.size() + "\t" + c + "\n");
        }

        /*
         * Then do it with batch.
         */
        Collection<ExperimentalFactor> factors = experimentalFactors;
        assert factors.contains(batchFactor);
        DifferentialExpressionAnalysisConfig configIncludingBatch = new DifferentialExpressionAnalysisConfig();
        configIncludingBatch.setQvalueThreshold(summaryQvalThreshold);

        configIncludingBatch.setFactorsToInclude(factors);

        DifferentialExpressionAnalysis withBatchEffectResults = lma.run(ee, mat, configIncludingBatch)
                .iterator().next();

        /*
         * Determine how many genes are diff ex wrt batch. The other factors are tracked; this shows how we would do
         * if we tried to simply directly include batch in the model
         */
        Map<CompositeSequence, Map<ExperimentalFactor, Double>> batchEffectDetails = new HashMap<>();

        for (ExpressionAnalysisResultSet brs : withBatchEffectResults.getResultSets()) {
            assert brs.getExperimentalFactors().size() == 1;
            ExperimentalFactor ef = brs.getExperimentalFactors().iterator().next();
            Collection<DifferentialExpressionAnalysisResult> results = brs.getResults();

            int c = 0;
            for (DifferentialExpressionAnalysisResult r : results) {
                c = tally(batchEffectDetails, ef, r, c);

                if (++j % LOGGING_FREQ == 0) {
                    log.info(j + " processed");
                }

            }
            summaryBuf.append("Batch\t" + ee.getId() + "\t" + ee.getShortName() + "\t" + ef.getId() + "\t"
                    + ef.getName() + "\t" + results.size() + "\t" + c + "\n");
        }

        /*
         * Correct for batch effects; covariates which are "unimportant" will be dropped.
         */
        log.info("ComBat-ing");
        boolean parametric = true;
        double importanceThreshold = 0.01;
        ExpressionDataDoubleMatrix comBat = expressionExperimentBatchCorrectionService.comBat(mat, parametric,
                importanceThreshold);
        assert comBat != null;

        /*
         * Check if we have removed the batch effect: there should be no diff ex wrt batch. This is just a sanity
         * check, really. The other factors are tracked just for completeness. Note that Combat log transforms the
         * data if necessary, but transforms it back.
         */
        DifferentialExpressionAnalysis revisedResultWithBatch = lma.run(ee, comBat, configIncludingBatch)
                .iterator().next();

        Map<CompositeSequence, Map<ExperimentalFactor, Double>> batchEffectAfterCorrDetails = new HashMap<CompositeSequence, Map<ExperimentalFactor, Double>>();

        for (ExpressionAnalysisResultSet brs : revisedResultWithBatch.getResultSets()) {
            assert brs.getExperimentalFactors().size() == 1;
            ExperimentalFactor ef = brs.getExperimentalFactors().iterator().next();
            Collection<DifferentialExpressionAnalysisResult> results = brs.getResults();
            int c = 0;
            for (DifferentialExpressionAnalysisResult r : results) {
                c = tally(batchEffectAfterCorrDetails, ef, r, c);

                if (++j % LOGGING_FREQ == 0) {
                    log.info(j + " processed");
                }

            }
            summaryBuf.append("BatchAftCorr\t" + ee.getId() + "\t" + ee.getShortName() + "\t" + ef.getId()
                    + "\t" + ef.getName() + "\t" + results.size() + "\t" + c + "\n");
        }

        /*
         * Now without batch as a factor, which is what we really want.
         */
        boolean hasNonNulls = false;
        DifferentialExpressionAnalysis revisedResult = lma.run(ee, comBat, configWithoutBatch).iterator()
                .next();
        Map<CompositeSequence, Map<ExperimentalFactor, Double>> revisedResultDetails = new HashMap<CompositeSequence, Map<ExperimentalFactor, Double>>();
        for (ExpressionAnalysisResultSet brs : revisedResult.getResultSets()) {
            assert brs.getExperimentalFactors().size() == 1;
            ExperimentalFactor ef = brs.getExperimentalFactors().iterator().next();
            Collection<DifferentialExpressionAnalysisResult> results = brs.getResults();
            int c = 0;
            for (DifferentialExpressionAnalysisResult r : results) {

                if (r.getCorrectedPvalue() != null && !Double.isNaN(r.getCorrectedPvalue())) {
                    hasNonNulls = true;
                }

                c = tally(revisedResultDetails, ef, r, c);

                if (++j % LOGGING_FREQ == 0) {
                    log.info(j + " processed");
                }

            }
            summaryBuf.append("After\t" + ee.getId() + "\t" + ee.getShortName() + "\t" + ef.getId() + "\t"
                    + ef.getName() + "\t" + results.size() + "\t" + c + "\n");

        }

        if (!hasNonNulls) {
            // this means something went wrong ... somewhere. Possibly the model cannot be fit.
            errorObjects.add("No valid pvalues after correction: " + ee.getShortName());
            return;
        }

        /*
         * Print out details
         */

        detailFile.write(
                "EEID\tEENAME\tEFID\tEFNAME\tPROBEID\tPROBENAME\tGENESYMBS\tGENEIDS\tBEFOREQVAL\tBATCHQVAL\tBATAFTERQVAL\tAFTERQVAL\n");

        getGeneAnnotations(ee);

        for (CompositeSequence c : beforeResultDetails.keySet()) {

            // Get the gene information
            String geneSymbs = "";
            String geneIds = "";
            if (genes.containsKey(c)) {
                Collection<Gene> g = new HashSet<Gene>();
                g.addAll(genes.get(c));
                CollectionUtils.transform(g, geneSymbolTransformer);
                geneSymbs = StringUtils.join(g, "|");
                geneIds = StringUtils.join(EntityUtils.getIds(genes.get(c)), "|");
            }

            for (ExperimentalFactor ef : factors) {

                detailFile.write(ee.getId() + "\t" + ee.getShortName() + "\t" + ef.getId() + "\t" + ef.getName()
                        + "\t" + c.getId() + "\t" + c.getName() + "\t" + geneSymbs + "\t" + geneIds + "\t");

                Double bpval = beforeResultDetails.get(c).get(ef); // will be null for 'batch'

                Double batpval = batchEffectDetails.get(c).get(ef); // when batch was included.

                Double batapval = batchEffectAfterCorrDetails.get(c).get(ef); // when batch was included.

                Double aftpval = revisedResultDetails.get(c).get(ef); // will be null for 'batch'

                detailFile.write(String.format("%.4g\t%.4g\t%.4g\t%.4g\n", bpval, batpval, batapval, aftpval));

            }
        }
        detailFile.close();

        summaryFile.write(summaryBuf.toString());
        summaryFile.flush();

        String rawDataFileName = fileprefix + ".originaldata.txt";
        saveData(mat, rawDataFileName);
        String correctedDataFileName = fileprefix + ".correcteddata.txt";
        saveData(comBat, correctedDataFileName);

        successObjects.add(ee);
    } catch (Exception e) {
        log.error(e, e);
        errorObjects.add(ee + e.getMessage());
    }
}

From source file:info.magnolia.cms.util.ContentUtilTest.java

@Test
public void testOrderBeforeMidNodeVariation2() throws RepositoryException, IOException {
    MockHierarchyManager hm = MockUtil.createHierarchyManager(
            "/node/a\n" + "/node/b\n" + "/node/c\n" + "/node/d\n" + "/node/e\n" + "/node/f\n");
    Content node = hm.getContent("/node");
    Content c = node.getContent("a");
    ContentUtil.orderBefore(c, "e");
    Collection<Content> result = node.getChildren();
    CollectionUtils.transform(result, new Transformer() {
        @Override//from w  ww. j  a v  a 2s  . c  o m
        public Object transform(Object childObj) {
            return ((Content) childObj).getName();
        }
    });
    assertEquals(asList("b", "c", "d", "a", "e", "f"), result);
}

From source file:info.magnolia.cms.util.ContentUtilTest.java

@Test
public void testOrderBeforeMidNodeVariation3() throws RepositoryException, IOException {
    MockHierarchyManager hm = MockUtil.createHierarchyManager(
            "/node/a\n" + "/node/b\n" + "/node/c\n" + "/node/d\n" + "/node/e\n" + "/node/f\n");
    Content node = hm.getContent("/node");
    Content c = node.getContent("f");
    ContentUtil.orderBefore(c, "e");
    Collection<Content> result = node.getChildren();
    CollectionUtils.transform(result, new Transformer() {
        @Override/* www.  j a  va  2 s .  c o  m*/
        public Object transform(Object childObj) {
            return ((Content) childObj).getName();
        }
    });
    assertEquals(asList("a", "b", "c", "d", "f", "e"), result);
}

From source file:fr.itldev.koya.alfservice.KoyaNodeService.java

public List<SecuredItem> listChildrenAsTree(NodeRef parent, final Integer depth, final boolean onlyFolders)
        throws KoyaServiceException {

    List<Pair<QName, Boolean>> sortProps = new ArrayList() {
        private static final long serialVersionUID = 1L;

        {/*from  w w w . j av  a2s  .c  om*/
            add(new Pair<>(GetChildrenCannedQuery.SORT_QNAME_NODE_IS_FOLDER, false));
            add(new Pair<>(ContentModel.PROP_TITLE, true));
        }
    };

    PagingResults<FileInfo> results = fileFolderService.list(parent, !onlyFolders, true, null, sortProps,
            new PagingRequest(Integer.valueOf(0), Integer.MAX_VALUE));

    List children = results.getPage();

    /**
     * Transform List<FileInfo> as List<SecuredItem>
     */
    CollectionUtils.transform(children, new Transformer() {
        @Override
        public Object transform(Object input) {
            try {
                FileInfo fi = (FileInfo) input;
                SecuredItem si = getSecuredItem(fi.getNodeRef());
                try {
                    if (depth != null && depth > 0) {
                        Directory dir = (Directory) si;
                        List children = listChildrenAsTree(fi.getNodeRef(), depth - 1, onlyFolders);
                        dir.setChildren(children);
                    }
                } catch (ClassCastException cce) {
                    //Faster than instanceOf
                }
                return si;
            } catch (KoyaServiceException ex) {
                return null;
            }
        }
    });
    return children;
}

From source file:com.architexa.diagrams.jdt.model.CodeUnit.java

/**
 * Note the input collection is changed by the method
 *//*w  w  w .j a va 2s  .c om*/
@SuppressWarnings("unchecked")
private static List<CodeUnit> transformResourcesToCodeUnits(List<Resource> in) {
    CollectionUtils.transform(in, new Transformer() {
        public Object transform(Object arg0) {
            return new CodeUnit((Resource) arg0);
        }
    });
    return (List) in;
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCollection.java

/**
 * {@inheritDoc}/*  www  .  j  ava  2 s  .c om*/
 */
@Override
public boolean has(final String name, final Scriptable start) {
    try {
        final int index = Integer.parseInt(name);
        final List<Object> elements = getElements();
        CollectionUtils.transform(elements, transformer_);
        if (index >= 0 && index < elements.size()) {
            return true;
        }
    } catch (final NumberFormatException e) {
        // Ignore.
    }

    if (name.equals("length")) {
        return true;
    }
    if (!getBrowserVersion().hasFeature(BrowserVersionFeatures.GENERATED_49)) {
        final JavaScriptConfiguration jsConfig = JavaScriptConfiguration.getInstance(BrowserVersion.FIREFOX_3);
        for (final String functionName : jsConfig.getClassConfiguration(getClassName()).functionKeys()) {
            if (name.equals(functionName)) {
                return true;
            }
        }
        return false;
    }
    return getWithPreemption(name) != NOT_FOUND;
}

From source file:fr.itldev.koya.alfservice.KoyaNodeService.java

/**
 *
 * @param parent//from www . j  ava  2  s.c o m
 * @param skipCount
 * @param maxItems
 * @param onlyFolders
 * @return
 * @throws KoyaServiceException
 */
public Pair<List<SecuredItem>, Pair<Integer, Integer>> listChildrenPaginated(NodeRef parent,
        final Integer skipCount, final Integer maxItems, final boolean onlyFolders, final String namePattern)
        throws KoyaServiceException {

    Integer skip = skipCount;
    Integer max = maxItems;
    if (skipCount == null) {
        skip = Integer.valueOf(0);
    }
    if (maxItems == null) {
        max = Integer.MAX_VALUE;
    }
    List<Pair<QName, Boolean>> sortProps = new ArrayList() {
        private static final long serialVersionUID = 1L;

        {
            add(new Pair<>(GetChildrenCannedQuery.SORT_QNAME_NODE_IS_FOLDER, false));
            add(new Pair<>(ContentModel.PROP_TITLE, true));
        }
    };

    PagingRequest pr = new PagingRequest(skip, max);
    pr.setRequestTotalCountMax(Integer.MAX_VALUE);

    PagingResults<FileInfo> results = fileFolderService.list(parent, !onlyFolders, true, namePattern, null,
            sortProps, pr);

    List children = results.getPage();

    /**
     * Transform List<FileInfo> as List<SecuredItem>
     */
    CollectionUtils.transform(children, new Transformer() {
        @Override
        public Object transform(Object input) {
            try {
                return getSecuredItem(((FileInfo) input).getNodeRef());
            } catch (KoyaServiceException ex) {
                return null;
            }
        }
    });

    return new Pair<List<SecuredItem>, Pair<Integer, Integer>>(children, results.getTotalResultCount());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCollection.java

/**
 * {@inheritDoc}./*from   w  w  w.j  av  a2  s  .  com*/
 */
@Override
public Object[] getIds() {
    final List<String> idList = new ArrayList<String>();

    final List<Object> elements = getElements();
    CollectionUtils.transform(elements, transformer_);

    if (!getBrowserVersion().hasFeature(BrowserVersionFeatures.GENERATED_50)) {
        final int length = getElements().size();
        for (int i = 0; i < length; i++) {
            idList.add(Integer.toString(i));
        }

        idList.add("length");
        final JavaScriptConfiguration jsConfig = JavaScriptConfiguration.getInstance(BrowserVersion.FIREFOX_3);
        for (final String name : jsConfig.getClassConfiguration(getClassName()).functionKeys()) {
            idList.add(name);
        }
    } else {
        idList.add("length");

        int index = 0;
        for (final Object next : elements) {
            if (next instanceof HtmlElement) {
                final HtmlElement element = (HtmlElement) next;
                final String name = element.getAttribute("name");
                if (name != HtmlElement.ATTRIBUTE_NOT_DEFINED) {
                    idList.add(name);
                } else {
                    final String id = element.getId();
                    if (id != HtmlElement.ATTRIBUTE_NOT_DEFINED) {
                        idList.add(id);
                    } else {
                        idList.add(Integer.toString(index));
                    }
                }
                index++;
            } else if (next instanceof WebWindow) {
                final WebWindow window = (WebWindow) next;
                final String windowName = window.getName();
                if (windowName != null) {
                    idList.add(windowName);
                }
            } else if (LOG.isDebugEnabled()) {
                LOG.debug("Unrecognized type in array: \"" + next.getClass().getName() + "\"");
            }
        }
    }
    return idList.toArray();
}

From source file:ddf.catalog.source.solr.SolrMetacardClientImpl.java

@Override
public void deleteByIds(String fieldName, List<? extends Serializable> identifiers, boolean forceCommit)
        throws IOException, SolrServerException {
    if (CollectionUtils.isEmpty(identifiers)) {
        return;/*  w  ww . ja  v a  2 s.  c  o  m*/
    }

    if (Metacard.ID.equals(fieldName)) {
        CollectionUtils.transform(identifiers, Object::toString);
        client.deleteById((List<String>) identifiers);
    } else {
        if (identifiers.size() < SolrCatalogProvider.MAX_BOOLEAN_CLAUSES) {
            client.deleteByQuery(getIdentifierQuery(fieldName, identifiers));
        } else {
            int i;
            for (i = SolrCatalogProvider.MAX_BOOLEAN_CLAUSES; i < identifiers
                    .size(); i += SolrCatalogProvider.MAX_BOOLEAN_CLAUSES) {
                client.deleteByQuery(getIdentifierQuery(fieldName,
                        identifiers.subList(i - SolrCatalogProvider.MAX_BOOLEAN_CLAUSES, i)));
            }
            client.deleteByQuery(getIdentifierQuery(fieldName,
                    identifiers.subList(i - SolrCatalogProvider.MAX_BOOLEAN_CLAUSES, identifiers.size())));
        }
    }

    if (forceCommit) {
        client.commit();
    }
}