Search in sources :

Example 1 with RankValue

use of org.baderlab.csplugins.enrichmentmap.view.heatmap.table.RankValue in project EnrichmentMapApp by BaderLab.

the class GSEALeadingEdgeRankingOption method computeRanking.

@Override
public CompletableFuture<Optional<Map<Integer, RankValue>>> computeRanking(Collection<Integer> genes) {
    initializeLeadingEdge();
    int topRank = getTopRank();
    boolean isNegative = isNegativeGS();
    Map<Integer, GeneExpression> expressions = dataset.getExpressionSets().getExpressionMatrix();
    Ranking ranking = dataset.getExpressionSets().getRanksByName(rankingName);
    Integer[] ranksSubset = new Integer[expressions.size()];
    HashMap<Integer, ArrayList<Integer>> rank2keys = new HashMap<Integer, ArrayList<Integer>>();
    int n = 0;
    Map<Integer, Rank> currentRanks = ranking.getRanking();
    for (Integer key : expressions.keySet()) {
        if (currentRanks.containsKey(key)) {
            ranksSubset[n] = currentRanks.get(key).getRank();
        } else {
            ranksSubset[n] = -1;
        }
        rank2keys.computeIfAbsent(ranksSubset[n], k -> new ArrayList<>()).add(key);
        n++;
    }
    Map<Integer, RankValue> result = new HashMap<>();
    int previous = -1;
    boolean significant = false;
    for (int m = 0; m < ranksSubset.length; m++) {
        //if the current gene doesn't have a rank then don't show it
        if (ranksSubset[m] == -1)
            continue;
        if (ranksSubset[m] == previous)
            continue;
        previous = ranksSubset[m];
        significant = false;
        if (ranksSubset[m] <= topRank && !isNegative && topRank != 0 && topRank != -1)
            significant = true;
        else if (ranksSubset[m] >= topRank && isNegative && topRank != 0 && topRank != -1)
            significant = true;
        List<Integer> keys = rank2keys.get(ranksSubset[m]);
        for (Integer key : keys) {
            Rank rank = currentRanks.get(key);
            result.put(key, new RankValue(rank.getRank(), rank.getScore(), significant));
        }
    }
    // Remove genes that we don't need
    result.keySet().retainAll(genes);
    BasicRankingOption.normalizeRanks(result);
    return CompletableFuture.completedFuture(Optional.of(result));
}
Also used : Method(org.baderlab.csplugins.enrichmentmap.model.EMDataSet.Method) GeneExpression(org.baderlab.csplugins.enrichmentmap.model.GeneExpression) DetermineEnrichmentResultFileReader(org.baderlab.csplugins.enrichmentmap.parsers.DetermineEnrichmentResultFileReader) Collection(java.util.Collection) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Ranking(org.baderlab.csplugins.enrichmentmap.model.Ranking) GSEAResult(org.baderlab.csplugins.enrichmentmap.model.GSEAResult) ArrayList(java.util.ArrayList) List(java.util.List) EMDataSet(org.baderlab.csplugins.enrichmentmap.model.EMDataSet) SwingUtil(org.baderlab.csplugins.enrichmentmap.view.util.SwingUtil) Map(java.util.Map) Rank(org.baderlab.csplugins.enrichmentmap.model.Rank) Optional(java.util.Optional) RankValue(org.baderlab.csplugins.enrichmentmap.view.heatmap.table.RankValue) EnrichmentResult(org.baderlab.csplugins.enrichmentmap.model.EnrichmentResult) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Rank(org.baderlab.csplugins.enrichmentmap.model.Rank) RankValue(org.baderlab.csplugins.enrichmentmap.view.heatmap.table.RankValue) Ranking(org.baderlab.csplugins.enrichmentmap.model.Ranking) GeneExpression(org.baderlab.csplugins.enrichmentmap.model.GeneExpression)

Example 2 with RankValue

use of org.baderlab.csplugins.enrichmentmap.view.heatmap.table.RankValue in project EnrichmentMapApp by BaderLab.

the class HeatMapRanksTest method testLeadingEdge.

