Search in sources :

Example 6 with TunableSetter

use of org.cytoscape.work.TunableSetter in project cytoscape-impl by cytoscape.

the class LoadVizmapFileTaskFactoryTest method testObserver.

@Test
public void testObserver() throws Exception {
    VizmapReaderManager vizmapReaderMgr = mock(VizmapReaderManager.class);
    VizmapReader reader = mock(VizmapReader.class);
    when(vizmapReaderMgr.getReader(any(URI.class), any(String.class))).thenReturn(reader);
    VisualMappingManager vmMgr = mock(VisualMappingManager.class);
    SynchronousTaskManager<?> syncTaskManager = mock(SynchronousTaskManager.class);
    SyncTunableMutatorFactory mutatorFactory = new SyncTunableMutatorFactory(new SyncTunableHandlerFactory());
    TunableRecorderManager recorderManager = new TunableRecorderManager();
    TunableSetter tunableSetter = new TunableSetterImpl(mutatorFactory, recorderManager);
    CyServiceRegistrar serviceRegistrar = mock(CyServiceRegistrar.class);
    when(serviceRegistrar.getService(VizmapReaderManager.class)).thenReturn(vizmapReaderMgr);
    when(serviceRegistrar.getService(VisualMappingManager.class)).thenReturn(vmMgr);
    when(serviceRegistrar.getService(SynchronousTaskManager.class)).thenReturn(syncTaskManager);
    when(serviceRegistrar.getService(TunableSetter.class)).thenReturn(tunableSetter);
    LoadVizmapFileTaskFactory factory = new LoadVizmapFileTaskFactoryImpl(serviceRegistrar);
    File file = new File("");
    TaskObserver observer = mock(TaskObserver.class);
    TaskIterator iterator = factory.createTaskIterator(file, observer);
    TaskMonitor taskMonitor = mock(TaskMonitor.class);
    while (iterator.hasNext()) {
        Task t = iterator.next();
        t.run(taskMonitor);
        if (t instanceof ObservableTask)
            observer.taskFinished((ObservableTask) t);
    }
    verify(observer, times(1)).taskFinished(any(ObservableTask.class));
}
Also used : Task(org.cytoscape.work.Task) ObservableTask(org.cytoscape.work.ObservableTask) SyncTunableHandlerFactory(org.cytoscape.work.internal.sync.SyncTunableHandlerFactory) VizmapReader(org.cytoscape.io.read.VizmapReader) URI(java.net.URI) CyServiceRegistrar(org.cytoscape.service.util.CyServiceRegistrar) VizmapReaderManager(org.cytoscape.io.read.VizmapReaderManager) TunableSetter(org.cytoscape.work.TunableSetter) LoadVizmapFileTaskFactory(org.cytoscape.task.read.LoadVizmapFileTaskFactory) TaskObserver(org.cytoscape.work.TaskObserver) ObservableTask(org.cytoscape.work.ObservableTask) TunableRecorderManager(org.cytoscape.work.internal.sync.TunableRecorderManager) TaskIterator(org.cytoscape.work.TaskIterator) TaskMonitor(org.cytoscape.work.TaskMonitor) SyncTunableMutatorFactory(org.cytoscape.work.internal.sync.SyncTunableMutatorFactory) VisualMappingManager(org.cytoscape.view.vizmap.VisualMappingManager) TunableSetterImpl(org.cytoscape.work.internal.sync.TunableSetterImpl) File(java.io.File) Test(org.junit.Test)

Example 7 with TunableSetter

use of org.cytoscape.work.TunableSetter in project PhenomeScape by soulj.

the class PhenomeExpress method run.

