Search in sources :

Example 1 with Edges

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.Edges in project clusterMaker2 by RBVI.

the class CyMatrixFactory method makeMatrix.

public static CyMatrix makeMatrix(CyNetwork network, String[] attributes, boolean selectedOnly, boolean ignoreMissing, boolean transpose, boolean assymetric, MatrixType type) {
    // Create our local copy of the weightAtributes array
    String[] attributeArray = new String[attributes.length];
    if (attributes.length >= 1 && attributes[0].startsWith("node.")) {
        // Get rid of the leading type information
        for (int i = 0; i < attributes.length; i++) {
            attributeArray[i] = attributes[i].substring(5);
        }
        List<CyNode> nodeList = ModelUtils.getSortedNodeList(network, selectedOnly);
        Map<CyNode, Map<String, Double>> nodeCondMap = getNodeCondMap(network, nodeList, attributeArray, ignoreMissing);
        CyMatrix matrix = makeTypedMatrix(network, nodeCondMap.size(), attributeArray.length, transpose, type);
        matrix.setAssymetricalEdge(false);
        return makeAttributeMatrix(network, matrix, nodeList, nodeCondMap, attributeArray, transpose);
    } else if (attributes.length == 1 && attributes[0].startsWith("edge.")) {
        String weight = attributes[0].substring(5);
        if (!assymetric) {
            // Get the list of nodes and edges of interest
            List<CyEdge> edgeList = getEdgeList(network, selectedOnly);
            List<CyNode> nodeList = getNodesFromEdges(network, edgeList, weight, ignoreMissing);
            Collections.sort(nodeList, new CyIdentifiableNameComparator(network));
            CyMatrix matrix = makeTypedMatrix(network, nodeList.size(), nodeList.size(), false, type);
            matrix.setAssymetricalEdge(false);
            return makeSymmetricalMatrix(network, matrix, nodeList, edgeList, weight);
        } else {
            List<CyEdge> edgeList = getEdgeList(network, selectedOnly);
            List<CyNode> targetNodeList = new ArrayList<CyNode>();
            List<CyNode> sourceNodeList = getNodesFromEdges(network, edgeList, targetNodeList, weight, ignoreMissing);
            Collections.sort(targetNodeList, new CyIdentifiableNameComparator(network));
            Collections.sort(sourceNodeList, new CyIdentifiableNameComparator(network));
            CyMatrix matrix = makeTypedMatrix(network, sourceNodeList.size(), targetNodeList.size(), false, type);
            matrix.setAssymetricalEdge(true);
            return makeAssymmetricalMatrix(network, matrix, sourceNodeList, targetNodeList, edgeList, weight);
        }
    }
    return null;
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) CyIdentifiableNameComparator(edu.ucsf.rbvi.clusterMaker2.internal.utils.CyIdentifiableNameComparator) CyNode(org.cytoscape.model.CyNode) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Edges

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.Edges in project clusterMaker2 by RBVI.

the class RunTransClust method run.