@Test
public void testLeadingEdge(EnrichmentMapManager emManager) throws Exception {
    final String geneSetName = "ENVELOPE%GO%GO:0031975";
    final int leadingEdgeSize = 170;
    // Sanity test
    EnrichmentMap map = emManager.getAllEnrichmentMaps().values().iterator().next();
    EMDataSet dataset = map.getDataSet(LegacySupport.DATASET1);
    GeneSet gs = dataset.getGeneSetsOfInterest().getGeneSets().get(geneSetName);
    assertNotNull(gs);
    // Run the ranking
    RankingOption rankingOption = new GSEALeadingEdgeRankingOption(dataset, geneSetName, Ranking.GSEARanking);
    Map<Integer, RankValue> ranks = rankingOption.computeRanking(gs.getGenes()).get().get();
    assertEquals(454, ranks.size());
    // Convert to useful collections
    Map<RankValue, Integer> rankToGeneId = HashBiMap.create(ranks).inverse();
    List<RankValue> sortedRanks = ranks.values().stream().sorted().collect(Collectors.toList());
    // Test leading edge
    for (int i = 0; i < sortedRanks.size(); i++) {
        RankValue v = sortedRanks.get(i);
        assertTrue(v.isSignificant() == i < leadingEdgeSize);
    }
    // Test genes are the same
    List<Integer> expectedGeneOrder = getGeneOrderFromFile(map, PATH + "gene_order_leading_edge.txt");
    List<Integer> actualGeneOrder = sortedRanks.stream().map(rankToGeneId::get).collect(Collectors.toList());
    assertEquals(expectedGeneOrder, actualGeneOrder);
}
Also used : RankingOption(org.baderlab.csplugins.enrichmentmap.view.heatmap.RankingOption) GSEALeadingEdgeRankingOption(org.baderlab.csplugins.enrichmentmap.view.heatmap.GSEALeadingEdgeRankingOption) EnrichmentMap(org.baderlab.csplugins.enrichmentmap.model.EnrichmentMap) RankValue(org.baderlab.csplugins.enrichmentmap.view.heatmap.table.RankValue) GSEALeadingEdgeRankingOption(org.baderlab.csplugins.enrichmentmap.view.heatmap.GSEALeadingEdgeRankingOption) EMDataSet(org.baderlab.csplugins.enrichmentmap.model.EMDataSet) GeneSet(org.baderlab.csplugins.enrichmentmap.model.GeneSet) BaseNetworkTest(org.baderlab.csplugins.enrichmentmap.task.BaseNetworkTest) Test(org.junit.Test)

Example 3 with RankValue

use of org.baderlab.csplugins.enrichmentmap.view.heatmap.table.RankValue in project EnrichmentMapApp by BaderLab.

the class HeatMapMainPanel method updateSetting_RankOption.

public void updateSetting_RankOption(RankingOption rankOption) {
    selectedRankOption = rankOption;
    List<String> genes = getGenes(getOperator());
    HeatMapTableModel tableModel = (HeatMapTableModel) table.getModel();
    EnrichmentMap map = tableModel.getEnrichmentMap();
    List<Integer> geneIds = genes.stream().map(map::getHashFromGene).collect(Collectors.toList());
    CompletableFuture<Optional<Map<Integer, RankValue>>> rankingFuture = rankOption.computeRanking(geneIds);
    if (rankingFuture != null) {
        rankingFuture.whenComplete((ranking, ex) -> {
            if (ranking.isPresent()) {
                tableModel.setRanking(rankOption.getName(), ranking.get());
                table.getColumnModel().getColumn(HeatMapTableModel.RANK_COL).setHeaderValue(rankOption);
            } else {
                tableModel.setRanking(rankOption.getName(), null);
                table.getColumnModel().getColumn(HeatMapTableModel.RANK_COL).setHeaderValue(new RankOptionErrorHeader(rankOption));
            }
            table.getTableHeader().repaint();
        });
    }
    settingChanged();
}
Also used : Optional(java.util.Optional) HeatMapTableModel(org.baderlab.csplugins.enrichmentmap.view.heatmap.table.HeatMapTableModel) EnrichmentMap(org.baderlab.csplugins.enrichmentmap.model.EnrichmentMap) RankValue(org.baderlab.csplugins.enrichmentmap.view.heatmap.table.RankValue) RankOptionErrorHeader(org.baderlab.csplugins.enrichmentmap.view.heatmap.table.RankOptionErrorHeader)

Example 4 with RankValue

use of org.baderlab.csplugins.enrichmentmap.view.heatmap.table.RankValue in project EnrichmentMapApp by BaderLab.