@Override
public void run(TaskMonitor taskMonitor) throws Exception {
    taskMonitor.setTitle("Running PhenomeExpress");
    taskMonitor.setStatusMessage("Preparing Networks");
    long time = System.currentTimeMillis();
    inputCheck();
    setUpNetworks();
    double[] relativeWeighting = { 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 };
    double[] alphas = { 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 };
    double[] piValues = proteinNetwork.getPivalues();
    Map<String, Integer> phenotypeMap = phenomeNetwork.getName2IndexMap();
    double[] phenoArray = new double[phenomeNetwork.getName2IndexMap().size()];
    selectedPhenotypes = new ArrayList<Phenotype>();
    boolean first = true;
    for (Phenotype phenotype : phenotypes) {
        if (phenotype.getSelected()) {
            if (first) {
                phenotypeSelected = phenotype.getID();
                phenotypeNamesSelected = phenotype.getName();
                selectedPhenotypes.add(phenotype);
                phenoArray[phenotypeMap.get(phenotype.getID())] = 1.0;
                first = false;
            } else {
                phenotypeSelected = phenotypeSelected + " ," + phenotype.getID();
                phenotypeNamesSelected = phenotypeNamesSelected + " ," + phenotype.getName();
                phenoArray[phenotypeMap.get(phenotype.getID())] = 1.0;
                selectedPhenotypes.add(phenotype);
            }
        }
    }
    Map<CyNode, Integer> map = proteinNetwork.getNode2IndexMap();
    int size = proteinNetwork.getNetwork().getNodeCount();
    int[] occuranceArray = new int[size];
    HashMap<CyNode, Protein> node2Protein = new HashMap<CyNode, Protein>();
    if (cancelled) {
        return;
    }
    taskMonitor.setStatusMessage("Calculating Activity Scores");
    taskMonitor.setProgress(0.0);
    double progress = 0.0;
    SparseRowMatrix adjMatrix = SparseRowMatrix.create(size, size);
    double[] transitionProbs = { 0.5, 0.6, 0.7, 0.8 };
    for (double transitionProb : transitionProbs) {
        TransitionMatrix transitionMatrix = new TransitionMatrix(phenomeNetwork.normalise(transitionProb, phenoGeneNetwork), proteinNetwork.normalise(transitionProb, phenoGeneNetwork), phenoGeneNetwork.normaliseT(transitionProb), phenoGeneNetwork.normalise(transitionProb));
        for (double weighting : relativeWeighting) {
            if (cancelled) {
                return;
            }
            transitionMatrix.prepareVector(piValues, phenoArray, weighting);
            for (double alpha : alphas) {
                progress += 0.0051;
                taskMonitor.setProgress(progress);
                double[] pageRankResults = transitionMatrix.pageRank(alpha);
                HashMap<Integer, Protein> index2ProteinMap = proteinNetwork.getIndex2ProteinMap();
                for (int i = 0; i < index2ProteinMap.size(); i++) {
                    Protein protein = index2ProteinMap.get(i);
                    protein.setValue(pageRankResults[i]);
                }
                List<Protein> proteinList = new ArrayList<Protein>(index2ProteinMap.values());
                Collections.sort(proteinList, new DescendingScoreComparator());
                int rank = 0;
                for (Protein protein : proteinList) {
                    rank++;
                    protein.setRank(rank);
                    node2Protein.put(protein.getNode(), protein);
                }
                Collections.sort(proteinList, new DescendingScoreComparator());
                GIGA giga = new GIGA(proteinList, node2Protein, proteinNetwork, maxNetworkSize);
                giga.compute();
                for (GIGACluster cluster : giga.getClusters()) {
                    for (CyNode node : cluster.getCluster()) {
                        Integer index = map.get(node);
                        AVector row = adjMatrix.getRow(index);
                        if (!row.isMutable()) {
                            row = row.mutable();
                        }
                        for (CyNode node2 : cluster.getCluster()) {
                            Integer index2 = map.get(node2);
                            row.set(index2, row.get(index2) + 1.0);
                            occuranceArray[index2]++;
                        }
                        adjMatrix.replaceRow(index, row);
                    }
                }
            }
        }
    }
    if (cancelled) {
        return;
    }
    taskMonitor.setProgress(1.0);
    taskMonitor.setStatusMessage("Creating Consensus Subnetworks");
    AMatrix cooccuranceMat = adjMatrix.transposeInnerProduct(adjMatrix);
    for (int i = 0; i < size; i++) {
        AVector row = cooccuranceMat.getRow(i);
        if (row.elementSum() > 0) {
            row.divide(occuranceArray[i]);
            Index nonSparse = row.nonSparseIndex();
            for (int j = 0; j < nonSparse.length(); j++) {
                if (row.get(nonSparse.get(j)) < 1.0) {
                    row.set(nonSparse.get(j), 0.0);
                }
            }
            cooccuranceMat.replaceRow(i, row.sparse());
        }
    }
    ConnectedComponentAnalyser CCA = new ConnectedComponentAnalyser(node2Protein);
    ArrayList<PhenomeExpressSubnetwork> subnetworks = CCA.ccFromAdjMatrix(adjMatrix, proteinNetwork);
    Iterator<PhenomeExpressSubnetwork> it = subnetworks.iterator();
    while (it.hasNext()) {
        PhenomeExpressSubnetwork subnetwork = it.next();
        if (subnetwork.getNodeList().size() < this.minNetSize) {
            it.remove();
        }
    }
    if (cancelled) {
        return;
    }
    taskMonitor.setStatusMessage("Calculating Subnetwork Significance");
    subnetworksSignificance(subnetworks);
    taskMonitor.setStatusMessage("Visualising Subnetworks");
    List<Double> foldChangeValues = proteinNetwork.getNodeTable().getColumn(foldChange).getValues(Double.class);
    double fcMax = Collections.max(foldChangeValues);
    double fcMin = Collections.min(foldChangeValues);
    // Apply the visual style to a NetworkView
    VizStyle vizStyle = new VizStyle();
    VisualStyle vs = vizStyle.createVizStyle(cyServiceRegistrar, geneName, foldChange, fcMax, fcMin);
    this.property = getNodeLabelPositionProperty();
    vs.setDefaultValue(property, property.parseSerializableString("N,S,c,0.0,0.0"));
    VisualMappingManager visualMappingManager = cyServiceRegistrar.getService(VisualMappingManager.class);
    visualMappingManager.setCurrentVisualStyle(vs);
    ArrayList<CyNetworkView> networkViewList = new ArrayList<CyNetworkView>();
    Iterator<PhenomeExpressSubnetwork> it2 = subnetworks.iterator();
    CyNetworkManager networkManager = cyServiceRegistrar.getService(CyNetworkManager.class);
    CyNetworkViewFactory networkViewFactory = cyServiceRegistrar.getService(CyNetworkViewFactory.class);
    CyLayoutAlgorithmManager layoutManager = cyServiceRegistrar.getService(CyLayoutAlgorithmManager.class);
    CyApplicationManager cyApplicationManager = cyServiceRegistrar.getService(CyApplicationManager.class);
    CyLayoutAlgorithm layout = layoutManager.getLayout("force-directed");
    Map<String, Object> settings = new HashMap<String, Object>();
    settings.put("defaultSpringLength", 5.0);
    settings.put("defaultSpringNodeMass", 2.0);
    TunableSetter setter = cyServiceRegistrar.getService(TunableSetter.class);
    Object context = layout.createLayoutContext();
    setter.applyTunables(context, settings);
    GOTermAnalyser2 goTermAnalyser = new GOTermAnalyser2(proteinNetwork, species);
    while (it2.hasNext()) {
        PhenomeExpressSubnetwork subnet = it2.next();
        if (subnet.getPvalue() > this.threshold) {
            it2.remove();
        } else {
            goTermAnalyser.calculateGOTermPValues(subnet, proteinNetwork);
            String subnetworkName = NetworkUtils.getUniqueNetworkName(cyServiceRegistrar, networkName + "_" + subnet.getBestGOTerm());
            subnet.setName(subnetworkName);
            CySubNetwork subnetwork = NetworkUtils.createSubNetwork(((CySubNetwork) proteinNetwork.getNetwork()).getRootNetwork(), subnet.getNodeList());
            subnetwork.getRow(subnetwork).set(CyNetwork.NAME, subnetworkName);
            addSeedPhenotypes(subnetwork);
            networkManager.addNetwork(subnetwork);
            CyNetworkViewManager viewManager = cyServiceRegistrar.getService(CyNetworkViewManager.class);
            CyNetworkView nv = networkViewFactory.createNetworkView(subnetwork);
            viewManager.addNetworkView(nv);
            cyApplicationManager.setCurrentNetworkView(nv);
            for (CyNode phenoNode : phenotypesAdded) {
                View<CyNode> nodeView = nv.getNodeView(phenoNode);
                nodeView.setLockedValue(BasicVisualLexicon.NODE_SHAPE, NodeShapeVisualProperty.RECTANGLE);
                nodeView.setLockedValue(BasicVisualLexicon.NODE_FILL_COLOR, Color.BLUE);
            }
            for (CyEdge phenoEdge : phenoEdges) {
                View<CyEdge> edgeView = nv.getEdgeView(phenoEdge);
                edgeView.setLockedValue(BasicVisualLexicon.EDGE_LINE_TYPE, LineTypeVisualProperty.DOT);
                edgeView.setLockedValue(BasicVisualLexicon.EDGE_PAINT, Color.BLUE);
            }
            networkViewList.add(nv);
            Set<View<CyNode>> nodeSet = Collections.emptySet();
            cyServiceRegistrar.getService(TaskManager.class).execute(layout.createTaskIterator(nv, context, nodeSet, null));
        }
    }
    CyEventHelper cyEventHelper = cyServiceRegistrar.getService(CyEventHelper.class);
    for (CyNetworkView nv : networkViewList) {
        visualMappingManager.setVisualStyle(vs, nv);
        vs.apply(nv);
        cyEventHelper.flushPayloadEvents();
        cyApplicationManager.setCurrentNetworkView(nv);
        nv.updateView();
    }
    phenomeNetwork = null;
    proteinNetwork = null;
    ResultsPanel resultsPanel = (ResultsPanel) CytoPanelUtils.getCytoPanel(cyServiceRegistrar, ResultsPanel.class, CytoPanelName.EAST);
    resultsPanel.newTableData(getResultsSummary(subnetworks), getParameterList());
    CytoPanelUtils.showCytoPanel(cyServiceRegistrar, CytoPanelName.EAST);
}
Also used : CyEventHelper(org.cytoscape.event.CyEventHelper) HashMap(java.util.HashMap) AMatrix(mikera.matrixx.AMatrix) ArrayList(java.util.ArrayList) Index(mikera.indexz.Index) TunableSetter(org.cytoscape.work.TunableSetter) AVector(mikera.vectorz.AVector) CyNode(org.cytoscape.model.CyNode) GOTermAnalyser2(org.cytoscape.phenomescape.internal.util.GOTermAnalyser2) CyNetworkViewManager(org.cytoscape.view.model.CyNetworkViewManager) CyApplicationManager(org.cytoscape.application.CyApplicationManager) Phenotype(org.cytoscape.phenomescape.internal.util.Phenotype) CyNetworkManager(org.cytoscape.model.CyNetworkManager) CyNetworkViewFactory(org.cytoscape.view.model.CyNetworkViewFactory) CyLayoutAlgorithmManager(org.cytoscape.view.layout.CyLayoutAlgorithmManager) ConnectedComponentAnalyser(org.cytoscape.phenomescape.internal.util.ConnectedComponentAnalyser) DescendingScoreComparator(org.cytoscape.phenomescape.internal.Protein.DescendingScoreComparator) CyLayoutAlgorithm(org.cytoscape.view.layout.CyLayoutAlgorithm) VisualStyle(org.cytoscape.view.vizmap.VisualStyle) VizStyle(org.cytoscape.phenomescape.internal.util.VizStyle) CyEdge(org.cytoscape.model.CyEdge) View(org.cytoscape.view.model.View) CyNetworkView(org.cytoscape.view.model.CyNetworkView) SparseRowMatrix(mikera.matrixx.impl.SparseRowMatrix) TaskManager(org.cytoscape.work.TaskManager) VisualMappingManager(org.cytoscape.view.vizmap.VisualMappingManager) CyNetworkView(org.cytoscape.view.model.CyNetworkView) CySubNetwork(org.cytoscape.model.subnetwork.CySubNetwork)