public List<NodeCluster> run(TaskMonitor monitor, CyNetwork network) {
    DoubleMatrix2D matrix = this.distanceMatrix.getColtMatrix();
    nodes = distanceMatrix.getRowNodes();
    HashMap<String, CyNode> nodeHash = new HashMap<String, CyNode>();
    for (CyNode node : nodes) {
        nodeHash.put(ModelUtils.getNodeName(network, node), node);
    }
    HashMap<String, Integer> integers2proteins = new HashMap<String, Integer>();
    HashMap<Integer, String> proteins2integers = new HashMap<Integer, String>();
    int count = 0;
    for (CyNode node : this.nodes) {
        integers2proteins.put(ModelUtils.getNodeName(network, node), count);
        proteins2integers.put(count, ModelUtils.getNodeName(network, node));
        count++;
    }
    Edges es = new Edges(this.nodes.size() * this.nodes.size(), this.nodes.size());
    count = 0;
    for (int i = 0; i < this.nodes.size(); i++) {
        CyNode cyNodeI = this.nodes.get(i);
        es.startPositions[integers2proteins.get(cyNodeI.getSUID())] = count;
        for (int j = 0; j < this.nodes.size(); j++) {
            CyNode cyNodeJ = this.nodes.get(j);
            es.sources[count] = i;
            es.targets[count] = j;
            Double val = distanceMatrix.getValue(i, j);
            if (val != null) {
                es.values[count] = val.floatValue();
                count++;
            }
        }
        es.endPositions[integers2proteins.get(cyNodeI.getSUID())] = count - 1;
    }
    Semaphore s = new Semaphore(1);
    TaskConfig.mode = TaskConfig.COMPARISON_MODE;
    TaskConfig.monitor = monitor;
    IteratorThread it = new IteratorThread(es, integers2proteins, proteins2integers, s);
    TaskConfig.minThreshold = threshold;
    TaskConfig.maxThreshold = threshold;
    try {
        s.acquire();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    it.start();
    monitor.showMessage(TaskMonitor.Level.INFO, "Executing TransClust Clustering...");
    try {
        s.acquire();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    monitor.showMessage(TaskMonitor.Level.INFO, "Assigning nodes to clusters");
    String result = it.resultsStringBuffer.toString();
    String[] clusters = result.split("\t")[2].split(";");
    Map<Integer, NodeCluster> clusterMap = getClusterMap(clusters, nodeHash);
    // Update node attributes in network to include clusters. Create cygroups from clustered nodes
    monitor.showMessage(TaskMonitor.Level.INFO, "Created " + clusterMap.size() + " clusters");
    if (clusterCount == 0) {
        monitor.showMessage(TaskMonitor.Level.ERROR, "Created 0 clusters!!!!");
        return null;
    }
    int clusterNumber = 1;
    Map<NodeCluster, NodeCluster> cMap = new HashMap();
    for (NodeCluster cluster : NodeCluster.sortMap(clusterMap)) {
        if (cMap.containsKey(cluster))
            continue;
        cMap.put(cluster, cluster);
        cluster.setClusterNumber(clusterNumber);
        clusterNumber++;
    }
    Set<NodeCluster> clusters2 = cMap.keySet();
    return new ArrayList<NodeCluster>(clusters2);
}
Also used : HashMap(java.util.HashMap) IteratorThread(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.iterativeclustering.IteratorThread) ArrayList(java.util.ArrayList) Semaphore(java.util.concurrent.Semaphore) Edges(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.Edges) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) DoubleMatrix2D(cern.colt.matrix.tdouble.DoubleMatrix2D) CyNode(org.cytoscape.model.CyNode)

Example 3 with Edges

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.Edges in project clusterMaker2 by RBVI.

the class RunSCPS method getSMat.

// Get Connected Components, cluster all components <= |5|, and connect the remaining components with random lowscoring edges
public DoubleMatrix2D getSMat(CyMatrix distanceMatrix) {
    // Matrix prior to filtration modification
    DoubleMatrix2D unfiltered_mat = distanceMatrix.getColtMatrix();
    // Size of newly created Umat after filtering of small components
    int sMat_rows = 0;
    HashMap<Integer, List<CyNode>> filtered_cmap = new HashMap<Integer, List<CyNode>>();
    // Connected Componets
    Map<Integer, List<CyNode>> cMap = MatrixUtils.findConnectedComponents(distanceMatrix);
    IntArrayList rowList = new IntArrayList();
    IntArrayList columnList = new IntArrayList();
    DoubleArrayList valueList = new DoubleArrayList();
    // Iterate through connected components
    int component_size_sum = 0;
    for (List<CyNode> component : cMap.values()) {
        numComponents += 1;
        // Size <= 5. Automatically create cluster and increment clusterCount.
        if (component.size() <= 5) {
            NodeCluster iCluster = new NodeCluster(component);
            iCluster.setClusterNumber(this.clusterCount);
            // iCluster.add(component,this.clusterCount);
            this.clusterMap.put(new Integer(clusterCount), iCluster);
            this.clusterCount++;
        } else {
            // iterate through components and assign them index mappings in new uMatrix
            component_size_sum += component.size();
            System.out.println("Normal Component size " + component.size() + " Total Sum " + component_size_sum);
            for (int i = 0; i < component.size(); i++) {
                CyNode n = component.get(i);
                int node_id = this.nodes.indexOf(n);
                // set mapping of new matrix index to old index
                setMap(node_id, sMat_rows);
                sMat_rows++;
            }
        }
    }
    DoubleMatrix2D sMat = DoubleFactory2D.sparse.make(sMat_rows, sMat_rows);
    // set diagnols of sMat to one
    for (int i = 0; i < sMat_rows; i++) sMat.set(i, i, 1);
    // iterate through nonzero edges. If both nodes in new index map, transfer the edge to new matrix
    unfiltered_mat.getNonZeros(rowList, columnList, valueList);
    for (int i = 0; i < rowList.size(); i++) {
        int row_id = rowList.get(i);
        int column_id = columnList.get(i);
        int new_row_id = getMap_new(row_id);
        int new_column_id = getMap_new(column_id);
        double value = valueList.get(i);
        // Set symmetrically the values in new matrix
        if (new_row_id > -1 && new_column_id > -1) {
            sMat.set(new_row_id, new_column_id, value);
            sMat.set(new_column_id, new_row_id, value);
        }
    }
    return sMat;
}
Also used : HashMap(java.util.HashMap) DoubleArrayList(cern.colt.list.tdouble.DoubleArrayList) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster) DoubleMatrix2D(cern.colt.matrix.tdouble.DoubleMatrix2D) DoubleArrayList(cern.colt.list.tdouble.DoubleArrayList) IntArrayList(cern.colt.list.tint.IntArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CyNode(org.cytoscape.model.CyNode) IntArrayList(cern.colt.list.tint.IntArrayList)

Example 4 with Edges

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.Edges in project clusterMaker2 by RBVI.

the class InOut method readSimilarityFile.

public static Edges readSimilarityFile(String file, HashMap<Integer, String> proteins2integers, HashMap<String, Integer> integers2proteins) throws IOException {
    BufferedReader br = new BufferedReader(new FileReader(file));
    int lineCount = 0;
    int proteinNumber = 0;
    if (!Config.createSimilarityFile) {
        // count lines and create proteins2integers HashMap
        if (Config.gui) {
            Console.println("\t start counting lines of similarity file ...");
            Console.restartBar(0, 100);
            Console.setBarText("start counting lines of similarity file");
        } else {
        }
        if (Config.fastaFile != null && TaskConfig.mode != TaskConfig.COMPARISON_MODE && TaskConfig.mode != TaskConfig.HIERARICHAL_MODE) {
            File f = new File(Config.similarityFile);
            lineCount = countLines(f);
            InOut.readFastaFile(Config.fastaFile, proteins2integers, integers2proteins);
        } else {
            try {
                while (true) {
                    String line = br.readLine();
                    String[] tokens = line.split(TAB);
                    lineCount++;
                    if (lineCount % 10000 == 0 && Config.gui)
                        Console.setBarText("start counting lines of similarity file  " + lineCount);
                    if (!integers2proteins.containsKey(tokens[0])) {
                        integers2proteins.put(tokens[0], proteinNumber);
                        proteins2integers.put(proteinNumber, tokens[0]);
                        proteinNumber++;
                    }
                    if (!integers2proteins.containsKey(tokens[1])) {
                        integers2proteins.put(tokens[1], proteinNumber);
                        proteins2integers.put(proteinNumber, tokens[1]);
                        proteinNumber++;
                    }
                }
            } catch (Exception e) {
            }
        }
        if (Config.gui)
            Console.println();
    // else System.out.println();
    } else
        lineCount = Config.linesInSimilarityFile;
    Edges es = new Edges(lineCount, proteins2integers.size());
    // set startPosition of all elements that have no outgoing edge to -1
    for (int i = 0; i < proteinNumber; i++) es.setStartPosition(i, -1);
    // reinitialise BufferedReader
    br.close();
    br = new BufferedReader(new FileReader(file));
    int i = 0;
    int currentProtein = -1;
    double percent = 0;
    // get values
    if (Config.gui) {
        Console.println("\t start reading similarity file ...");
        Console.restartBar(0, 100);
        Console.setBarText("start reading similarity file");
    }
    // else System.out.println("\t start reading similarity file ...");
    double percentOld = 0;
    // char[] buffer = new char[4096];
    // 
    // for (int charsRead = br.read(buffer); charsRead >= 0; charsRead = br.read(buffer)) {
    // 
    // for (int charIndex = 0; charIndex < charsRead ; charIndex++) {
    // 
    // if(i%100000==0&&i>0){
    // percent  = Math.rint(((double) i / lineCount)*10000)/100;
    // if(percent>percentOld+1){
    // percentOld = percent;
    // if(Config.gui){
    // Console.setBarValue((int) Math.rint(percent));
    // Console.setBarTextPlusRestTime("reading similarity file  " + percent + " %");
    // }else System.out.print( percent + " %\t" );
    // }
    // }
    // 
    // if(buffer[charIndex]=='\n') {
    // es.values[i] = Float.parseFloat(sb.toString());
    // //					 es.setValue(i, Float.parseFloat(sb.toString()));
    // //					 sb.delete(0, sb.length());
    // sb = new StringBuffer();
    // 
    // if(sou!=currentProtein){
    // 
    // currentProtein = sou;
    // es.setStartPosition(sou, i);
    // 
    // }
    // i++;
    // 
    // }else 	if(buffer[charIndex]=='\t'){
    // if(so){
    // sou = integers2proteins.get(sb.toString());
    // //						es.setSource(i, sou);
    // es.sources[i] = sou;
    // sb = new StringBuffer();
    // //						sb.delete(0, sb.length());
    // so=false;
    // }else{
    // tar = integers2proteins.get(sb.toString());
    // es.targets[i] = tar;
    // //						es.setTarget(i, tar);
    // sb = new StringBuffer();
    // //						sb.delete(0, sb.length());
    // so = true;
    // }
    // }else{
    // sb.append(buffer[charIndex]);
    // }
    // }
    // }
    String line = "";
    String[] tokens = new String[3];
    int s = -1;
    int t = 0;
    int indexOfFirstTAB;
    int indexOfSecondTAB;
    while ((line = br.readLine()) != null) {
        if (line.equals(""))
            continue;
        if (i % 100000 == 0 && i > 0) {
            percent = Math.rint(((double) i / lineCount) * 10000) / 100;
            if (percent > percentOld + 1) {
                percentOld = percent;
                if (Config.gui) {
                    Console.setBarValue((int) Math.rint(percent));
                    Console.setBarTextPlusRestTime("reading similarity file  " + percent + " %");
                } else
                    System.out.print(percent + " %\t");
            }
        }
        indexOfFirstTAB = line.indexOf('\t');
        indexOfSecondTAB = line.indexOf('\t', indexOfFirstTAB + 1);
        tokens[0] = line.substring(0, indexOfFirstTAB);
        tokens[1] = line.substring(indexOfFirstTAB + 1, indexOfSecondTAB);
        tokens[2] = line.substring(indexOfSecondTAB + 1, line.length());
        s = integers2proteins.get(tokens[0]);
        t = integers2proteins.get(tokens[1]);
        es.sources[i] = s;
        es.targets[i] = t;
        es.values[i] = Float.parseFloat(tokens[2]);
        if (es.values[i] < InOut.min)
            InOut.min = es.values[i];
        if (s != currentProtein) {
            currentProtein = s;
            es.setStartPosition(s, i);
        }
        i++;
    }
    if (Config.gui) {
        Console.println();
        Console.println("\t start sorting");
        Console.restartBar(0, 100);
        Console.setBarText("start sorting");
    } else {
    // System.out.println();
    // System.out.println("\t start sorting");
    }
    int[] positions2 = es.getStartPosition().clone();
    Arrays.sort(positions2);
    if (Config.gui) {
        Console.println();
        Console.println("\t start finding endpositions");
        Console.restartBar(0, 100);
        Console.setBarText("finding endpositions");
    } else {
    // System.out.println();
    // System.out.println("\t start finding endpositions");
    }
    percentOld = 0;
    for (int j = 0; j < positions2.length; j++) {
        if (j % 100000 == 0 && j > 0) {
            percent = Math.rint(((double) j / positions2.length) * 10000) / 100;
            if (percent > percentOld + 1) {
                percentOld = percent;
                if (Config.gui) {
                    Console.setBarValue((int) Math.rint(percent));
                    Console.setBarTextPlusRestTime("finding endpositions  " + percent + " %");
                } else
                    System.out.print(percent + " %\t");
            }
        }
        int dum = es.getStartPosition(j);
        int k = Arrays.binarySearch(positions2, dum);
        if (k + 1 < positions2.length) {
            if (dum == -1)
                es.setEndPosition(j, -1);
            else
                es.setEndPosition(j, positions2[k + 1]);
        } else
            es.setEndPosition(j, es.size());
    }
    if (Config.gui) {
        Console.println();
        Console.println("\t start normalizing");
        Console.restartBarTimer();
        Console.setBarValue(0);
        Console.setBarText("normalizing");
    } else {
    // System.out.println();
    // System.out.println("\t start normalizing");
    }
    // HashMap<Integer,Boolean> already = new HashMap<Integer, Boolean>(es.size()/2);
    boolean[] already = new boolean[es.size()];
    int countPairs = 0;
    int nonPairs = 0;
    percentOld = 0;
    // normalize (set all edges which have only one direction to -1 and otherwise choose the minimal value)
    for (int j = 0; j < es.size(); j++) {
        if (j % 100000 == 0 && j > 0) {
            percent = Math.rint(((double) j / es.size()) * 10000) / 100;
            if (percent > percentOld + 1) {
                percentOld = percent;
                if (Config.gui) {
                    Console.setBarValue((int) Math.rint(percent));
                    Console.setBarTextPlusRestTime("normalizing  " + percent + " %");
                }
            // else System.out.print( percent + " %\t");
            }
        }
        if (!already[j]) {
            int source = es.sources[j];
            // int source = es.getSource(j);
            int target = es.targets[j];
            // int target = es.getTarget(j);
            float value = es.values[j];
            // float value = es.getValue(j);
            int startPosition = es.startPositions[target];
            // int startPosition = es.getStartPosition(target);
            int endPosition = es.endPositions[target];
            // int endPosition = es.getEndPosition(target);
            boolean hasPartner = false;
            for (int k = startPosition; k < endPosition; k++) {
                int target2 = es.targets[k];
                if (target2 == source) {
                    // Partner found
                    already[k] = true;
                    hasPartner = true;
                    float value2 = es.getValue(k);
                    float minimalValue = Math.min(value, value2);
                    es.setValue(j, minimalValue);
                    es.setValue(k, minimalValue);
                    countPairs++;
                    break;
                }
            }
            if (!hasPartner) {
                // es.setValue(j,0);
                es.setValue(j, InOut.min);
                nonPairs++;
            }
        }
    // end test if already calculated
    }
    if (Config.gui)
        Console.println();
    return es;
}
Also used : Edges(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.Edges) IOException(java.io.IOException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) BlastFile(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.BlastFile)

Example 5 with Edges

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.Edges in project clusterMaker2 by RBVI.

the class Splitter method run.

public void run(HashMap<Integer, String> proteins2integers, HashMap<String, Integer> integers2proteins) throws IOException {
    this.threshold = Config.threshold;
    if (Config.gui)
        Console.println("Start reading similarity file ... ");
    // else // System.out.println("Start reading similarity file ... ");
    Edges es = InOut.readSimilarityFile(Config.similarityFile, proteins2integers, integers2proteins);
    if (Config.gui)
        Console.println();
    if (Config.gui) {
        Console.println("Start splitting ...");
        Console.setBarValue(0);
        Console.setBarText("splitting into connected components");
    }
    // else // System.out.println("Start splitting ...");
    Vector<Vector<Integer>> clusters = splitIntoConnectedComponents(es, proteins2integers, threshold, false);
    if (Config.gui)
        Console.println();
    if (Config.gui) {
        Console.println("Writing costmatrices ...");
        Console.setBarValue(0);
        Console.restartBarTimer();
        Console.setBarText("writing costmatrices");
    }
    // else // System.out.println("Writing costmatrices ...");
    InOut.writeCostMatrices(es, clusters, proteins2integers, integers2proteins);
    if (Config.gui)
        Console.println();
// else // System.out.println();
}
Also used : Edges(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.Edges) Vector(java.util.Vector)

Aggregations

CyNode (org.cytoscape.model.CyNode)10 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 CyEdge (org.cytoscape.model.CyEdge)8 NodeCluster (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster)6 Edges (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.costmatrixcreation.dataTypes.Edges)4 List (java.util.List)4 Vector (java.util.Vector)4 CyNetwork (org.cytoscape.model.CyNetwork)4 CyMatrix (edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)3 Random (java.util.Random)3 Semaphore (java.util.concurrent.Semaphore)3 CyNetworkView (org.cytoscape.view.model.CyNetworkView)3 DoubleMatrix2D (cern.colt.matrix.tdouble.DoubleMatrix2D)2 Point (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.autosome.cluststruct.Point)2 ConnectedComponent (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.datastructure.ConnectedComponent)2 ICCEdges (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.datastructure.ICCEdges)2 ClusteringManager (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.taskmanaging.ClusteringManager)2 HeaderInfo (edu.ucsf.rbvi.clusterMaker2.internal.treeview.HeaderInfo)2 Hashtable (java.util.Hashtable)2