the class BasicRankingOption method computeRanking.

@Override
public CompletableFuture<Optional<Map<Integer, RankValue>>> computeRanking(Collection<Integer> genes) {
    Map<Integer, RankValue> result = new HashMap<>();
    for (Map.Entry<Integer, Rank> entry : ranking.getRanking().entrySet()) {
        Rank rank = entry.getValue();
        result.put(entry.getKey(), new RankValue(rank.getRank(), rank.getScore(), false));
    }
    // Remove genes that we don't need
    result.keySet().retainAll(genes);
    normalizeRanks(result);
    return CompletableFuture.completedFuture(Optional.of(result));
}
Also used : HashMap(java.util.HashMap) Rank(org.baderlab.csplugins.enrichmentmap.model.Rank) HashMap(java.util.HashMap) Map(java.util.Map) RankValue(org.baderlab.csplugins.enrichmentmap.view.heatmap.table.RankValue)

Example 5 with RankValue

use of org.baderlab.csplugins.enrichmentmap.view.heatmap.table.RankValue in project EnrichmentMapApp by BaderLab.

the class ClusterRankingOption method computeRanking.

@Override
public CompletableFuture<Optional<Map<Integer, RankValue>>> computeRanking(Collection<Integer> genes) {
    if (genes.size() < 2) {
        // The HierarchicalClusterTask requires at least 2 genes
        return CompletableFuture.completedFuture(Optional.of(Collections.emptyMap()));
    }
    HierarchicalClusterTask task = new HierarchicalClusterTask(map, genes, distance.getMetric());
    CompletableFuture<Optional<Map<Integer, RankValue>>> future = new CompletableFuture<>();
    taskManager.execute(new TaskIterator(task), new TaskObserver() {

        @Override
        public void taskFinished(ObservableTask task) {
            if (task instanceof HierarchicalClusterTask) {
                HierarchicalClusterTask clusterTask = (HierarchicalClusterTask) task;
                Optional<Map<Integer, RankValue>> ranking = clusterTask.getActualResults();
                future.complete(ranking);
            }
        }

        @Override
        public void allFinished(FinishStatus finishStatus) {
            // Don't see why this would ever happen
            if (!future.isDone()) {
                future.completeExceptionally(new RuntimeException("Failed"));
            }
        }
    });
    return future;
}
Also used : TaskObserver(org.cytoscape.work.TaskObserver) ObservableTask(org.cytoscape.work.ObservableTask) CompletableFuture(java.util.concurrent.CompletableFuture) TaskIterator(org.cytoscape.work.TaskIterator) Optional(java.util.Optional) FinishStatus(org.cytoscape.work.FinishStatus) RankValue(org.baderlab.csplugins.enrichmentmap.view.heatmap.table.RankValue) HierarchicalClusterTask(org.baderlab.csplugins.enrichmentmap.task.cluster.HierarchicalClusterTask)

Aggregations

RankValue (org.baderlab.csplugins.enrichmentmap.view.heatmap.table.RankValue)6 HashMap (java.util.HashMap)3 Optional (java.util.Optional)3 EMDataSet (org.baderlab.csplugins.enrichmentmap.model.EMDataSet)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 EnrichmentMap (org.baderlab.csplugins.enrichmentmap.model.EnrichmentMap)2 GeneExpression (org.baderlab.csplugins.enrichmentmap.model.GeneExpression)2 Rank (org.baderlab.csplugins.enrichmentmap.model.Rank)2 Collection (java.util.Collection)1 List (java.util.List)1 AvgLinkHierarchicalClustering (org.baderlab.csplugins.brainlib.AvgLinkHierarchicalClustering)1 DistanceMatrix (org.baderlab.csplugins.brainlib.DistanceMatrix)1 Method (org.baderlab.csplugins.enrichmentmap.model.EMDataSet.Method)1 EnrichmentResult (org.baderlab.csplugins.enrichmentmap.model.EnrichmentResult)1 GSEAResult (org.baderlab.csplugins.enrichmentmap.model.GSEAResult)1 GeneExpressionMatrix (org.baderlab.csplugins.enrichmentmap.model.GeneExpressionMatrix)1 GeneSet (org.baderlab.csplugins.enrichmentmap.model.GeneSet)1 Ranking (org.baderlab.csplugins.enrichmentmap.model.Ranking)1