Aggregations

TunableSetter (org.cytoscape.work.TunableSetter)7 HashMap (java.util.HashMap)5 CyApplicationManager (org.cytoscape.application.CyApplicationManager)3 VisualMappingManager (org.cytoscape.view.vizmap.VisualMappingManager)3 TaskIterator (org.cytoscape.work.TaskIterator)3 ArrayList (java.util.ArrayList)2 CyEventHelper (org.cytoscape.event.CyEventHelper)2 CyNetworkManager (org.cytoscape.model.CyNetworkManager)2 CyServiceRegistrar (org.cytoscape.service.util.CyServiceRegistrar)2 CyLayoutAlgorithmManager (org.cytoscape.view.layout.CyLayoutAlgorithmManager)2 CyNetworkViewFactory (org.cytoscape.view.model.CyNetworkViewFactory)2 CyNetworkViewManager (org.cytoscape.view.model.CyNetworkViewManager)2 File (java.io.File)1 URI (java.net.URI)1 Index (mikera.indexz.Index)1 AMatrix (mikera.matrixx.AMatrix)1 SparseRowMatrix (mikera.matrixx.impl.SparseRowMatrix)1 AVector (mikera.vectorz.AVector)1 CyGroupFactory (org.cytoscape.group.CyGroupFactory)1 CyGroupManager (org.cytoscape.group.CyGroupManager)1