mev-4.0.01/source/org/tigr/microarray/mev/cluster/gui/helpers/ExperimentUtil.java

Code
Comments
Other
Rev Date Author Line
2 26 Feb 07 jari 1 /*
2 26 Feb 07 jari 2 Copyright @ 1999-2003, 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 /*
2 26 Feb 07 jari 6  * $RCSfile: ExperimentUtil.java,v $
2 26 Feb 07 jari 7  * $Revision: 1.9 $
2 26 Feb 07 jari 8  * $Date: 2006/02/23 20:59:48 $
2 26 Feb 07 jari 9  * $Author: caliente $
2 26 Feb 07 jari 10  * $State: Exp $
2 26 Feb 07 jari 11  */
2 26 Feb 07 jari 12
2 26 Feb 07 jari 13 package org.tigr.microarray.mev.cluster.gui.helpers;
2 26 Feb 07 jari 14
2 26 Feb 07 jari 15 import java.awt.Frame;
2 26 Feb 07 jari 16 import java.io.BufferedReader;
2 26 Feb 07 jari 17 import java.io.File;
2 26 Feb 07 jari 18 import java.io.FileOutputStream;
2 26 Feb 07 jari 19 import java.io.FileReader;
2 26 Feb 07 jari 20 import java.io.IOException;
2 26 Feb 07 jari 21 import java.io.PrintWriter;
2 26 Feb 07 jari 22 import java.util.StringTokenizer;
2 26 Feb 07 jari 23 import java.util.Vector;
2 26 Feb 07 jari 24
2 26 Feb 07 jari 25 import javax.swing.JFileChooser;
2 26 Feb 07 jari 26 import javax.swing.JFrame;
2 26 Feb 07 jari 27 import javax.swing.JOptionPane;
2 26 Feb 07 jari 28
2 26 Feb 07 jari 29 import org.tigr.microarray.mev.TMEV;
2 26 Feb 07 jari 30 import org.tigr.microarray.mev.cluster.gui.Experiment;
2 26 Feb 07 jari 31 import org.tigr.microarray.mev.cluster.gui.IData;
2 26 Feb 07 jari 32 import org.tigr.util.BrowserLauncher;
2 26 Feb 07 jari 33 import org.tigr.util.StringSplitter;
2 26 Feb 07 jari 34
2 26 Feb 07 jari 35 /**
2 26 Feb 07 jari 36  * This class contains set of static methods to store
2 26 Feb 07 jari 37  * an experiment data.
2 26 Feb 07 jari 38  *
2 26 Feb 07 jari 39  * @version 1.0
2 26 Feb 07 jari 40  * @author Aleksey D.Rezantsev
2 26 Feb 07 jari 41  */
2 26 Feb 07 jari 42 public class ExperimentUtil {
2 26 Feb 07 jari 43     
2 26 Feb 07 jari 44     public static final int INTEGER_TYPE = 10;
2 26 Feb 07 jari 45     public static final int FLOAT_TYPE = 11;
2 26 Feb 07 jari 46     public static final int DOUBLE_TYPE = 12;
2 26 Feb 07 jari 47     public static final int STRING_TYPE = 13;
2 26 Feb 07 jari 48     public static final int BOOLEAN_TYPE = 14;
2 26 Feb 07 jari 49     
2 26 Feb 07 jari 50     
2 26 Feb 07 jari 51     /**
2 26 Feb 07 jari 52      * Saves all the experiment values.
2 26 Feb 07 jari 53      */
2 26 Feb 07 jari 54     public static void saveExperiment(Frame frame, Experiment experiment, IData data) throws Exception {
2 26 Feb 07 jari 55         saveExperiment(frame, experiment, data, createDefaultRows(experiment.getNumberOfGenes()));
2 26 Feb 07 jari 56     }
2 26 Feb 07 jari 57     
2 26 Feb 07 jari 58     /**
2 26 Feb 07 jari 59      * Saves values from common experiment with specified rows.
2 26 Feb 07 jari 60      */
2 26 Feb 07 jari 61     public static void saveExperiment(Frame frame, IData data, int[] rows) throws Exception {
2 26 Feb 07 jari 62         saveExperiment(frame, data.getExperiment(), data, rows);
2 26 Feb 07 jari 63     }
2 26 Feb 07 jari 64     
2 26 Feb 07 jari 65     /**
2 26 Feb 07 jari 66      * Saves values from specified experiment and its rows.
2 26 Feb 07 jari 67      */
2 26 Feb 07 jari 68     public static void saveExperiment(Frame frame, Experiment experiment, IData data, int[] rows) throws Exception {
2 26 Feb 07 jari 69         File file = getFile(frame);
2 26 Feb 07 jari 70         if (file != null) {
2 26 Feb 07 jari 71             saveCluster(file, experiment, data, rows);
2 26 Feb 07 jari 72         }
2 26 Feb 07 jari 73     }
2 26 Feb 07 jari 74     
2 26 Feb 07 jari 75     /**
2 26 Feb 07 jari 76      * Saves values from specified experiment and cluster.
2 26 Feb 07 jari 77      */
2 26 Feb 07 jari 78     public static void saveExperiment(Frame frame, Experiment experiment, IData data, int[][] clusters) throws Exception {
2 26 Feb 07 jari 79         File file = getFile(frame);
2 26 Feb 07 jari 80
2 26 Feb 07 jari 81         if (file != null) {
2 26 Feb 07 jari 82             //if file name already has a .txt extension, remove it
2 26 Feb 07 jari 83             String path = file.getPath();
2 26 Feb 07 jari 84             int extIndex = path.lastIndexOf(".txt");
2 26 Feb 07 jari 85             if( extIndex > 0) {
2 26 Feb 07 jari 86                 path = path.substring(0, extIndex);
2 26 Feb 07 jari 87                 file = new File(path);
2 26 Feb 07 jari 88             }
2 26 Feb 07 jari 89             
2 26 Feb 07 jari 90             File aFile;
2 26 Feb 07 jari 91             for (int i=0; i<clusters.length; i++) {
2 26 Feb 07 jari 92                 if (clusters[i] == null || clusters[i].length == 0) {
2 26 Feb 07 jari 93                     continue;
2 26 Feb 07 jari 94                 }
2 26 Feb 07 jari 95                 aFile = new File(file.getPath()+"-"+String.valueOf(i+1)+".txt");
2 26 Feb 07 jari 96                 saveCluster(aFile, experiment, data, clusters[i]);
2 26 Feb 07 jari 97             }
2 26 Feb 07 jari 98         }
2 26 Feb 07 jari 99     }
2 26 Feb 07 jari 100     
2 26 Feb 07 jari 101     public static void saveAllGeneClustersWithAux(Frame frame, Experiment experiment, IData data, int[][] clusters, String[] auxTitles, Object[][] auxData) throws Exception {
2 26 Feb 07 jari 102         File file = getFile(frame);
2 26 Feb 07 jari 103         if (file != null) {
2 26 Feb 07 jari 104             File aFile;
2 26 Feb 07 jari 105             for (int i=0; i<clusters.length; i++) {
2 26 Feb 07 jari 106                 if (clusters[i] == null || clusters[i].length == 0) {
2 26 Feb 07 jari 107                     continue;
2 26 Feb 07 jari 108                 }
2 26 Feb 07 jari 109                 aFile = new File(file.getPath()+"-"+String.valueOf(i+1)+".txt");
2 26 Feb 07 jari 110                 saveGeneClusterWithAux(aFile, experiment, data, clusters[i], auxTitles, auxData);
2 26 Feb 07 jari 111             }
2 26 Feb 07 jari 112         }
2 26 Feb 07 jari 113     }  
2 26 Feb 07 jari 114     
2 26 Feb 07 jari 115     private static void saveGeneClusterWithAux(File file, Experiment experiment, IData data, int [] rows, String [] auxTitles, Object [][] auxData) throws Exception{
2 26 Feb 07 jari 116         int[] typeArray = getTypes(auxData);
2 26 Feb 07 jari 117         PrintWriter out = new PrintWriter(new FileOutputStream(file));
2 26 Feb 07 jari 118         String[] fieldNames = data.getFieldNames();
2 26 Feb 07 jari 119         
2 26 Feb 07 jari 120         if(fieldNames == null)
2 26 Feb 07 jari 121             return;
2 26 Feb 07 jari 122         
2 26 Feb 07 jari 123         out.print("Original row");
2 26 Feb 07 jari 124         out.print("\t");
2 26 Feb 07 jari 125         
2 26 Feb 07 jari 126         for (int i = 0; i < fieldNames.length; i++) {
2 26 Feb 07 jari 127             out.print(fieldNames[i]);
2 26 Feb 07 jari 128             if (i < fieldNames.length - 1) {
2 26 Feb 07 jari 129                 out.print("\t");
2 26 Feb 07 jari 130             }
2 26 Feb 07 jari 131         }
2 26 Feb 07 jari 132         for(int i = 0; i < auxTitles.length; i++){
2 26 Feb 07 jari 133             out.print("\t"+auxTitles[i]);
2 26 Feb 07 jari 134         }        
2 26 Feb 07 jari 135         //out.print("UniqueID\tName");
2 26 Feb 07 jari 136         for (int i=0; i<experiment.getNumberOfSamples(); i++) {
2 26 Feb 07 jari 137             out.print("\t");
2 26 Feb 07 jari 138             out.print(data.getSampleAnnotation(i, IData.DEFAULT_SAMPLE_ANNOTATION_KEY));
2 26 Feb 07 jari 139         }
2 26 Feb 07 jari 140         out.print("\n");
2 26 Feb 07 jari 141         
2 26 Feb 07 jari 142         //primary sample names are ouput already, if additional sample annotation is present it should be
2 26 Feb 07 jari 143         //output as well
2 26 Feb 07 jari 144         
2 26 Feb 07 jari 145         Vector sampleFieldNames = data.getSampleAnnotationFieldNames();
2 26 Feb 07 jari 146         //if there are additional sample names
2 26 Feb 07 jari 147         String key;
2 26 Feb 07 jari 148
2 26 Feb 07 jari 149         for(int i = 1; i < sampleFieldNames.size(); i++) {
2 26 Feb 07 jari 150
2 26 Feb 07 jari 151             String val = new String();            
2 26 Feb 07 jari 152             
2 26 Feb 07 jari 153             //skip gene annotation columns
2 26 Feb 07 jari 154             for(int k= 0; k < fieldNames.length+auxTitles.length; k++) {
2 26 Feb 07 jari 155                 val += "\t";
2 26 Feb 07 jari 156             }
2 26 Feb 07 jari 157             
2 26 Feb 07 jari 158             //append key
2 26 Feb 07 jari 159             key = (String)(sampleFieldNames.elementAt(i));
2 26 Feb 07 jari 160             val += key+"\t";
2 26 Feb 07 jari 161             
2 26 Feb 07 jari 162             //append annoation
2 26 Feb 07 jari 163             for(int j = 0; j < data.getFeaturesCount(); j++) {
2 26 Feb 07 jari 164                 val += data.getSampleAnnotation(j, key);
2 26 Feb 07 jari 165                 if(j != data.getFeaturesCount()-1)
2 26 Feb 07 jari 166                     val += "\t";
2 26 Feb 07 jari 167                 else
2 26 Feb 07 jari 168                     val += "\n";
2 26 Feb 07 jari 169             }
2 26 Feb 07 jari 170             out.print(val);
2 26 Feb 07 jari 171         }
2 26 Feb 07 jari 172         
2 26 Feb 07 jari 173         /*
2 26 Feb 07 jari 174         for (int i=0; i<data.getFeaturesCount(); i++) {
2 26 Feb 07 jari 175             out.print("\t");
2 26 Feb 07 jari 176             out.print(data.getFullSampleName(i));
2 26 Feb 07 jari 177         }
2 26 Feb 07 jari 178          */
2 26 Feb 07 jari 179         //aux titles
2 26 Feb 07 jari 180         /*
2 26 Feb 07 jari 181         for(int i = 0; i < auxTitles.length; i++){
2 26 Feb 07 jari 182             out.print("\t"+auxTitles[i]);
2 26 Feb 07 jari 183         }
2 26 Feb 07 jari 184          **/
2 26 Feb 07 jari 185         for (int i=0; i<rows.length; i++) {
2 26 Feb 07 jari 186             out.print(Integer.toString(experiment.getGeneIndexMappedToData(rows[i]) + 1));  //JCB handles cuttoffs, gets gene mapping
2 26 Feb 07 jari 187             out.print("\t");
2 26 Feb 07 jari 188             for (int k = 0; k < fieldNames.length; k++) {
2 26 Feb 07 jari 189                 out.print(data.getElementAttribute(experiment.getGeneIndexMappedToData(rows[i]), k));  //JCB in case of using cuttoffs, get mapping
2 26 Feb 07 jari 190                 
2 26 Feb 07 jari 191                 if (k < fieldNames.length - 1) {
2 26 Feb 07 jari 192                     out.print("\t");
2 26 Feb 07 jari 193                 }
2 26 Feb 07 jari 194             }
2 26 Feb 07 jari 195             
2 26 Feb 07 jari 196             for(int j = 0; j < auxData[0].length; j++){
2 26 Feb 07 jari 197                 out.print("\t");//+auxData[rows[i]][j]);
2 26 Feb 07 jari 198                 printDataType(out, auxData[rows[i]][j], typeArray[j]);
2 26 Feb 07 jari 199             }    
2 26 Feb 07 jari 200             
2 26 Feb 07 jari 201             for (int j=0; j<experiment.getNumberOfSamples(); j++) {
2 26 Feb 07 jari 202                 out.print("\t");
2 26 Feb 07 jari 203                 out.print(Float.toString(experiment.get(rows[i], j)));
2 26 Feb 07 jari 204             }
2 26 Feb 07 jari 205             /*
2 26 Feb 07 jari 206             for (int j=0; j<data.getFeaturesCount(); j++) {
2 26 Feb 07 jari 207                 out.print("\t");
2 26 Feb 07 jari 208                 out.print(Float.toString(data.getRatio(j, rows[i], IData.LOG)));
2 26 Feb 07 jari 209             }
2 26 Feb 07 jari 210              */
2 26 Feb 07 jari 211             
2 26 Feb 07 jari 212             out.print("\n");
2 26 Feb 07 jari 213         }
2 26 Feb 07 jari 214         out.flush();
2 26 Feb 07 jari 215         out.close();
2 26 Feb 07 jari 216     }    
2 26 Feb 07 jari 217     
2 26 Feb 07 jari 218     /**
2 26 Feb 07 jari 219      * Returns a file choosed by the user.
2 26 Feb 07 jari 220      */
2 26 Feb 07 jari 221     private static File getFile(Frame frame) {
2 26 Feb 07 jari 222         
2 26 Feb 07 jari 223         String dataPath = TMEV.getDataPath();
2 26 Feb 07 jari 224         File pathFile = TMEV.getFile("data/");
2 26 Feb 07 jari 225         
2 26 Feb 07 jari 226         if(dataPath != null) {
2 26 Feb 07 jari 227             pathFile = new File(dataPath);
2 26 Feb 07 jari 228             if(!pathFile.exists())
2 26 Feb 07 jari 229                 pathFile = TMEV.getFile("data/");
2 26 Feb 07 jari 230         } else {
2 26 Feb 07 jari 231             System.out.println("null data path");
2 26 Feb 07 jari 232         }
2 26 Feb 07 jari 233         
2 26 Feb 07 jari 234         File file = null;
2 26 Feb 07 jari 235         
2 26 Feb 07 jari 236         
2 26 Feb 07 jari 237         final JFileChooser fc = new JFileChooser(pathFile);
2 26 Feb 07 jari 238         fc.addChoosableFileFilter(new ExpressionFileFilter());
2 26 Feb 07 jari 239         fc.setFileView(new ExpressionFileView());
2 26 Feb 07 jari 240         int ret = fc.showSaveDialog(frame);
2 26 Feb 07 jari 241         if (ret == JFileChooser.APPROVE_OPTION) {
2 26 Feb 07 jari 242             file = fc.getSelectedFile();
2 26 Feb 07 jari 243         } else {
2 26 Feb 07 jari 244             return null;
2 26 Feb 07 jari 245         }
2 26 Feb 07 jari 246         
2 26 Feb 07 jari 247         String fileName = file.getName();
2 26 Feb 07 jari 248         int extIndex = fileName.lastIndexOf(".");
2 26 Feb 07 jari 249         
2 26 Feb 07 jari 250         //check for a three char extension, else append the default .txt
2 26 Feb 07 jari 251         if(extIndex < 0 || ( fileName.length()- 1 - extIndex ) != 3)
2 26 Feb 07 jari 252             file = new File(file.getPath()+".txt");
2 26 Feb 07 jari 253         
2 26 Feb 07 jari 254         TMEV.updateDataPath(formatDataPath(file.getPath()));
2 26 Feb 07 jari 255         TMEV.setDataPath(file.getParent());
2 26 Feb 07 jari 256         
2 26 Feb 07 jari 257         return file;
2 26 Feb 07 jari 258     }
2 26 Feb 07 jari 259     
2 26 Feb 07 jari 260
2 26 Feb 07 jari 261     private static String formatDataPath(String dataPath) {
2 26 Feb 07 jari 262         if(dataPath == null)
2 26 Feb 07 jari 263             return " ";
2 26 Feb 07 jari 264         
2 26 Feb 07 jari 265         String renderedSep = "/";
2 26 Feb 07 jari 266         String renderedPath = new String();
2 26 Feb 07 jari 267
2 26 Feb 07 jari 268         String sep = System.getProperty("file.separator");        
2 26 Feb 07 jari 269
2 26 Feb 07 jari 270         StringTokenizer stok = new StringTokenizer(dataPath, sep);
2 26 Feb 07 jari 271         
2 26 Feb 07 jari 272         String newDataPath = new String();
2 26 Feb 07 jari 273
2 26 Feb 07 jari 274         String str;
2 26 Feb 07 jari 275         while(stok.hasMoreTokens() && stok.countTokens() > 1){
2 26 Feb 07 jari 276             str = stok.nextToken();
2 26 Feb 07 jari 277             renderedPath += str + renderedSep;            
2 26 Feb 07 jari 278             newDataPath += str + sep;
2 26 Feb 07 jari 279         } 
2 26 Feb 07 jari 280         return renderedPath;
2 26 Feb 07 jari 281     }
2 26 Feb 07 jari 282     
2 26 Feb 07 jari 283     /**
2 26 Feb 07 jari 284      * Saves values from specified rows in IData.
2 26 Feb 07 jari 285      */
2 26 Feb 07 jari 286     public static void saveGeneCluster(Frame frame, IData data, int[] rows) throws Exception {
2 26 Feb 07 jari 287         File file = getFile(frame);
2 26 Feb 07 jari 288         if (file != null) {
2 26 Feb 07 jari 289             saveGeneCluster(file, data, rows);
2 26 Feb 07 jari 290         }
2 26 Feb 07 jari 291     }
2 26 Feb 07 jari 292     
2 26 Feb 07 jari 293     private static void saveGeneCluster(File file, IData data, int [] rows) throws Exception{
2 26 Feb 07 jari 294         PrintWriter out = new PrintWriter(new FileOutputStream(file));
2 26 Feb 07 jari 295         String[] fieldNames = data.getFieldNames();
2 26 Feb 07 jari 296         
2 26 Feb 07 jari 297         if(fieldNames == null)
2 26 Feb 07 jari 298             return;
2 26 Feb 07 jari 299         
2 26 Feb 07 jari 300         out.print("Original row");
2 26 Feb 07 jari 301         out.print("\t");
2 26 Feb 07 jari 302         
2 26 Feb 07 jari 303         for (int i = 0; i < fieldNames.length; i++) {
2 26 Feb 07 jari 304             out.print(fieldNames[i]);
2 26 Feb 07 jari 305             if (i < fieldNames.length - 1) {
2 26 Feb 07 jari 306                 out.print("\t");
2 26 Feb 07 jari 307             }
2 26 Feb 07 jari 308         }
2 26 Feb 07 jari 309         //out.print("UniqueID\tName");
2 26 Feb 07 jari 310         for (int i=0; i<data.getFeaturesCount(); i++) {
2 26 Feb 07 jari 311             out.print("\t");
2 26 Feb 07 jari 312             out.print(data.getSampleAnnotation(i, IData.DEFAULT_SAMPLE_ANNOTATION_KEY));
2 26 Feb 07 jari 313         }
2 26 Feb 07 jari 314         out.print("\n");
2 26 Feb 07 jari 315
2 26 Feb 07 jari 316         //primary sample names are ouput already, if additional sample annotation is present it should be
2 26 Feb 07 jari 317         //output as well
2 26 Feb 07 jari 318         
2 26 Feb 07 jari 319         Vector sampleFieldNames = data.getSampleAnnotationFieldNames();
2 26 Feb 07 jari 320         //if there are additional sample names
2 26 Feb 07 jari 321         String key;
2 26 Feb 07 jari 322
2 26 Feb 07 jari 323         for(int i = 1; i < sampleFieldNames.size(); i++) {
2 26 Feb 07 jari 324
2 26 Feb 07 jari 325             String val = new String();            
2 26 Feb 07 jari 326             
2 26 Feb 07 jari 327             //skip gene annotation columns
2 26 Feb 07 jari 328             for(int k= 0; k < fieldNames.length; k++) {
2 26 Feb 07 jari 329                 val += "\t";
2 26 Feb 07 jari 330             }
2 26 Feb 07 jari 331             
2 26 Feb 07 jari 332             //append key
2 26 Feb 07 jari 333             key = (String)(sampleFieldNames.elementAt(i));
2 26 Feb 07 jari 334             val += key+"\t";
2 26 Feb 07 jari 335             
2 26 Feb 07 jari 336             //append annoation
2 26 Feb 07 jari 337             for(int j = 0; j < data.getFeaturesCount(); j++) {
2 26 Feb 07 jari 338                 val += data.getSampleAnnotation(j, key);
2 26 Feb 07 jari 339                 if(j != data.getFeaturesCount()-1)
2 26 Feb 07 jari 340                     val += "\t";
2 26 Feb 07 jari 341                 else
2 26 Feb 07 jari 342                     val += "\n";
2 26 Feb 07 jari 343             }
2 26 Feb 07 jari 344             out.print(val);
2 26 Feb 07 jari 345         }
2 26 Feb 07 jari 346         
2 26 Feb 07 jari 347         for (int i=0; i<rows.length; i++) {
2 26 Feb 07 jari 348             out.print(Integer.toString(rows[i] + 1));  //JCB handles cuttoffs, gets gene mapping
2 26 Feb 07 jari 349             out.print("\t");
2 26 Feb 07 jari 350             for (int k = 0; k < fieldNames.length; k++) {
2 26 Feb 07 jari 351                 out.print(data.getElementAttribute(rows[i], k));  //JCB in case of using cuttoffs, get mapping
2 26 Feb 07 jari 352                 
2 26 Feb 07 jari 353                 if (k < fieldNames.length - 1) {
2 26 Feb 07 jari 354                     out.print("\t");
2 26 Feb 07 jari 355                 }
2 26 Feb 07 jari 356             }
2 26 Feb 07 jari 357             for (int j=0; j<data.getFeaturesCount(); j++) {
2 26 Feb 07 jari 358                 out.print("\t");
2 26 Feb 07 jari 359                 out.print(Float.toString(data.getRatio(j, rows[i], IData.LOG)));
2 26 Feb 07 jari 360             }
2 26 Feb 07 jari 361             out.print("\n");
2 26 Feb 07 jari 362         }
2 26 Feb 07 jari 363         out.flush();
2 26 Feb 07 jari 364         out.close();
2 26 Feb 07 jari 365     }
2 26 Feb 07 jari 366     
2 26 Feb 07 jari 367     
2 26 Feb 07 jari 368     /**
2 26 Feb 07 jari 369      * Saves values from specified rows in IData with specified auxilary header titles and data.
2 26 Feb 07 jari 370      * (rows are mapped IData indices, Experiment object is used for mapping)
2 26 Feb 07 jari 371      */
2 26 Feb 07 jari 372     public static void saveGeneClusterWithAux(Frame frame, Experiment experiment, IData data, int[] rows, String [] auxTitles, Object auxData[][]) throws Exception {
2 26 Feb 07 jari 373         File file = getFile(frame);
2 26 Feb 07 jari 374         if (file != null) {
2 26 Feb 07 jari 375             saveGeneClusterWithAux(file, experiment, data, rows, auxTitles, auxData);
2 26 Feb 07 jari 376         }
2 26 Feb 07 jari 377     }
2 26 Feb 07 jari 378     
2 26 Feb 07 jari 379     /**
2 26 Feb 07 jari 380      *  Saves gene cluster and aux data.  Presumes rows are IData indices.
2 26 Feb 07 jari 381      */
2 26 Feb 07 jari 382     
2 26 Feb 07 jari 383     /*
2 26 Feb 07 jari 384     private static void saveGeneCluster(File file, IData data, int [] rows, String [] auxTitles, Object [][] auxData) throws Exception{
2 26 Feb 07 jari 385         int[] typeArray = getTypes(auxData);
2 26 Feb 07 jari 386         PrintWriter out = new PrintWriter(new FileOutputStream(file));
2 26 Feb 07 jari 387         String[] fieldNames = data.getFieldNames();
2 26 Feb 07 jari 388         
2 26 Feb 07 jari 389         if(fieldNames == null)
2 26 Feb 07 jari 390             return;
2 26 Feb 07 jari 391         
2 26 Feb 07 jari 392         out.print("Original row");
2 26 Feb 07 jari 393         out.print("\t");
2 26 Feb 07 jari 394         
2 26 Feb 07 jari 395         for (int i = 0; i < fieldNames.length; i++) {
2 26 Feb 07 jari 396             out.print(fieldNames[i]);
2 26 Feb 07 jari 397             if (i < fieldNames.length - 1) {
2 26 Feb 07 jari 398                 out.print("\t");
2 26 Feb 07 jari 399             }
2 26 Feb 07 jari 400         }
2 26 Feb 07 jari 401         //out.print("UniqueID\tName");
2 26 Feb 07 jari 402         for (int i=0; i<data.getFeaturesCount(); i++) {
2 26 Feb 07 jari 403             out.print("\t");
2 26 Feb 07 jari 404             out.print(data.getFullSampleName(i));
2 26 Feb 07 jari 405         }
2 26 Feb 07 jari 406         //aux titles
2 26 Feb 07 jari 407         for(int i = 0; i < auxTitles.length; i++){
2 26 Feb 07 jari 408             out.print("\t"+auxTitles[i]);
2 26 Feb 07 jari 409         }
2 26 Feb 07 jari 410         out.print("\n");
2 26 Feb 07 jari 411         for (int i=0; i<rows.length; i++) {
2 26 Feb 07 jari 412             out.print(Integer.toString(rows[i] + 1));  //JCB handles cuttoffs, gets gene mapping
2 26 Feb 07 jari 413             out.print("\t");
2 26 Feb 07 jari 414             for (int k = 0; k < fieldNames.length; k++) {
2 26 Feb 07 jari 415                 out.print(data.getElementAttribute(rows[i], k));  //JCB in case of using cuttoffs, get mapping
2 26 Feb 07 jari 416                 
2 26 Feb 07 jari 417                 if (k < fieldNames.length - 1) {
2 26 Feb 07 jari 418                     out.print("\t");
2 26 Feb 07 jari 419                 }
2 26 Feb 07 jari 420             }
2 26 Feb 07 jari 421             for (int j=0; j<data.getFeaturesCount(); j++) {
2 26 Feb 07 jari 422                 out.print("\t");
2 26 Feb 07 jari 423                 out.print(Float.toString(data.getRatio(j, rows[i], IData.LOG)));
2 26 Feb 07 jari 424             }
2 26 Feb 07 jari 425             
2 26 Feb 07 jari 426             for(int j = 0; j < auxData[0].length; j++){
2 26 Feb 07 jari 427                 out.print("\t");//+auxData[rows[i]][j]);
2 26 Feb 07 jari 428                 printDataType(out, auxData[rows[i]][j], typeArray[i]);
2 26 Feb 07 jari 429             }
2 26 Feb 07 jari 430             out.print("\n");
2 26 Feb 07 jari 431         }
2 26 Feb 07 jari 432         out.flush();
2 26 Feb 07 jari 433         out.close();
2 26 Feb 07 jari 434     }
2 26 Feb 07 jari 435     
2 26 Feb 07 jari 436     */
2 26 Feb 07 jari 437     
2 26 Feb 07 jari 438     
2 26 Feb 07 jari 439     /**
2 26 Feb 07 jari 440      * Saves experiment data as a cluster.
2 26 Feb 07 jari 441      */
2 26 Feb 07 jari 442     
2 26 Feb 07 jari 443     private static void saveCluster(File file, Experiment experiment, IData data, int[] rows) throws Exception {
2 26 Feb 07 jari 444         PrintWriter out = new PrintWriter(new FileOutputStream(file));
2 26 Feb 07 jari 445         String[] fieldNames = data.getFieldNames();
2 26 Feb 07 jari 446         if(fieldNames == null)
2 26 Feb 07 jari 447             return;
2 26 Feb 07 jari 448         out.print("Original row");
2 26 Feb 07 jari 449         out.print("\t");
2 26 Feb 07 jari 450         for (int i = 0; i < fieldNames.length; i++) {
2 26 Feb 07 jari 451             out.print(fieldNames[i]);
2 26 Feb 07 jari 452             if (i < fieldNames.length - 1) {
2 26 Feb 07 jari 453                 out.print("\t");
2 26 Feb 07 jari 454             }
2 26 Feb 07 jari 455         }
2 26 Feb 07 jari 456         //out.print("UniqueID\tName");
2 26 Feb 07 jari 457         for (int i=0; i<experiment.getNumberOfSamples(); i++) {
2 26 Feb 07 jari 458             out.print("\t");
2 26 Feb 07 jari 459             out.print(data.getSampleAnnotation(i, IData.DEFAULT_SAMPLE_ANNOTATION_KEY));
2 26 Feb 07 jari 460         }
2 26 Feb 07 jari 461         out.print("\n");
2 26 Feb 07 jari 462
2 26 Feb 07 jari 463         //primary sample names are ouput already, if additional sample annotation is present it should be
2 26 Feb 07 jari 464         //output as well
2 26 Feb 07 jari 465         
2 26 Feb 07 jari 466         Vector sampleFieldNames = data.getSampleAnnotationFieldNames();
2 26 Feb 07 jari 467         //if there are additional sample names
2 26 Feb 07 jari 468         String key;
2 26 Feb 07 jari 469
2 26 Feb 07 jari 470         for(int i = 1; i < sampleFieldNames.size(); i++) {
2 26 Feb 07 jari 471
2 26 Feb 07 jari 472             String val = new String();            
2 26 Feb 07 jari 473             
2 26 Feb 07 jari 474             //skip gene annotation columns
2 26 Feb 07 jari 475             for(int k= 0; k < fieldNames.length; k++) {
2 26 Feb 07 jari 476                 val += "\t";
2 26 Feb 07 jari 477             }
2 26 Feb 07 jari 478             
2 26 Feb 07 jari 479             //append key
2 26 Feb 07 jari 480             key = (String)(sampleFieldNames.elementAt(i));
2 26 Feb 07 jari 481             val += key+"\t";
2 26 Feb 07 jari 482             
2 26 Feb 07 jari 483             //append annoation
2 26 Feb 07 jari 484             for(int j = 0; j < data.getFeaturesCount(); j++) {
2 26 Feb 07 jari 485                 val += data.getSampleAnnotation(j, key);
2 26 Feb 07 jari 486                 if(j != data.getFeaturesCount()-1)
2 26 Feb 07 jari 487                     val += "\t";
2 26 Feb 07 jari 488                 else
2 26 Feb 07 jari 489                     val += "\n";
2 26 Feb 07 jari 490             }
2 26 Feb 07 jari 491             out.print(val);
2 26 Feb 07 jari 492         }
2 26 Feb 07 jari 493         
2 26 Feb 07 jari 494         for (int i=0; i<rows.length; i++) {
2 26 Feb 07 jari 495             out.print(Integer.toString(experiment.getGeneIndexMappedToData(rows[i]) + 1));  //JCB handles cuttoffs, gets gene mapping
2 26 Feb 07 jari 496             out.print("\t");
2 26 Feb 07 jari 497             for (int k = 0; k < fieldNames.length; k++) {
2 26 Feb 07 jari 498                 out.print(data.getElementAttribute(experiment.getGeneIndexMappedToData(rows[i]), k));  //JCB in case of using cuttoffs, get mapping
2 26 Feb 07 jari 499                 
2 26 Feb 07 jari 500                 if (k < fieldNames.length - 1) {
2 26 Feb 07 jari 501                     out.print("\t");
2 26 Feb 07 jari 502                 }
2 26 Feb 07 jari 503             }
2 26 Feb 07 jari 504             for (int j=0; j<experiment.getNumberOfSamples(); j++) {
2 26 Feb 07 jari 505                 out.print("\t");
2 26 Feb 07 jari 506                 out.print(Float.toString(experiment.get(rows[i], j)));
2 26 Feb 07 jari 507             }
2 26 Feb 07 jari 508             out.print("\n");
2 26 Feb 07 jari 509         }
2 26 Feb 07 jari 510         out.flush();
2 26 Feb 07 jari 511         out.close();
2 26 Feb 07 jari 512     }
2 26 Feb 07 jari 513     
2 26 Feb 07 jari 514     
2 26 Feb 07 jari 515     /**
2 26 Feb 07 jari 516      * Saves values from specified experiment cluster and its rows.
2 26 Feb 07 jari 517      */
2 26 Feb 07 jari 518     public static void saveExperimentCluster(Frame frame, Experiment experiment, IData data, int[] rows) throws Exception {
2 26 Feb 07 jari 519         File file = getFile(frame);
2 26 Feb 07 jari 520         if (file != null) {
2 26 Feb 07 jari 521             saveExperimentCluster(file, experiment, data, rows);
2 26 Feb 07 jari 522         }
2 26 Feb 07 jari 523     }
2 26 Feb 07 jari 524     
2 26 Feb 07 jari 525     /**
2 26 Feb 07 jari 526      * Saves values from specified experiment cluster and its rows.
2 26 Feb 07 jari 527      */
2 26 Feb 07 jari 528     public static void saveAllExperimentClusters(Frame frame, Experiment experiment, IData data, int[][] clusters) throws Exception {
2 26 Feb 07 jari 529         
2 26 Feb 07 jari 530         File file = getFile(frame);
2 26 Feb 07 jari 531         if (file != null) {
2 26 Feb 07 jari 532             File aFile;
2 26 Feb 07 jari 533             for (int i=0; i<clusters.length; i++) {
2 26 Feb 07 jari 534                 if (clusters[i] == null || clusters[i].length == 0) {
2 26 Feb 07 jari 535                     continue;
2 26 Feb 07 jari 536                 }
2 26 Feb 07 jari 537                 aFile = new File(file.getPath()+"-"+String.valueOf(i+1)+".txt");
2 26 Feb 07 jari 538                 saveExperimentCluster(aFile, experiment, data, clusters[i]);
2 26 Feb 07 jari 539             }
2 26 Feb 07 jari 540         }
2 26 Feb 07 jari 541     }
2 26 Feb 07 jari 542     
2 26 Feb 07 jari 543     
2 26 Feb 07 jari 544     public static void saveAllExperimentClustersWithAux(Frame frame, Experiment experiment, IData data, int[][] clusters, String[] auxTitles, Object[][] auxData) throws Exception {
2 26 Feb 07 jari 545         
2 26 Feb 07 jari 546         File file = getFile(frame);
2 26 Feb 07 jari 547         if (file != null) {
2 26 Feb 07 jari 548             File aFile;
2 26 Feb 07 jari 549             for (int i=0; i<clusters.length; i++) {
2 26 Feb 07 jari 550                 if (clusters[i] == null || clusters[i].length == 0) {
2 26 Feb 07 jari 551                     continue;
2 26 Feb 07 jari 552                 }
2 26 Feb 07 jari 553                 aFile = new File(file.getPath()+"-"+String.valueOf(i+1)+".txt");
2 26 Feb 07 jari 554                 saveExperimentClusterWithAux(aFile, experiment, data, clusters[i], auxTitles, auxData);
2 26 Feb 07 jari 555             }
2 26 Feb 07 jari 556         }
2 26 Feb 07 jari 557     }    
2 26 Feb 07 jari 558     
2 26 Feb 07 jari 559     /**
2 26 Feb 07 jari 560      * Saves values from specified experiment cluster and its rows and auxillary data.
2 26 Feb 07 jari 561      */
2 26 Feb 07 jari 562     public static void saveExperimentClusterWithAux(Frame frame, Experiment experiment, IData data, int[] rows, String [] auxTitles, Object [][] auxData) throws Exception {
2 26 Feb 07 jari 563         File file = getFile(frame);
2 26 Feb 07 jari 564         if (file != null) {
2 26 Feb 07 jari 565             saveExperimentClusterWithAux(file, experiment, data, rows, auxTitles, auxData);
2 26 Feb 07 jari 566         }
2 26 Feb 07 jari 567     }
2 26 Feb 07 jari 568     
2 26 Feb 07 jari 569     /**
2 26 Feb 07 jari 570      *  Saves experiment cluster with auxilary data
2 26 Feb 07 jari 571      */
2 26 Feb 07 jari 572     private static void saveExperimentClusterWithAux(File file, Experiment experiment, IData data, int[] experiments, String [] auxTitles, Object [][] auxData) throws Exception {
2 26 Feb 07 jari 573         int[] typeArray = getTypes(auxData);
2 26 Feb 07 jari 574         PrintWriter out = new PrintWriter(new FileOutputStream(file));
2 26 Feb 07 jari 575         String[] fieldNames = data.getFieldNames();
2 26 Feb 07 jari 576         
2 26 Feb 07 jari 577         int numberOfGenes = experiment.getNumberOfGenes();
2 26 Feb 07 jari 578
2 26 Feb 07 jari 579         out.print("Original row");
2 26 Feb 07 jari 580         out.print("\t");
2 26 Feb 07 jari 581
2 26 Feb 07 jari 582         for (int i = 0; i < fieldNames.length; i++) {
2 26 Feb 07 jari 583             out.print(fieldNames[i]);
2 26 Feb 07 jari 584             if (i < fieldNames.length - 1) {
2 26 Feb 07 jari 585                 out.print("\t");
2 26 Feb 07 jari 586             }
2 26 Feb 07 jari 587         }
2 26 Feb 07 jari 588         //out.print("UniqueID\tName");
2 26 Feb 07 jari 589         for (int i=0; i<experiments.length; i++) {
2 26 Feb 07 jari 590             out.print("\t");
2 26 Feb 07 jari 591             out.print(data.getSampleAnnotation(experiment.getSampleIndex(experiments[i]), IData.DEFAULT_SAMPLE_ANNOTATION_KEY));
2 26 Feb 07 jari 592         }
2 26 Feb 07 jari 593         out.print("\n");
2 26 Feb 07 jari 594
2 26 Feb 07 jari 595         Vector sampleFieldNames = data.getSampleAnnotationFieldNames();
2 26 Feb 07 jari 596         //if there are additional sample names
2 26 Feb 07 jari 597         String key;
2 26 Feb 07 jari 598
2 26 Feb 07 jari 599         for(int i = 1; i < sampleFieldNames.size(); i++) {
2 26 Feb 07 jari 600
2 26 Feb 07 jari 601             String val = new String();
2 26 Feb 07 jari 602             
2 26 Feb 07 jari 603             //skip gene annotation columns
2 26 Feb 07 jari 604             for(int k= 0; k < fieldNames.length; k++) {
2 26 Feb 07 jari 605                 val += "\t";
2 26 Feb 07 jari 606             }
2 26 Feb 07 jari 607             
2 26 Feb 07 jari 608             //append key
2 26 Feb 07 jari 609             key = (String)(sampleFieldNames.elementAt(i));
2 26 Feb 07 jari 610             val += key+"\t";
2 26 Feb 07 jari 611             
2 26 Feb 07 jari 612             //append annoation
2 26 Feb 07 jari 613             for(int j = 0; j < experiments.length; j++) {
2 26 Feb 07 jari 614                 val += data.getSampleAnnotation(experiment.getSampleIndex(experiments[j]), key);
2 26 Feb 07 jari 615                 if(j != experiments.length-1)
2 26 Feb 07 jari 616                     val += "\t";
2 26 Feb 07 jari 617                 else
2 26 Feb 07 jari 618                     val += "\n";
2 26 Feb 07 jari 619             }
2 26 Feb 07 jari 620             out.print(val);
2 26 Feb 07 jari 621         }        
2 26 Feb 07 jari 622         
2 26 Feb 07 jari 623         //aux titles
2 26 Feb 07 jari 624         //out.print("\t");        
2 26 Feb 07 jari 625         for(int i = 0; i < auxTitles.length; i++){
2 26 Feb 07 jari 626                 out.print(auxTitles[i]+"\t");
2 26 Feb 07 jari 627                 for(int k = 0; k < fieldNames.length; k++)
2 26 Feb 07 jari 628                     out.print("\t");
2 26 Feb 07 jari 629             
2 26 Feb 07 jari 630                 //out.print(auxTitles[i]+"\t");
2 26 Feb 07 jari 631                 
2 26 Feb 07 jari 632                 for(int j = 0; j < experiments.length; j++){
2 26 Feb 07 jari 633                     printDataType(out, auxData[experiments[j]][i], typeArray[i]);
2 26 Feb 07 jari 634                     //out.print(auxData[experiments[j]][i]);
2 26 Feb 07 jari 635                     if (j < experiments.length - 1)
2 26 Feb 07 jari 636                         out.print("\t");
2 26 Feb 07 jari 637                 }                
2 26 Feb 07 jari 638                 out.print("\n");            
2 26 Feb 07 jari 639         }
2 26 Feb 07 jari 640         //out.print("\n");
2 26 Feb 07 jari 641         for (int i=0; i<numberOfGenes; i++) {
2 26 Feb 07 jari 642             out.print(Integer.toString(experiment.getGeneIndexMappedToData(i) + 1));  //JCB handles cuttoffs, gets gene mapping
2 26 Feb 07 jari 643             out.print("\t");
2 26 Feb 07 jari 644             for (int k = 0; k < fieldNames.length; k++) {
2 26 Feb 07 jari 645                 out.print(data.getElementAttribute(experiment.getGeneIndexMappedToData(i), k));  //JCB in case of using cuttoffs, get mapping
2 26 Feb 07 jari 646                 
2 26 Feb 07 jari 647                 if (k < fieldNames.length - 1) {
2 26 Feb 07 jari 648                     out.print("\t");
2 26 Feb 07 jari 649                 }
2 26 Feb 07 jari 650             }
2 26 Feb 07 jari 651             for (int j=0; j<experiments.length; j++) {
2 26 Feb 07 jari 652                 out.print("\t");
2 26 Feb 07 jari 653                 out.print(Float.toString(experiment.get(i, experiment.getSampleIndex(experiments[j]))));
2 26 Feb 07 jari 654             }
2 26 Feb 07 jari 655             out.print("\n");
2 26 Feb 07 jari 656         }
2 26 Feb 07 jari 657         out.flush();
2 26 Feb 07 jari 658         out.close();
2 26 Feb 07 jari 659     }
2 26 Feb 07 jari 660     
2 26 Feb 07 jari 661     
2 26 Feb 07 jari 662     /**
2 26 Feb 07 jari 663      *  Saves experiment cluster
2 26 Feb 07 jari 664      */
2 26 Feb 07 jari 665     private static void saveExperimentCluster(File file, Experiment experiment, IData data, int[] experiments) throws Exception {
2 26 Feb 07 jari 666         PrintWriter out = new PrintWriter(new FileOutputStream(file));
2 26 Feb 07 jari 667         String[] fieldNames = data.getFieldNames();
2 26 Feb 07 jari 668         
2 26 Feb 07 jari 669         int numberOfGenes = experiment.getNumberOfGenes();
2 26 Feb 07 jari 670         
2 26 Feb 07 jari 671         out.print("Original row");
2 26 Feb 07 jari 672         out.print("\t");
2 26 Feb 07 jari 673         
2 26 Feb 07 jari 674         for (int i = 0; i < fieldNames.length; i++) {
2 26 Feb 07 jari 675             out.print(fieldNames[i]);
2 26 Feb 07 jari 676             if (i < fieldNames.length - 1) {
2 26 Feb 07 jari 677                 out.print("\t");
2 26 Feb 07 jari 678             }
2 26 Feb 07 jari 679         }
2 26 Feb 07 jari 680         
2 26 Feb 07 jari 681         //out.print("UniqueID\tName");
2 26 Feb 07 jari 682         for (int i=0; i<experiments.length; i++) {
2 26 Feb 07 jari 683             out.print("\t");
2 26 Feb 07 jari 684             out.print(data.getSampleAnnotation(experiment.getSampleIndex(experiments[i]), IData.DEFAULT_SAMPLE_ANNOTATION_KEY));
2 26 Feb 07 jari 685         }        
2 26 Feb 07 jari 686         out.print("\n");
2 26 Feb 07 jari 687         
2 26 Feb 07 jari 688         Vector sampleFieldNames = data.getSampleAnnotationFieldNames();
2 26 Feb 07 jari 689         //if there are additional sample names
2 26 Feb 07 jari 690         String key;
2 26 Feb 07 jari 691
2 26 Feb 07 jari 692         for(int i = 1; i < sampleFieldNames.size(); i++) {
2 26 Feb 07 jari 693
2 26 Feb 07 jari 694             String val = new String();            
2 26 Feb 07 jari 695             
2 26 Feb 07 jari 696             //skip gene annotation columns
2 26 Feb 07 jari 697             for(int k= 0; k < fieldNames.length; k++) {
2 26 Feb 07 jari 698                 val += "\t";
2 26 Feb 07 jari 699             }
2 26 Feb 07 jari 700             
2 26 Feb 07 jari 701             //append key
2 26 Feb 07 jari 702             key = (String)(sampleFieldNames.elementAt(i));
2 26 Feb 07 jari 703             val += key+"\t";
2 26 Feb 07 jari 704             
2 26 Feb 07 jari 705             //append annoation
2 26 Feb 07 jari 706             for(int j = 0; j < experiments.length; j++) {
2 26 Feb 07 jari 707                 val += data.getSampleAnnotation(experiment.getSampleIndex(experiments[j]), key);
2 26 Feb 07 jari 708                 if(j != experiments.length-1)
2 26 Feb 07 jari 709                     val += "\t";
2 26 Feb 07 jari 710                 else
2 26 Feb 07 jari 711                     val += "\n";
2 26 Feb 07 jari 712             }
2 26 Feb 07 jari 713             out.print(val);
2 26 Feb 07 jari 714         }        
2 26 Feb 07 jari 715         
2 26 Feb 07 jari 716         for (int i=0; i<numberOfGenes; i++) {
2 26 Feb 07 jari 717             out.print(Integer.toString(experiment.getGeneIndexMappedToData(i) + 1));  //JCB handles cuttoffs, gets gene mapping
2 26 Feb 07 jari 718             //out.print(data.getUniqueId(rows[i]));
2 26 Feb 07 jari 719             out.print("\t");
2 26 Feb 07 jari 720             //out.print(data.getGeneName(rows[i]));
2 26 Feb 07 jari 721             for (int k = 0; k < fieldNames.length; k++) {
2 26 Feb 07 jari 722                 //                out.print(data.getElementAttribute(rows[i], k));
2 26 Feb 07 jari 723                 out.print(data.getElementAttribute(experiment.getGeneIndexMappedToData(i), k));  //JCB in case of using cuttoffs, get mapping
2 26 Feb 07 jari 724                 
2 26 Feb 07 jari 725                 if (k < fieldNames.length - 1) {
2 26 Feb 07 jari 726                     out.print("\t");
2 26 Feb 07 jari 727                 }
2 26 Feb 07 jari 728             }
2 26 Feb 07 jari 729             for (int j=0; j<experiments.length; j++) {
2 26 Feb 07 jari 730                 out.print("\t");
2 26 Feb 07 jari 731                 out.print(Float.toString(experiment.get(i, experiment.getSampleIndex(experiments[j]))));
2 26 Feb 07 jari 732             }
2 26 Feb 07 jari 733             out.print("\n");
2 26 Feb 07 jari 734         }
2 26 Feb 07 jari 735         out.flush();
2 26 Feb 07 jari 736         out.close();
2 26 Feb 07 jari 737     }
2 26 Feb 07 jari 738     
2 26 Feb 07 jari 739
2 26 Feb 07 jari 740     public static void linkToURL(JFrame frame, Experiment experiment, IData data, int row)  /*throws Exception*/ {
2 26 Feb 07 jari 741         //NOTE: In this method, the argument "row" is what's obtained AFTER applying getGeneIndexMappedToSelectedRows(); i.e., use as is; no need to re-map for cutoffs 
2 26 Feb 07 jari 742         try {
2 26 Feb 07 jari 743             File file = TMEV.getConfigurationFile("annotation_URLs.txt");
2 26 Feb 07 jari 744             //System.out.println("Found annotation file");
2 26 Feb 07 jari 745             AnnotationURLLinkDialog aDialog = new AnnotationURLLinkDialog(frame, false, experiment, data, row, file);
2 26 Feb 07 jari 746             aDialog.setVisible(true);
2 26 Feb 07 jari 747             //if (aDialog.isOkPressed()) lastSelectedAnnotationIndices = aDialog.getLastSelectedIndices();
2 26 Feb 07 jari 748         } catch (Exception e) {
2 26 Feb 07 jari 749             JOptionPane.showMessageDialog(new JFrame(), "Could not link to URL! Make sure \"annotation_URLs.txt\" file in \"config\" directory is in correct format", "Error", JOptionPane.ERROR_MESSAGE);
2 26 Feb 07 jari 750             //System.out.println("Did not find file");
2 26 Feb 07 jari 751             //JOptionPane.showMessageDialog(new JFrame(), "Could not find annotation_URLs.txt file in config directory", "Error", JOptionPane.ERROR_MESSAGE);
2 26 Feb 07 jari 752         }
2 26 Feb 07 jari 753     }
2 26 Feb 07 jari 754     
2 26 Feb 07 jari 755     public static void linkToURL(JFrame frame, Experiment experiment, IData data, int row, int[] lastSelectedIndices)  /*throws Exception*/ {
2 26 Feb 07 jari 756         //int[] indices = {0, 0};
2 26 Feb 07 jari 757         //NOTE: In this method, the argument "row" is what's obtained AFTER applying getGeneIndexMappedToSelectedRows(); i.e., use as is; no need to re-map for cutoffs 
2 26 Feb 07 jari 758         try {
2 26 Feb 07 jari 759             File file = TMEV.getConfigurationFile("annotation_URLs.txt");
2 26 Feb 07 jari 760             System.out.println("Found annotation file");
2 26 Feb 07 jari 761             AnnotationURLLinkDialog aDialog = new AnnotationURLLinkDialog(frame, false, experiment, data, row, file, lastSelectedIndices);
2 26 Feb 07 jari 762             aDialog.setVisible(true);
2 26 Feb 07 jari 763             //if (aDialog.isOkPressed()) 
2 26 Feb 07 jari 764                 //indices = aDialog.getLastSelectedIndices();            
2 26 Feb 07 jari 765         } catch (Exception e) {
2 26 Feb 07 jari 766             JOptionPane.showMessageDialog(new JFrame(), "Could not link to URL! Make sure \"annotation_URLs.txt\" file in \"config\" directory is in correct format", "Error", JOptionPane.ERROR_MESSAGE);            
2 26 Feb 07 jari 767             //System.out.println("Did not find file");
2 26 Feb 07 jari 768             //JOptionPane.showMessageDialog(new JFrame(), "Could not find annotation_URLs.txt file in config directory", "Error", JOptionPane.ERROR_MESSAGE);
2 26 Feb 07 jari 769         }
2 26 Feb 07 jari 770         //return indices;
2 26 Feb 07 jari 771     }    
2 26 Feb 07 jari 772     
2 26 Feb 07 jari 773     public static void linkToURL(JFrame frame, Experiment experiment, IData data, int row, String annotationKey, int[] lastSelectedIndices) {
2 26 Feb 07 jari 774   //NOTE: In this method, the argument "row" is what's obtained AFTER applying getGeneIndexMappedToSelectedRows(); i.e., use as is; no need to re-map for cutoffs      
2 26 Feb 07 jari 775         try {
2 26 Feb 07 jari 776             File file = TMEV.getConfigurationFile("annotation_URLs.txt");
2 26 Feb 07 jari 777             if (annotationKey.equalsIgnoreCase("Stored Color")) {
2 26 Feb 07 jari 778                 JOptionPane.showMessageDialog(new JFrame(), "Cannot link stored color to an URL. Pick a different field to link from", "Error", JOptionPane.ERROR_MESSAGE);
2 26 Feb 07 jari 779                 
2 26 Feb 07 jari 780             } else {
2 26 Feb 07 jari 781                 String[] fieldNames = data.getFieldNames();
2 26 Feb 07 jari 782                 int currFieldIndex = -1;
2 26 Feb 07 jari 783                 for (int i = 0; i < fieldNames.length; i++) {
2 26 Feb 07 jari 784                     if (annotationKey.equalsIgnoreCase(fieldNames[i])) {
2 26 Feb 07 jari 785                         currFieldIndex = i;
2 26 Feb 07 jari 786                         break;
2 26 Feb 07 jari 787                     }
2 26 Feb 07 jari 788                 }
2 26 Feb 07 jari 789                 String[][] annotationFields = getAnnotationFieldsFromFile(file);                
2 26 Feb 07 jari 790                 //System.out.println("Found annotation file");
2 26 Feb 07 jari 791                 String[] urlTemplates = annotationFields[0];
2 26 Feb 07 jari 792                 String[] urlKeys = annotationFields[1];
2 26 Feb 07 jari 793                 
2 26 Feb 07 jari 794                 if (isFound(annotationKey, urlKeys)) {
2 26 Feb 07 jari 795                     int currKeyIndex =  -1;
2 26 Feb 07 jari 796                     for (int i =0; i < urlKeys.length; i++) {
2 26 Feb 07 jari 797                         if (annotationKey.equalsIgnoreCase(urlKeys[i])) {
2 26 Feb 07 jari 798                             currKeyIndex = i;
2 26 Feb 07 jari 799                             break;
2 26 Feb 07 jari 800                         }
2 26 Feb 07 jari 801                     }
2 26 Feb 07 jari 802                     String currURLTemplate = urlTemplates[currKeyIndex];
2 26 Feb 07 jari 803        //NOTE: In the following statement, the argument "row" is what's obtained AFTER applying getGeneIndexMappedToSelectedRows(); i.e., use as is; no need to re-map for cutoffs                            
2 26 Feb 07 jari 804                     String currentAnnotationString = data.getElementAttribute(row, currFieldIndex);                    
2 26 Feb 07 jari 805                     String currentURL = getCurrentURL(annotationKey, currentAnnotationString, currURLTemplate);
2 26 Feb 07 jari 806                     try {
2 26 Feb 07 jari 807                         BrowserLauncher.openURL(currentURL);                       
2 26 Feb 07 jari 808                     }  catch (IOException ie) {
2 26 Feb 07 jari 809                         JOptionPane.showMessageDialog(new JFrame(), ie.toString(),"Error", JOptionPane.ERROR_MESSAGE);
2 26 Feb 07 jari 810                     } catch (Exception e) {
2 26 Feb 07 jari 811                         JOptionPane.showMessageDialog(new JFrame(), "Browser could not be launched! Possible problem: the annotation format may not be appropriate for this URL type!","Error", JOptionPane.ERROR_MESSAGE);
2 26 Feb 07 jari 812                     }
2 26 Feb 07 jari 813                     
2 26 Feb 07 jari 814                 } else { // if annotationKey is not found in annotation_URLs.txt file
2 26 Feb 07 jari 815                     int[] indicesToUse = new int[2];
2 26 Feb 07 jari 816                     indicesToUse[0] = currFieldIndex; //top dropdown list in dialog defaults to current annotation field
2 26 Feb 07 jari 817                     indicesToUse[1] = lastSelectedIndices[1]; // bottom dropdown list in dialog defaults to last chosen
2 26 Feb 07 jari 818                     AnnotationURLLinkDialog aDialog = new AnnotationURLLinkDialog(frame, false, experiment, data, row, file, indicesToUse);
2 26 Feb 07 jari 819                     aDialog.setVisible(true);
2 26 Feb 07 jari 820                 }
2 26 Feb 07 jari 821
2 26 Feb 07 jari 822             }
2 26 Feb 07 jari 823         } catch (Exception e) {
2 26 Feb 07 jari 824             JOptionPane.showMessageDialog(new JFrame(), "Could not open browser! Possible problems: bad URL, or \"annotation_URLs.txt\" file in \"config\" directory is not in correct format", "Error", JOptionPane.ERROR_MESSAGE);
2 26 Feb 07 jari 825             //System.out.println("Did not find file");
2 26 Feb 07 jari 826             //JOptionPane.showMessageDialog(new JFrame(), "Could not find annotation_URLs.txt file in config directory", "Error", JOptionPane.ERROR_MESSAGE);
2 26 Feb 07 jari 827         }
2 26 Feb 07 jari 828         
2 26 Feb 07 jari 829         //return indices;
2 26 Feb 07 jari 830     }    
2 26 Feb 07 jari 831     
2 26 Feb 07 jari 832     private static String getCurrentURL(String currKey, String currAnn, String currTemplate) {
2 26 Feb 07 jari 833         //System.out.println("currKey = " + currKey);
2 26 Feb 07 jari 834         String urlToUse = "";
2 26 Feb 07 jari 835         if (currKey.equalsIgnoreCase("UniGene")) {
2 26 Feb 07 jari 836             String[] splitAnnotation = currAnn.split("\\.");
2 26 Feb 07 jari 837             /*
2 26 Feb 07 jari 838             System.out.println("splitAnnotation.length = " + splitAnnotation.length);
2 26 Feb 07 jari 839             for (int i = 0; i < splitAnnotation.length; i++) {
2 26 Feb 07 jari 840                 System.out.print("splitAnnotation[" + i + "] = " + splitAnnotation[i]);
2 26 Feb 07 jari 841             }
2 26 Feb 07 jari 842              */
2 26 Feb 07 jari 843             String s1 = currTemplate.replaceAll("FIELD1", splitAnnotation[1]);
2 26 Feb 07 jari 844             urlToUse = s1.replaceAll("FIELD2", splitAnnotation[0]);
2 26 Feb 07 jari 845         } else {
2 26 Feb 07 jari 846             urlToUse = currTemplate.replaceAll("FIELD1", currAnn);
2 26 Feb 07 jari 847         }
2 26 Feb 07 jari 848         //System.out.println("url To use = " + urlToUse);
2 26 Feb 07 jari 849         return urlToUse;        
2 26 Feb 07 jari 850     }
2 26 Feb 07 jari 851     
2 26 Feb 07 jari 852     private static boolean isFound(String annKey, String[] keys) {
2 26 Feb 07 jari 853         for (int i = 0; i < keys.length; i++) {
2 26 Feb 07 jari 854             if (annKey.equalsIgnoreCase(keys[i]))
2 26 Feb 07 jari 855                 return true;
2 26 Feb 07 jari 856         }
2 26 Feb 07 jari 857         return false;
2 26 Feb 07 jari 858     }
2 26 Feb 07 jari 859     
2 26 Feb 07 jari 860     private static String[][] getAnnotationFieldsFromFile(File file) {
2 26 Feb 07 jari 861         String[][] annFields = new String[2][];
2 26 Feb 07 jari 862         Vector annotFieldsVector = new Vector();
2 26 Feb 07 jari 863         Vector urlKeysVector = new Vector();
2 26 Feb 07 jari 864         Vector urlTemplateVector = new Vector();
2 26 Feb 07 jari 865         //Vector urlDescriptionVector = new Vector();
2 26 Feb 07 jari 866         try {
2 26 Feb 07 jari 867             FileReader fr = new FileReader(file);
2 26 Feb 07 jari 868             BufferedReader buff = new BufferedReader(fr);
2 26 Feb 07 jari 869             StringSplitter st = new StringSplitter('\t');
2 26 Feb 07 jari 870             boolean eof = false;
2 26 Feb 07 jari 871             while (!eof) {
2 26 Feb 07 jari 872                 String line = buff.readLine();
2 26 Feb 07 jari 873                 if (line == null) eof = true;
2 26 Feb 07 jari 874                 else {
2 26 Feb 07 jari 875                     st.init(line);
2 26 Feb 07 jari 876                     urlKeysVector.add(st.nextToken());
2 26 Feb 07 jari 877                     urlTemplateVector.add(st.nextToken());
2 26 Feb 07 jari 878                     //urlDescriptionVector.add(st.nextToken());
2 26 Feb 07 jari 879                 }
2 26 Feb 07 jari 880             }
2 26 Feb 07 jari 881             buff.close();
2 26 Feb 07 jari 882             /*
2 26 Feb 07 jari 883             String[] urlDescriptions = new String[urlDescriptionVector.size()];
2 26 Feb 07 jari 884             for (int i = 0; i < urlDescriptions.length; i++) {
2 26 Feb 07 jari 885                 urlDescriptions[i] = (String)(urlDescriptionVector.get(i));
2 26 Feb 07 jari 886             }
2 26 Feb 07 jari 887              */
2 26 Feb 07 jari 888
2 26 Feb 07 jari 889             String[] urlTemplates = new String[urlTemplateVector.size()];
2 26 Feb 07 jari 890             String[] urlKeys = new String[urlKeysVector.size()];
2 26 Feb 07 jari 891             
2 26 Feb 07 jari 892             for (int i = 0; i < urlTemplates.length; i++) {
2 26 Feb 07 jari 893                 urlTemplates[i] = (String)(urlTemplateVector.get(i));
2 26 Feb 07 jari 894             }
2 26 Feb 07 jari 895             for (int i = 0; i < urlKeys.length; i++) {
2 26 Feb 07 jari 896                 urlKeys[i] = (String)(urlKeysVector.get(i));
2 26 Feb 07 jari 897             }
2 26 Feb 07 jari 898             annFields[0] = urlTemplates;
2 26 Feb 07 jari 899             annFields[1] = urlKeys;
2 26 Feb 07 jari 900         } catch (java.io.FileNotFoundException fne) {
2 26 Feb 07 jari 901             JOptionPane.showMessageDialog(new JFrame(), "Could not find \"annotation_URLs.txt\" file in \"config\" directory", "Error", JOptionPane.ERROR_MESSAGE);            
2 26 Feb 07 jari 902         } catch (Exception e) {
2 26 Feb 07 jari 903             JOptionPane.showMessageDialog(new JFrame(), "Incompatible \"annotation_URLs.txt\" file in \"config\" directory! Possible issues: extra newline characters, too many or too few tabs per line", "Error", JOptionPane.ERROR_MESSAGE);
2 26 Feb 07 jari 904         }
2 26 Feb 07 jari 905         return annFields;
2 26 Feb 07 jari 906     }
2 26 Feb 07 jari 907     
2 26 Feb 07 jari 908     public static int[] lastSelectedAnnotationIndices = {0,0};
2 26 Feb 07 jari 909     
2 26 Feb 07 jari 910     /*
2 26 Feb 07 jari 911     public static int[] getLastSelectedAnnotationIndices() {
2 26 Feb 07 jari 912         return lastSelectedAnnotationIndices;
2 26 Feb 07 jari 913     }
2 26 Feb 07 jari 914      **/
2 26 Feb 07 jari 915     
2 26 Feb 07 jari 916     /**
2 26 Feb 07 jari 917      * Creates array of integers with increasing order.
2 26 Feb 07 jari 918      */
2 26 Feb 07 jari 919     private static int[] createDefaultRows(final int genes) {
2 26 Feb 07 jari 920         int[] rows = new int[genes];
2 26 Feb 07 jari 921         for (int i=0; i<genes; i++) {
2 26 Feb 07 jari 922             rows[i] = i;
2 26 Feb 07 jari 923         }
2 26 Feb 07 jari 924         return rows;
2 26 Feb 07 jari 925     }
2 26 Feb 07 jari 926     
2 26 Feb 07 jari 927     private static void printDataType(PrintWriter out, Object obj, int dataType) {
2 26 Feb 07 jari 928         switch(dataType) {
2 26 Feb 07 jari 929             case ExperimentUtil.BOOLEAN_TYPE:
2 26 Feb 07 jari 930                 out.print(((Boolean)obj).booleanValue());
2 26 Feb 07 jari 931                 break;
2 26 Feb 07 jari 932             case ExperimentUtil.DOUBLE_TYPE:
2 26 Feb 07 jari 933                 out.print(((Double)obj).doubleValue());
2 26 Feb 07 jari 934                 break;
2 26 Feb 07 jari 935             case ExperimentUtil.FLOAT_TYPE:
2 26 Feb 07 jari 936                 out.print(((Float)obj).floatValue());
2 26 Feb 07 jari 937                 break;
2 26 Feb 07 jari 938             case ExperimentUtil.INTEGER_TYPE:
2 26 Feb 07 jari 939                 out.print(((Integer)obj).intValue());
2 26 Feb 07 jari 940                 break;
2 26 Feb 07 jari 941             case ExperimentUtil.STRING_TYPE:
2 26 Feb 07 jari 942                 out.print((String)obj);
2 26 Feb 07 jari 943                 break;
2 26 Feb 07 jari 944             default: 
2 26 Feb 07 jari 945                 out.print(obj);
2 26 Feb 07 jari 946                 break;
2 26 Feb 07 jari 947         }
2 26 Feb 07 jari 948         
2 26 Feb 07 jari 949         return;
2 26 Feb 07 jari 950     }
2 26 Feb 07 jari 951     
2 26 Feb 07 jari 952     private static int[] getTypes (Object[][] objData) {
2 26 Feb 07 jari 953         int[] types = new int[objData[0].length];
2 26 Feb 07 jari 954         for (int i = 0; i < types.length; i++) {
2 26 Feb 07 jari 955             types[i] = getObjectType(objData[0][i]);
2 26 Feb 07 jari 956             //Object 
2 26 Feb 07 jari 957         }
2 26 Feb 07 jari 958         return types;
2 26 Feb 07 jari 959     }
2 26 Feb 07 jari 960     
2 26 Feb 07 jari 961     private static int getObjectType(Object obj) {
2 26 Feb 07 jari 962         int obType = -1;
2 26 Feb 07 jari 963         if (obj instanceof Boolean) {
2 26 Feb 07 jari 964             return ExperimentUtil.BOOLEAN_TYPE;
2 26 Feb 07 jari 965         } else if (obj instanceof Double) {
2 26 Feb 07 jari 966             return ExperimentUtil.DOUBLE_TYPE;
2 26 Feb 07 jari 967         } else if (obj instanceof Float) {
2 26 Feb 07 jari 968             return ExperimentUtil.FLOAT_TYPE;
2 26 Feb 07 jari 969         } else if (obj instanceof Integer) {
2 26 Feb 07 jari 970             return ExperimentUtil.INTEGER_TYPE;
2 26 Feb 07 jari 971         } else if (obj instanceof String) {
2 26 Feb 07 jari 972             return ExperimentUtil.STRING_TYPE;
2 26 Feb 07 jari 973         } else {
2 26 Feb 07 jari 974             return obType;
2 26 Feb 07 jari 975         }
2 26 Feb 07 jari 976     }
2 26 Feb 07 jari 977 }
2 26 Feb 07 jari 978
2 26 Feb 07 jari 979
2 26 Feb 07 jari 980
2 26 Feb 07 jari 981
2 26 Feb 07 jari 982
2 26 Feb 07 jari 983
2 26 Feb 07 jari 984
2 26 Feb 07 jari 985
2 26 Feb 07 jari 986