mev-4.0.01/source/org/tigr/microarray/mev/cluster/gui/impl/lem/LEMGUI.java

Code
Comments
Other
Rev Date Author Line
2 26 Feb 07 jari 1 /*
2 26 Feb 07 jari 2 Copyright @ 1999-2006, The Institute for Genomic Research (TIGR).
2 26 Feb 07 jari 3 All rights reserved.
2 26 Feb 07 jari 4 */
2 26 Feb 07 jari 5 package org.tigr.microarray.mev.cluster.gui.impl.lem;
2 26 Feb 07 jari 6
2 26 Feb 07 jari 7 import java.io.BufferedReader;
2 26 Feb 07 jari 8 import java.io.FileNotFoundException;
2 26 Feb 07 jari 9 import java.io.FileReader;
2 26 Feb 07 jari 10 import java.io.IOException;
2 26 Feb 07 jari 11 import java.util.Arrays;
2 26 Feb 07 jari 12 import java.util.Enumeration;
2 26 Feb 07 jari 13 import java.util.Hashtable;
2 26 Feb 07 jari 14 import java.util.NoSuchElementException;
2 26 Feb 07 jari 15 import java.util.Vector;
2 26 Feb 07 jari 16
2 26 Feb 07 jari 17 import javax.swing.JOptionPane;
2 26 Feb 07 jari 18 import javax.swing.tree.DefaultMutableTreeNode;
2 26 Feb 07 jari 19
2 26 Feb 07 jari 20 import org.tigr.microarray.mev.TMEV;
2 26 Feb 07 jari 21 import org.tigr.microarray.mev.cluster.algorithm.Algorithm;
2 26 Feb 07 jari 22 import org.tigr.microarray.mev.cluster.algorithm.AlgorithmData;
2 26 Feb 07 jari 23 import org.tigr.microarray.mev.cluster.algorithm.AlgorithmEvent;
2 26 Feb 07 jari 24 import org.tigr.microarray.mev.cluster.algorithm.AlgorithmException;
2 26 Feb 07 jari 25 import org.tigr.microarray.mev.cluster.algorithm.AlgorithmListener;
2 26 Feb 07 jari 26 import org.tigr.microarray.mev.cluster.gui.Experiment;
2 26 Feb 07 jari 27 import org.tigr.microarray.mev.cluster.gui.IClusterGUI;
2 26 Feb 07 jari 28 import org.tigr.microarray.mev.cluster.gui.IData;
2 26 Feb 07 jari 29 import org.tigr.microarray.mev.cluster.gui.IFramework;
2 26 Feb 07 jari 30 import org.tigr.microarray.mev.cluster.gui.LeafInfo;
2 26 Feb 07 jari 31 import org.tigr.microarray.mev.cluster.gui.helpers.ClusterTableViewer;
2 26 Feb 07 jari 32 import org.tigr.microarray.mev.cluster.gui.impl.dialogs.DialogListener;
2 26 Feb 07 jari 33 import org.tigr.microarray.mev.cluster.gui.impl.dialogs.HTMLMessageFileChooser;
2 26 Feb 07 jari 34 import org.tigr.microarray.mev.cluster.gui.impl.dialogs.Logger;
2 26 Feb 07 jari 35 import org.tigr.microarray.mev.file.StringSplitter;
2 26 Feb 07 jari 36 import org.tigr.util.FloatMatrix;
2 26 Feb 07 jari 37
2 26 Feb 07 jari 38 /**
2 26 Feb 07 jari 39  * @author braisted
2 26 Feb 07 jari 40  * 
2 26 Feb 07 jari 41  * Handles control of the LEM algorithm operation and result construction
2 26 Feb 07 jari 42  * LEM, Linear Expression Maps, constructs viewers to visualize expression
2 26 Feb 07 jari 43  * data presented ordered by chromosomal location.  Chromosome ids and gene
2 26 Feb 07 jari 44  * coordinates can be supplied in the annotation file or in a coordinages
2 26 Feb 07 jari 45  * file.  For multiple chromosomes or plasmids, one viewer is constructed for
2 26 Feb 07 jari 46  * each chromosome.
2 26 Feb 07 jari 47  */
2 26 Feb 07 jari 48 public class LEMGUI implements IClusterGUI { //IScriptGUI
2 26 Feb 07 jari 49   
2 26 Feb 07 jari 50   private Algorithm algorithm;
2 26 Feb 07 jari 51   private Logger logger;
2 26 Feb 07 jari 52   private boolean stop = false;
2 26 Feb 07 jari 53   private String singleChrName = "Single Chromosome";
2 26 Feb 07 jari 54   private IFramework framework;
2 26 Feb 07 jari 55   
2 26 Feb 07 jari 56   /**
2 26 Feb 07 jari 57    *  Gathers parameters, kicks off algorithm, builds output nodes and viewers
2 26 Feb 07 jari 58    */
2 26 Feb 07 jari 59   public DefaultMutableTreeNode execute(IFramework framework) throws AlgorithmException {
2 26 Feb 07 jari 60     this.framework = framework;
2 26 Feb 07 jari 61     IData idata = framework.getData();
2 26 Feb 07 jari 62     Experiment experiment = idata.getExperiment();
2 26 Feb 07 jari 63     FloatMatrix matrix = experiment.getMatrix();
2 26 Feb 07 jari 64     int [] origIndices = experiment.getRowMappingArrayCopy();
2 26 Feb 07 jari 65     DefaultMutableTreeNode algNode = new DefaultMutableTreeNode("LEM");
2 26 Feb 07 jari 66
2 26 Feb 07 jari 67     //LEM dialog
2 26 Feb 07 jari 68     LEMInitDialog dialog = new LEMInitDialog(framework.getFrame(), framework.getData().getFieldNames());    
2 26 Feb 07 jari 69
2 26 Feb 07 jari 70     if(dialog.showModal() != JOptionPane.OK_OPTION)      
2 26 Feb 07 jari 71       return null; //if anything but OK, return null
2 26 Feb 07 jari 72     
2 26 Feb 07 jari 73     //log events
2 26 Feb 07 jari 74     Listener listener = new Listener();
2 26 Feb 07 jari 75     logger = new Logger(framework.getFrame(), "LEM Processing", listener);
2 26 Feb 07 jari 76     
2 26 Feb 07 jari 77     //Algorithm data to capture parameters
2 26 Feb 07 jari 78     AlgorithmData data = new AlgorithmData();
2 26 Feb 07 jari 79     
2 26 Feb 07 jari 80     String locusField = dialog.getLocusField();
2 26 Feb 07 jari 81     String startField = "5' End";
2 26 Feb 07 jari 82     String endField = "3' End";    
2 26 Feb 07 jari 83     String chrField = null;
2 26 Feb 07 jari 84     String fileName = "None";
2 26 Feb 07 jari 85     
2 26 Feb 07 jari 86     boolean hasMultipleChr = dialog.hasMultipleChr();
2 26 Feb 07 jari 87     
2 26 Feb 07 jari 88     logger.show();
2 26 Feb 07 jari 89     logger.append("LEM Processing\n");
2 26 Feb 07 jari 90     logger.append("Retrieving Locus List\n");
2 26 Feb 07 jari 91     
2 26 Feb 07 jari 92     String [] locusList = idata.getAnnotationList(locusField, origIndices);
2 26 Feb 07 jari 93
2 26 Feb 07 jari 94     String [] chrList;
2 26 Feb 07 jari 95     String [] startList;
2 26 Feb 07 jari 96     String [] endList;
2 26 Feb 07 jari 97     int [] startArray;
2 26 Feb 07 jari 98     int [] endArray;
2 26 Feb 07 jari 99     int [] origIndexArray;
2 26 Feb 07 jari 100     boolean [] mappedSpots = new boolean[origIndices.length];
2 26 Feb 07 jari 101
2 26 Feb 07 jari 102     int locusInfoCount = 0;
2 26 Feb 07 jari 103     int incompleteCount = 0;
2 26 Feb 07 jari 104     int [] chrLocusCount;
2 26 Feb 07 jari 105     int globalIncompleteCount = 0;
2 26 Feb 07 jari 106     int totalNumberOfMappedSpots = 0;
2 26 Feb 07 jari 107
2 26 Feb 07 jari 108     //accumulate result nodes based on chr name, iff hasMultipleChr
2 26 Feb 07 jari 109     Hashtable lemResultHash = new Hashtable();
2 26 Feb 07 jari 110
2 26 Feb 07 jari 111     if(hasMultipleChr) {
2 26 Feb 07 jari 112       chrField = dialog.getChrIDField();
2 26 Feb 07 jari 113     }
2 26 Feb 07 jari 114     
2 26 Feb 07 jari 115     //get coord information from file if that is the selected mode
2 26 Feb 07 jari 116     if(dialog.useFileInput()) {
2 26 Feb 07 jari 117       
2 26 Feb 07 jari 118       String [] info = null;
2 26 Feb 07 jari 119       
2 26 Feb 07 jari 120       locusField = "Locus ID";
2 26 Feb 07 jari 121       
2 26 Feb 07 jari 122       String msg = "<html><body><h1><center>Select Chromosomal Location File</center></h1><hr size = 3>";
2 26 Feb 07 jari 123       msg += "Please select a file to supply information containing chromosomal coordinates for your array.";
2 26 Feb 07 jari 124       msg += "<br><br>The file format should be tab delimited text with:<br><br><center><b> gene_id | [chromosome ID] | 5'-end | 3'-end</b></center>";
2 26 Feb 07 jari 125       msg += "</body></html>";
2 26 Feb 07 jari 126       
2 26 Feb 07 jari 127       HTMLMessageFileChooser chooser = new HTMLMessageFileChooser(framework.getFrame(), "Select Chromosomal Location File",msg,TMEV.getDataPath(),true);
2 26 Feb 07 jari 128       if(chooser.showModal() == JOptionPane.OK_OPTION) {
2 26 Feb 07 jari 129         
2 26 Feb 07 jari 130         Hashtable hash = new Hashtable();
2 26 Feb 07 jari 131         logger.append("Reading Coordinate File: "+chooser.getSelectedFile().getName()+"\n");
2 26 Feb 07 jari 132         fileName = chooser.getSelectedFile().getName();
2 26 Feb 07 jari 133         
2 26 Feb 07 jari 134         //parse coord file
2 26 Feb 07 jari 135         try {
2 26 Feb 07 jari 136           BufferedReader bfr = new BufferedReader(new FileReader(chooser.getSelectedFile()));        
2 26 Feb 07 jari 137           
2 26 Feb 07 jari 138           String line;
2 26 Feb 07 jari 139           StringSplitter ss = new StringSplitter('\t');
2 26 Feb 07 jari 140           int cnt = 0;
2 26 Feb 07 jari 141           int tokenCount;          
2 26 Feb 07 jari 142                     
2 26 Feb 07 jari 143           String locus;
2 26 Feb 07 jari 144           
2 26 Feb 07 jari 145           while( (line = bfr.readLine()) != null) {
2 26 Feb 07 jari 146             ss.init(line);            
2 26 Feb 07 jari 147             locus = ss.nextToken();
2 26 Feb 07 jari 148             
2 26 Feb 07 jari 149             if(!hasMultipleChr) {
2 26 Feb 07 jari 150               info = new String[3];
2 26 Feb 07 jari 151               info[0] = singleChrName;
2 26 Feb 07 jari 152               info[1] = ss.nextToken();  //5'
2 26 Feb 07 jari 153               info[2] = ss.nextToken();  //3'
2 26 Feb 07 jari 154             } else {
2 26 Feb 07 jari 155               info = new String[3];
2 26 Feb 07 jari 156               info[0] = ss.nextToken();  //chr
2 26 Feb 07 jari 157               info[1] = ss.nextToken();  //5'
2 26 Feb 07 jari 158               info[2] = ss.nextToken();  //3'            
2 26 Feb 07 jari 159             }
2 26 Feb 07 jari 160             //populate hash
2 26 Feb 07 jari 161             hash.put(locus, info);                    
2 26 Feb 07 jari 162           }                
2 26 Feb 07 jari 163         } catch (FileNotFoundException ioe) {
2 26 Feb 07 jari 164           JOptionPane.showMessageDialog(framework.getFrame(),"File Not Found Error using input file: "+fileName, "Error", JOptionPane.ERROR_MESSAGE);
2 26 Feb 07 jari 165           return null;
2 26 Feb 07 jari 166         } catch (IOException fnfe) {
2 26 Feb 07 jari 167           JOptionPane.showMessageDialog(framework.getFrame(),"I/O Error using input file: "+fileName, "Error", JOptionPane.ERROR_MESSAGE);
2 26 Feb 07 jari 168           return null;          
2 26 Feb 07 jari 169         } catch (NoSuchElementException nsee) {
2 26 Feb 07 jari 170           JOptionPane.showMessageDialog(framework.getFrame(),"File Parsing Error: "+fileName, "Error", JOptionPane.ERROR_MESSAGE);
2 26 Feb 07 jari 171           return null;
2 26 Feb 07 jari 172         }
2 26 Feb 07 jari 173                   
2 26 Feb 07 jari 174         //create lists
2 26 Feb 07 jari 175         startList = new String[locusList.length];
2 26 Feb 07 jari 176         endList = new String[locusList.length];
2 26 Feb 07 jari 177         chrList = new String[locusList.length];
2 26 Feb 07 jari 178         
2 26 Feb 07 jari 179         for(int i = 0; i < locusList.length; i++) {
2 26 Feb 07 jari 180           info = (String [])(hash.get(locusList[i]));
2 26 Feb 07 jari 181           if(info != null) {
2 26 Feb 07 jari 182             chrList[i] = info[0];
2 26 Feb 07 jari 183             startList[i] = info[1];
2 26 Feb 07 jari 184             endList[i] = info[2];    
2 26 Feb 07 jari 185           } else {
2 26 Feb 07 jari 186             chrList[i] = "";
2 26 Feb 07 jari 187             startList[i] = "";
2 26 Feb 07 jari 188             endList[i] = "";
2 26 Feb 07 jari 189           }
2 26 Feb 07 jari 190         }
2 26 Feb 07 jari 191         
2 26 Feb 07 jari 192       } else {  //no file selected
2 26 Feb 07 jari 193         return null;
2 26 Feb 07 jari 194       }
2 26 Feb 07 jari 195       
2 26 Feb 07 jari 196     } else {       
2 26 Feb 07 jari 197       //Not a file input, get info from annotation, not file
2 26 Feb 07 jari 198       chrField = dialog.getChrIDField();
2 26 Feb 07 jari 199       startField = dialog.getStartField();
2 26 Feb 07 jari 200       endField = dialog.getEndField();      
2 26 Feb 07 jari 201       chrList = idata.getAnnotationList(chrField, origIndices);
2 26 Feb 07 jari 202       startList = idata.getAnnotationList(startField, origIndices);
2 26 Feb 07 jari 203       endList = idata.getAnnotationList(endField, origIndices);
2 26 Feb 07 jari 204     }
2 26 Feb 07 jari 205     
2 26 Feb 07 jari 206     //reset origIndices to be a default ordering of loci names
2 26 Feb 07 jari 207     //to point to FloatMatrix rows from the current Experiment
2 26 Feb 07 jari 208     for(int i = 0; i < origIndices.length; i++)
2 26 Feb 07 jari 209       origIndices[i] = i;
2 26 Feb 07 jari 210     
2 26 Feb 07 jari 211     Vector [] chrData;
2 26 Feb 07 jari 212
2 26 Feb 07 jari 213     //Contains multiple Vectors related to particular chrs
2 26 Feb 07 jari 214     Hashtable chrHash = new Hashtable();  
2 26 Feb 07 jari 215     //contains chr names in order of occurance
2 26 Feb 07 jari 216     Vector chrNames = new Vector();
2 26 Feb 07 jari 217     //array of chr names that will be sorted by name for output organization
2 26 Feb 07 jari 218     String [] chrKeys = new String[1];
2 26 Feb 07 jari 219     
2 26 Feb 07 jari 220     if(hasMultipleChr) {      
2 26 Feb 07 jari 221       for(int i = 0; i < chrList.length; i++) {      
2 26 Feb 07 jari 222         
2 26 Feb 07 jari 223         //skip values missing chr == ""        
2 26 Feb 07 jari 224         if(chrList[i].equals(""))
2 26 Feb 07 jari 225           continue;
2 26 Feb 07 jari 226         
2 26 Feb 07 jari 227         if(chrHash.containsKey(chrList[i])) {
2 26 Feb 07 jari 228           
2 26 Feb 07 jari 229           chrData = (Vector [])chrHash.get(chrList[i]);
2 26 Feb 07 jari 230           chrData[0].add(locusList[i]);
2 26 Feb 07 jari 231           chrData[1].add(startList[i]);
2 26 Feb 07 jari 232           chrData[2].add(endList[i]);
2 26 Feb 07 jari 233           
2 26 Feb 07 jari 234           
2 26 Feb 07 jari 235           //chrData[3].add(new Integer(origIndices[i]));
2 26 Feb 07 jari 236           chrData[3].add(new Integer(origIndices[i]));
2 26 Feb 07 jari 237           
2 26 Feb 07 jari 238         } else {
2 26 Feb 07 jari 239           chrNames.add(chrList[i]);
2 26 Feb 07 jari 240           
2 26 Feb 07 jari 241           chrData = new Vector[4];
2 26 Feb 07 jari 242           chrData[0] = new Vector();
2 26 Feb 07 jari 243           chrData[1] = new Vector();
2 26 Feb 07 jari 244           chrData[2] = new Vector();
2 26 Feb 07 jari 245           chrData[3] = new Vector();
2 26 Feb 07 jari 246           
2 26 Feb 07 jari 247           chrData[0].add(locusList[i]);
2 26 Feb 07 jari 248           chrData[1].add(startList[i]);
2 26 Feb 07 jari 249           chrData[2].add(endList[i]);
2 26 Feb 07 jari 250           
2 26 Feb 07 jari 251           
2 26 Feb 07 jari 252           chrData[3].add(new Integer(origIndices[i]));
2 26 Feb 07 jari 253           
2 26 Feb 07 jari 254           chrHash.put(chrList[i], chrData);
2 26 Feb 07 jari 255         }        
2 26 Feb 07 jari 256       }      
2 26 Feb 07 jari 257     } else {
2 26 Feb 07 jari 258       String singleChrName = "Chromosome";
2 26 Feb 07 jari 259       chrNames.add(singleChrName);
2 26 Feb 07 jari 260       chrKeys[0] = singleChrName;
2 26 Feb 07 jari 261       
2 26 Feb 07 jari 262       chrData = new Vector[4];
2 26 Feb 07 jari 263       chrData[0] = new Vector();
2 26 Feb 07 jari 264       chrData[1] = new Vector();
2 26 Feb 07 jari 265       chrData[2] = new Vector();
2 26 Feb 07 jari 266       chrData[3] = new Vector();
2 26 Feb 07 jari 267       
2 26 Feb 07 jari 268       for(int i = 0; i < locusList.length; i++) {
2 26 Feb 07 jari 269         chrData[0].add(locusList[i]);
2 26 Feb 07 jari 270         chrData[1].add(startList[i]);
2 26 Feb 07 jari 271         chrData[2].add(endList[i]);          
2 26 Feb 07 jari 272         chrData[3].add(new Integer(origIndices[i]));
2 26 Feb 07 jari 273       }
2 26 Feb 07 jari 274       
2 26 Feb 07 jari 275       chrHash.put(singleChrName, chrData);        
2 26 Feb 07 jari 276     }
2 26 Feb 07 jari 277     
2 26 Feb 07 jari 278     //catch incomplete information
2 26 Feb 07 jari 279     if(startList.length != endList.length) {
2 26 Feb 07 jari 280       JOptionPane.showMessageDialog(framework.getFrame(), "Coordinate information is incomplete.  Some coordinates are not paired (one 3' per 5')", "Coordinate Information Error", JOptionPane.ERROR_MESSAGE);
2 26 Feb 07 jari 281       return null;
2 26 Feb 07 jari 282     }
2 26 Feb 07 jari 283     
2 26 Feb 07 jari 284     startArray = new int[startList.length];
2 26 Feb 07 jari 285     endArray = new int[endList.length];
2 26 Feb 07 jari 286     chrLocusCount = new int[chrNames.size()];
2 26 Feb 07 jari 287     
2 26 Feb 07 jari 288     String chrName;
2 26 Feb 07 jari 289     int locusCount;
2 26 Feb 07 jari 290     String [] chrLocusList;
2 26 Feb 07 jari 291     int [] mappingData = new int[chrNames.size()];
2 26 Feb 07 jari 292     int [] locusCounts = new int[chrNames.size()];
2 26 Feb 07 jari 293     Hashtable mappingDataTable = new Hashtable();
2 26 Feb 07 jari 294     Hashtable locusCountsTable = new Hashtable();
2 26 Feb 07 jari 295     
2 26 Feb 07 jari 296     for(int chr = 0; chr < chrNames.size(); chr++) {
2 26 Feb 07 jari 297       //get chr name        
2 26 Feb 07 jari 298       chrName = (String)chrNames.get(chr);
2 26 Feb 07 jari 299       
2 26 Feb 07 jari 300       if(chrName.equals(""))
2 26 Feb 07 jari 301         continue;
2 26 Feb 07 jari 302       
2 26 Feb 07 jari 303       //get associated vectors
2 26 Feb 07 jari 304       chrData = (Vector [])chrHash.get(chrName);
2 26 Feb 07 jari 305       
2 26 Feb 07 jari 306       //original
2 26 Feb 07 jari 307       locusCount = chrData[0].size();
2 26 Feb 07 jari 308
2 26 Feb 07 jari 309       //initialize arrays
2 26 Feb 07 jari 310       chrLocusList = new String[chrData[0].size()];
2 26 Feb 07 jari 311       startArray = new int[chrData[1].size()];
2 26 Feb 07 jari 312       endArray = new int[chrData[2].size()];
2 26 Feb 07 jari 313       origIndexArray = new int[locusCount];
2 26 Feb 07 jari 314       
2 26 Feb 07 jari 315       String locus, start, end;
2 26 Feb 07 jari 316       int origIndex;
2 26 Feb 07 jari 317       incompleteCount = 0;
2 26 Feb 07 jari 318       
2 26 Feb 07 jari 319       //build arrays
2 26 Feb 07 jari 320       for(int locusIndex = 0; locusIndex < locusCount; locusIndex++) {
2 26 Feb 07 jari 321         locus = (String)chrData[0].get(locusIndex);
2 26 Feb 07 jari 322         start = (String)chrData[1].get(locusIndex);
2 26 Feb 07 jari 323         end = (String)chrData[2].get(locusIndex);
2 26 Feb 07 jari 324         origIndex = ((Integer)chrData[3].get(locusIndex)).intValue();
2 26 Feb 07 jari 325         
2 26 Feb 07 jari 326         chrLocusList[locusIndex] = locus;
2 26 Feb 07 jari 327
2 26 Feb 07 jari 328         if(!locus.equals("") && !start.equals("") && !end.equals("")) {  
2 26 Feb 07 jari 329           startArray[locusIndex] = Integer.parseInt(start);
2 26 Feb 07 jari 330           endArray[locusIndex] = Integer.parseInt(end);
2 26 Feb 07 jari 331           origIndexArray[locusIndex] = origIndex;
2 26 Feb 07 jari 332         } else {
2 26 Feb 07 jari 333           //flag missing data
2 26 Feb 07 jari 334           startArray[locusIndex] = -1;
2 26 Feb 07 jari 335           endArray[locusIndex] = -1;
2 26 Feb 07 jari 336           origIndexArray[locusIndex] = origIndex;
2 26 Feb 07 jari 337           globalIncompleteCount++;
2 26 Feb 07 jari 338         }
2 26 Feb 07 jari 339       }
2 26 Feb 07 jari 340       
2 26 Feb 07 jari 341       //build locus list, startArray, and endArray;
2 26 Feb 07 jari 342       data.addIntArray("idata-indices", origIndices);
2 26 Feb 07 jari 343       data.addIntArray("original-indices", origIndexArray);      
2 26 Feb 07 jari 344       data.addMatrix("expression-matrix", matrix);                  
2 26 Feb 07 jari 345       data.addStringArray("locus-array", chrLocusList);      
2 26 Feb 07 jari 346       data.addIntArray("start-array", startArray);      
2 26 Feb 07 jari 347       data.addIntArray("end-array", endArray);
2 26 Feb 07 jari 348       
2 26 Feb 07 jari 349       //execute LEM on chromosome
2 26 Feb 07 jari 350       logger.append("Start LEM Construction Operations (LEM.java)\n");
2 26 Feb 07 jari 351       logger.append("Working on Chromosome: "+chrName+"\n");
2 26 Feb 07 jari 352       algorithm = framework.getAlgorithmFactory().getAlgorithm("LEM");
2 26 Feb 07 jari 353       algorithm.addAlgorithmListener(listener);
2 26 Feb 07 jari 354       data = algorithm.execute(data);
2 26 Feb 07 jari 355       
2 26 Feb 07 jari 356       logger.append("Constructing LEM Viewer\n");
2 26 Feb 07 jari 357       
2 26 Feb 07 jari 358       LinearExpressionMapViewer viewer = createViewer(data, experiment, locusField);
2 26 Feb 07 jari 359       LinearExpressionGraphViewer graphViewer = createGraphViewer(data, experiment, locusField, chrName);
2 26 Feb 07 jari 360       //build node
2 26 Feb 07 jari 361       DefaultMutableTreeNode node, legNode, chrNode;
2 26 Feb 07 jari 362       
2 26 Feb 07 jari 363       if(!hasMultipleChr) {
2 26 Feb 07 jari 364         node = new DefaultMutableTreeNode(new LeafInfo("LEM Viewer", viewer));      
2 26 Feb 07 jari 365         legNode = new DefaultMutableTreeNode(new LeafInfo("LEG Viewer", graphViewer));              
2 26 Feb 07 jari 366         algNode.add(node);
2 26 Feb 07 jari 367         algNode.add(legNode);
2 26 Feb 07 jari 368         
2 26 Feb 07 jari 369         lemResultHash.put(chrName, algNode);                
2 26 Feb 07 jari 370       } else {        
2 26 Feb 07 jari 371         chrNode = new DefaultMutableTreeNode(new LeafInfo(chrName));        
2 26 Feb 07 jari 372         node = new DefaultMutableTreeNode(new LeafInfo("LEM Viewer - "+ chrName, viewer));      
2 26 Feb 07 jari 373         chrNode.add(node);
2 26 Feb 07 jari 374         legNode = new DefaultMutableTreeNode(new LeafInfo("LEG Viewer - "+ chrName, graphViewer));                      
2 26 Feb 07 jari 375         chrNode.add(legNode);
2 26 Feb 07 jari 376
2 26 Feb 07 jari 377         lemResultHash.put(chrName, chrNode);        
2 26 Feb 07 jari 378       }
2 26 Feb 07 jari 379       
2 26 Feb 07 jari 380       //get number of mapped spots, also indicate mapped spots
2 26 Feb 07 jari 381       int [][] replicateArrays = data.getIntMatrix("replication-indices-matrix");
2 26 Feb 07 jari 382       int totalMapped = 0;
2 26 Feb 07 jari 383       for(int i = 0; i < replicateArrays.length; i++) {
2 26 Feb 07 jari 384         totalMapped += replicateArrays[i].length;
2 26 Feb 07 jari 385         for(int j = 0; j < replicateArrays[i].length; j++)
2 26 Feb 07 jari 386           mappedSpots[replicateArrays[i][j]] = true;
2 26 Feb 07 jari 387       }
2 26 Feb 07 jari 388       
2 26 Feb 07 jari 389       locusCountsTable.put(chrName, new Integer(replicateArrays.length));
2 26 Feb 07 jari 390       locusCounts[chr] = replicateArrays.length;
2 26 Feb 07 jari 391       
2 26 Feb 07 jari 392       mappingDataTable.put(chrName, new Integer(totalMapped));
2 26 Feb 07 jari 393       mappingData[chr] = totalMapped;
2 26 Feb 07 jari 394       
2 26 Feb 07 jari 395       totalNumberOfMappedSpots += totalMapped;
2 26 Feb 07 jari 396
2 26 Feb 07 jari 397     }
2 26 Feb 07 jari 398     
2 26 Feb 07 jari 399     //sort result nodes if have multiple chr
2 26 Feb 07 jari 400     if(hasMultipleChr) {
2 26 Feb 07 jari 401       chrKeys = new String[lemResultHash.size()];
2 26 Feb 07 jari 402
2 26 Feb 07 jari 403       Enumeration e = lemResultHash.keys();
2 26 Feb 07 jari 404       int cnt = 0;
2 26 Feb 07 jari 405       while(e.hasMoreElements()) {
2 26 Feb 07 jari 406         chrKeys[cnt] = (String)(e.nextElement());
2 26 Feb 07 jari 407         cnt++;
2 26 Feb 07 jari 408       }
2 26 Feb 07 jari 409       
2 26 Feb 07 jari 410       //sort on chr names
2 26 Feb 07 jari 411       Arrays.sort(chrKeys);
2 26 Feb 07 jari 412       
2 26 Feb 07 jari 413       for(int i = 0; i < chrKeys.length; i++) {
2 26 Feb 07 jari 414         //add in sorted order, by name
2 26 Feb 07 jari 415         algNode.add((DefaultMutableTreeNode)(lemResultHash.get(chrKeys[i])));
2 26 Feb 07 jari 416         locusCounts[i] = ((Integer)(locusCountsTable.get(chrKeys[i]))).intValue();
2 26 Feb 07 jari 417         mappingData[i] = ((Integer)(mappingDataTable.get(chrKeys[i]))).intValue();
2 26 Feb 07 jari 418       }      
2 26 Feb 07 jari 419     }
2 26 Feb 07 jari 420         
2 26 Feb 07 jari 421     DefaultMutableTreeNode unMappedTableNode = createTableOfUnmappedSpots(experiment, idata, origIndices, mappedSpots);
2 26 Feb 07 jari 422     
2 26 Feb 07 jari 423     if(unMappedTableNode != null)
2 26 Feb 07 jari 424       algNode.add(unMappedTableNode);
2 26 Feb 07 jari 425       
2 26 Feb 07 jari 426     DefaultMutableTreeNode summaryNode = createSummaryNode(locusField, startField, endField, hasMultipleChr, (hasMultipleChr ? chrField : null), dialog.useFileInput(), fileName, 
2 26 Feb 07 jari 427         framework.getData().getFullExperiment().getNumberOfGenes(), experiment.getNumberOfGenes(), totalNumberOfMappedSpots, chrKeys, mappingData, locusCounts);
2 26 Feb 07 jari 428     algNode.add(summaryNode);
2 26 Feb 07 jari 429         
2 26 Feb 07 jari 430     logger.dispose();
2 26 Feb 07 jari 431     return algNode;    
2 26 Feb 07 jari 432   }
2 26 Feb 07 jari 433   
2 26 Feb 07 jari 434   /**
2 26 Feb 07 jari 435    * Builds the expression map viewer
2 26 Feb 07 jari 436    * @param data parameters and input data
2 26 Feb 07 jari 437    * @param experiment Experiment object
2 26 Feb 07 jari 438    * @param locusFieldName locus id field name (annotation label)
2 26 Feb 07 jari 439    * @return returns the LinearExpressionMapViewer
2 26 Feb 07 jari 440    */
2 26 Feb 07 jari 441   private LinearExpressionMapViewer createViewer(AlgorithmData data, Experiment experiment, String locusFieldName) {
2 26 Feb 07 jari 442     
2 26 Feb 07 jari 443     String [] sortedLociNames = data.getStringArray("sorted-loci-names");
2 26 Feb 07 jari 444     int [][] replicationMatrix = data.getIntMatrix("replication-indices-matrix");
2 26 Feb 07 jari 445     
2 26 Feb 07 jari 446     //Matrix and indices can be used to build a new Experiment for the viewer
2 26 Feb 07 jari 447     FloatMatrix condensedMatrix = data.getMatrix("condensed-matrix");
2 26 Feb 07 jari 448     int [] sortedIDataIndices = data.getIntArray("sorted-idata-indices");
2 26 Feb 07 jari 449     
2 26 Feb 07 jari 450     Experiment newExperiment = new Experiment(condensedMatrix, experiment.getColumnIndicesCopy(), sortedIDataIndices);
2 26 Feb 07 jari 451
2 26 Feb 07 jari 452     //sorted start and end points (min and max coord regardless of direction)
2 26 Feb 07 jari 453     int [] sortedStartCoordinates = data.getIntArray("sorted-start");
2 26 Feb 07 jari 454     int [] sortedEndCoordinates = data.getIntArray("sorted-end");
2 26 Feb 07 jari 455     
2 26 Feb 07 jari 456     //direction indicator, 1 == forward, -1 == back
2 26 Feb 07 jari 457     int [] directionIntArray = data.getIntArray("direction-array");
2 26 Feb 07 jari 458     
2 26 Feb 07 jari 459     //convert to boolean array      
2 26 Feb 07 jari 460     boolean [] isForward = new boolean[directionIntArray.length];
2 26 Feb 07 jari 461     for(int i = 0; i < directionIntArray.length;i++) {
2 26 Feb 07 jari 462       isForward[i] = (directionIntArray[i] == 1);
2 26 Feb 07 jari 463     }
2 26 Feb 07 jari 464     
2 26 Feb 07 jari 465     //offset for overlaps
2 26 Feb 07 jari 466     int [] strata = data.getIntArray("strata-array");
2 26 Feb 07 jari 467         
2 26 Feb 07 jari 468     LinearExpressionMapViewer viewer = new LinearExpressionMapViewer(experiment,
2 26 Feb 07 jari 469         newExperiment, sortedLociNames, sortedStartCoordinates, sortedEndCoordinates,
2 26 Feb 07 jari 470         replicationMatrix, isForward, strata, "Chromosome", locusFieldName);
2 26 Feb 07 jari 471     
2 26 Feb 07 jari 472     return viewer;
2 26 Feb 07 jari 473   }
2 26 Feb 07 jari 474   
2 26 Feb 07 jari 475   /**
2 26 Feb 07 jari 476    * Creates the graph viewer
2 26 Feb 07 jari 477    * @param data Parameters and input data
2 26 Feb 07 jari 478    * @param experiment Experiment object
2 26 Feb 07 jari 479    * @param locusFieldName locus field name
2 26 Feb 07 jari 480    * @param chrName character field name
2 26 Feb 07 jari 481    * @return returns a LinearExpressionGraphViewer
2 26 Feb 07 jari 482    */
2 26 Feb 07 jari 483   private LinearExpressionGraphViewer createGraphViewer(AlgorithmData data, Experiment experiment, String locusFieldName, String chrName) {
2 26 Feb 07 jari 484     
2 26 Feb 07 jari 485     String [] sortedLociNames = data.getStringArray("sorted-loci-names");
2 26 Feb 07 jari 486     int [][] replicationMatrix = data.getIntMatrix("replication-indices-matrix");
2 26 Feb 07 jari 487     
2 26 Feb 07 jari 488     //Matrix and indices can be used to build a new Experiment for the viewer
2 26 Feb 07 jari 489     FloatMatrix condensedMatrix = data.getMatrix("condensed-matrix");
2 26 Feb 07 jari 490     int [] sortedIDataIndices = data.getIntArray("sorted-idata-indices");
2 26 Feb 07 jari 491   
2 26 Feb 07 jari 492     Experiment newExperiment = new Experiment(condensedMatrix, experiment.getColumnIndicesCopy(), sortedIDataIndices);
2 26 Feb 07 jari 493
2 26 Feb 07 jari 494     //sorted start and end points (min and max coord regardless of direction)
2 26 Feb 07 jari 495     int [] sortedStartCoordinates = data.getIntArray("sorted-start");
2 26 Feb 07 jari 496     int [] sortedEndCoordinates = data.getIntArray("sorted-end");
2 26 Feb 07 jari 497     
2 26 Feb 07 jari 498     //direction indicator, 1 == forward, -1 == back
2 26 Feb 07 jari 499     int [] directionIntArray = data.getIntArray("direction-array");
2 26 Feb 07 jari 500     
2 26 Feb 07 jari 501     //convert to boolean array      
2 26 Feb 07 jari 502     boolean [] isForward = new boolean[directionIntArray.length];
2 26 Feb 07 jari 503     for(int i = 0; i < directionIntArray.length;i++) {
2 26 Feb 07 jari 504       isForward[i] = (directionIntArray[i] == 1);
2 26 Feb 07 jari 505     }
2 26 Feb 07 jari 506     
2 26 Feb 07 jari 507     //offset for overlaps
2 26 Feb 07 jari 508     int [] strata = data.getIntArray("strata-array");
2 26 Feb 07 jari 509         
2 26 Feb 07 jari 510     LinearExpressionGraphViewer viewer = new LinearExpressionGraphViewer(/*framework.getData(), */experiment,
2 26 Feb 07 jari 511         newExperiment, sortedLociNames, sortedStartCoordinates, sortedEndCoordinates,
2 26 Feb 07 jari 512         replicationMatrix, chrName, locusFieldName);
2 26 Feb 07 jari 513     
2 26 Feb 07 jari 514     return viewer;
2 26 Feb 07 jari 515   }
2 26 Feb 07 jari 516   
2 26 Feb 07 jari 517   /**
2 26 Feb 07 jari 518    * Builds the summary node
2 26 Feb 07 jari 519    * @param locusField locus id field name
2 26 Feb 07 jari 520    * @param startField start coord. field name
2 26 Feb 07 jari 521    * @param endField end coord. field name
2 26 Feb 07 jari 522    * @param hasMultipleChr boolean for multiple coordinates
2 26 Feb 07 jari 523    * @param chrField chromosome field name
2 26 Feb 07 jari 524    * @param useFileInput boolean for coord. file input or not
2 26 Feb 07 jari 525    * @param fileName file name
2 26 Feb 07 jari 526    * @param totSpotCount total number of input rows
2 26 Feb 07 jari 527    * @param lemSpotCount number of rows entering lem
2 26 Feb 07 jari 528    * @param numberOfMappedSpots number of rows (spots) mapped to loc.
2 26 Feb 07 jari 529    * @param chrNames list of chr names
2 26 Feb 07 jari 530    * @param mappingCounts mapping count for each chr
2 26 Feb 07 jari 531    * @param locusCounts locus counts for each chr
2 26 Feb 07 jari 532    * @return returns a node containing the summary viewer <code>LEMInfoViewer</code>
2 26 Feb 07 jari 533    */
2 26 Feb 07 jari 534   private DefaultMutableTreeNode createSummaryNode(String locusField, String startField, String endField, boolean hasMultipleChr, String chrField, boolean useFileInput, String fileName,    
2 26 Feb 07 jari 535       int totSpotCount, int lemSpotCount, int numberOfMappedSpots, String [] chrNames, int [] mappingCounts, int [] locusCounts) {
2 26 Feb 07 jari 536     
2 26 Feb 07 jari 537     LEMInfoViewer viewer = new LEMInfoViewer(locusField, startField, endField, hasMultipleChr, chrField, useFileInput, fileName,
2 26 Feb 07 jari 538         totSpotCount, lemSpotCount, numberOfMappedSpots, chrNames, mappingCounts, locusCounts);    
2 26 Feb 07 jari 539     
2 26 Feb 07 jari 540     return new DefaultMutableTreeNode(new LeafInfo("Locus Mapping Summary", viewer));    
2 26 Feb 07 jari 541   }
2 26 Feb 07 jari 542   
2 26 Feb 07 jari 543   /**
2 26 Feb 07 jari 544    * Builds a table viewer of unmapped spots
2 26 Feb 07 jari 545    * @param experiment Input Experiment
2 26 Feb 07 jari 546    * @param data IData object
2 26 Feb 07 jari 547    * @param origIndices mapping indices
2 26 Feb 07 jari 548    * @param mappedSpots spots that are mapped
2 26 Feb 07 jari 549    * @return returns a table of unmapped spots
2 26 Feb 07 jari 550    */
2 26 Feb 07 jari 551   private DefaultMutableTreeNode createTableOfUnmappedSpots(Experiment experiment, IData data, int [] origIndices, boolean [] mappedSpots) {
2 26 Feb 07 jari 552     
2 26 Feb 07 jari 553     int count = 0;
2 26 Feb 07 jari 554     for(int i = 0; i < mappedSpots.length; i++) {
2 26 Feb 07 jari 555       if(!mappedSpots[i])
2 26 Feb 07 jari 556         count++;
2 26 Feb 07 jari 557     }
2 26 Feb 07 jari 558
2 26 Feb 07 jari 559     if(count == 0)
2 26 Feb 07 jari 560       return null;
2 26 Feb 07 jari 561     
2 26 Feb 07 jari 562     int [] cluster = new int[count];
2 26 Feb 07 jari 563     count = 0;
2 26 Feb 07 jari 564     for(int i = 0; i < mappedSpots.length; i++) {
2 26 Feb 07 jari 565       if(!mappedSpots[i]) {
2 26 Feb 07 jari 566         cluster[count] = origIndices[i];
2 26 Feb 07 jari 567         count++;
2 26 Feb 07 jari 568       }        
2 26 Feb 07 jari 569     }
2 26 Feb 07 jari 570     
2 26 Feb 07 jari 571     int [][] clusters = new int[1][];
2 26 Feb 07 jari 572     clusters[0] = cluster;
2 26 Feb 07 jari 573
2 26 Feb 07 jari 574     ClusterTableViewer viewer = new ClusterTableViewer(experiment, clusters, data);
2 26 Feb 07 jari 575       
2 26 Feb 07 jari 576     return new DefaultMutableTreeNode(new LeafInfo("Unmapped Spot Table", viewer, new Integer(0)));    
2 26 Feb 07 jari 577
2 26 Feb 07 jari 578   }
2 26 Feb 07 jari 579   
2 26 Feb 07 jari 580     /** Listens to algorithm events and updates the logger.
2 26 Feb 07 jari 581      */
2 26 Feb 07 jari 582     private class Listener extends DialogListener implements AlgorithmListener{
2 26 Feb 07 jari 583         String eventDescription;
2 26 Feb 07 jari 584         /** Handles algorithm events.
2 26 Feb 07 jari 585          * @param actionEvent event object
2 26 Feb 07 jari 586          */
2 26 Feb 07 jari 587         public void actionPerformed(java.awt.event.ActionEvent actionEvent) {
2 26 Feb 07 jari 588             String command = actionEvent.getActionCommand();
2 26 Feb 07 jari 589             if (command.equals("cancel-command")) {
2 26 Feb 07 jari 590                 System.out.println("abort execution");
2 26 Feb 07 jari 591                 stop = true;
2 26 Feb 07 jari 592                 if(algorithm!= null)
2 26 Feb 07 jari 593                   algorithm.abort();
2 26 Feb 07 jari 594                 logger.dispose();
2 26 Feb 07 jari 595             }
2 26 Feb 07 jari 596         }
2 26 Feb 07 jari 597         
2 26 Feb 07 jari 598         /** Invoked when an algorithm progress value was changed.
2 26 Feb 07 jari 599          *
2 26 Feb 07 jari 600          * @param event a <code>AlgorithmEvent</code> object.
2 26 Feb 07 jari 601          */
2 26 Feb 07 jari 602        public void valueChanged(AlgorithmEvent event) {
2 26 Feb 07 jari 603             if(event.getId() == AlgorithmEvent.MONITOR_VALUE){
2 26 Feb 07 jari 604                 logger.append( event.getDescription() );
2 26 Feb 07 jari 605             } 
2 26 Feb 07 jari 606         }
2 26 Feb 07 jari 607        
2 26 Feb 07 jari 608     }
2 26 Feb 07 jari 609     
2 26 Feb 07 jari 610   
2 26 Feb 07 jari 611 }