mev-4.0.01/source/org/tigr/microarray/mev/cluster/algorithm/impl/tease/TEASEBench.java

Code
Comments
Other
Rev Date Author Line
2 26 Feb 07 jari 1 /*
2 26 Feb 07 jari 2  * Created on Jul 12, 2005
2 26 Feb 07 jari 3  *
2 26 Feb 07 jari 4  */
2 26 Feb 07 jari 5 package org.tigr.microarray.mev.cluster.algorithm.impl.tease;
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.File;
2 26 Feb 07 jari 9 import java.io.FileReader;
2 26 Feb 07 jari 10 import java.io.FileWriter;
2 26 Feb 07 jari 11 import java.math.BigDecimal;
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.Iterator;
2 26 Feb 07 jari 15 import java.util.Vector;
2 26 Feb 07 jari 16
2 26 Feb 07 jari 17 import org.tigr.microarray.mev.cluster.algorithm.impl.ease.HypergeometricProbability;
2 26 Feb 07 jari 18
2 26 Feb 07 jari 19 /**
2 26 Feb 07 jari 20  * TEASEBench contains all the methods required for GO term matching.
2 26 Feb 07 jari 21  * This class works like JEASEStatistics and is used by TEASE
2 26 Feb 07 jari 22  * 
2 26 Feb 07 jari 23  * @author Annie Liu
2 26 Feb 07 jari 24  * @version Jul 12, 2005
2 26 Feb 07 jari 25  */
2 26 Feb 07 jari 26 public class TEASEBench {
2 26 Feb 07 jari 27   
2 26 Feb 07 jari 28 //  private HashMap categoryMap;           
2 26 Feb 07 jari 29 //  private HashMap categoryHitsMap;
2 26 Feb 07 jari 30 //  private HashMap impliedCategory;
2 26 Feb 07 jari 31   
2 26 Feb 07 jari 32     public String population_file_name = "";  //name of the population file
2 26 Feb 07 jari 33     public String list_file_name = ""; //the name of the input file that contains a list of gene identifiers
2 26 Feb 07 jari 34     public String output_file_name;  //the name of the file which the result will be written to
2 26 Feb 07 jari 35     public Vector annotation_file_names;  //a vector that contains names of the annotation files.
2 26 Feb 07 jari 36     public int population_total;  //the total number of genes that are imported from a file and 
2 26 Feb 07 jari 37                                   //belong to categories in the annotation files
2 26 Feb 07 jari 38     public int list_total;  //the total number of genes from the sample list that belong 
2 26 Feb 07 jari 39                             //to categories in the annotation files
2 26 Feb 07 jari 40     public Hashtable categories;  //the categories from the annotation files
2 26 Feb 07 jari 41     public Hashtable categories_population;  //categories and hits in each of the 
2 26 Feb 07 jari 42                                    //categories for the population
2 26 Feb 07 jari 43     public Hashtable categories_list;  //categories and hits in each of the categories for the sample
2 26 Feb 07 jari 44     public Hashtable hitAccumulator; //stores category names and associated locus id hits 
2 26 Feb 07 jari 45                                      //as java.lang.String in java.util.Vector
2 26 Feb 07 jari 46     public HypergeometricProbability hgp_computation;  //An instance of HypergeometricProbability class
2 26 Feb 07 jari 47     public BufferedReader read_in_stream;  //A BufferedReader object used to read an external file
2 26 Feb 07 jari 48     public String[][] records;  // two dimension array to store the result
2 26 Feb 07 jari 49     public String [] categoryNames;  //ategory names
2 26 Feb 07 jari 50     public String [][] listHitMatrix;  //Category list hits
2 26 Feb 07 jari 51     public String[] orders;  //the order in which the results should be listed
2 26 Feb 07 jari 52     public boolean reportEaseScore; //indicates statistic to report
2 26 Feb 07 jari 53     private Hashtable pop_totals;  //accumulates totals for various systems 
2 26 Feb 07 jari 54     private Hashtable sample_totals;  //accumulates totals for various systems 
2 26 Feb 07 jari 55   
2 26 Feb 07 jari 56     private String sep = //"/";  //os file separator
2 26 Feb 07 jari 57       System.getProperty("file.separator");
2 26 Feb 07 jari 58   /**
2 26 Feb 07 jari 59    * Create an instance of TEASEBench
2 26 Feb 07 jari 60    * create all global variables
2 26 Feb 07 jari 61    */
2 26 Feb 07 jari 62   public TEASEBench() {
2 26 Feb 07 jari 63 //    this.categoryMap  = new HashMap();        //create the hashmap categoryMap
2 26 Feb 07 jari 64 //    this.impliedCategory = new HashMap();
2 26 Feb 07 jari 65 //    this.categoryHitsMap = new HashMap();
2 26 Feb 07 jari 66
2 26 Feb 07 jari 67     initiation();
2 26 Feb 07 jari 68     this.pop_totals = new Hashtable();
2 26 Feb 07 jari 69     this.categories = new Hashtable();
2 26 Feb 07 jari 70     this.sample_totals = new Hashtable();
2 26 Feb 07 jari 71     this.annotation_file_names = new Vector();
2 26 Feb 07 jari 72     this.categories_population = new Hashtable();
2 26 Feb 07 jari 73         
2 26 Feb 07 jari 74         this.reportEaseScore = true;
2 26 Feb 07 jari 75   }
2 26 Feb 07 jari 76   
2 26 Feb 07 jari 77   public void initiation() {
2 26 Feb 07 jari 78     this.hitAccumulator = new Hashtable();
2 26 Feb 07 jari 79     this.hgp_computation = new HypergeometricProbability();
2 26 Feb 07 jari 80     
2 26 Feb 07 jari 81     this.population_total = 0;
2 26 Feb 07 jari 82         this.list_total =0;
2 26 Feb 07 jari 83   }
2 26 Feb 07 jari 84   
2 26 Feb 07 jari 85   /**
2 26 Feb 07 jari 86    * alternative constructor that sets reportEaseScore
2 26 Feb 07 jari 87    * @param reportEaseScore
2 26 Feb 07 jari 88    */
2 26 Feb 07 jari 89   public TEASEBench(boolean reportEaseScore) {
2 26 Feb 07 jari 90     this();
2 26 Feb 07 jari 91         this.reportEaseScore = reportEaseScore;
2 26 Feb 07 jari 92   }
2 26 Feb 07 jari 93   
2 26 Feb 07 jari 94 //  /**
2 26 Feb 07 jari 95 //   * Reads in the file containing annotation information and create
2 26 Feb 07 jari 96 //   * a hashmap that maps categories to locus IDs
2 26 Feb 07 jari 97 //   * key: category, value: ArrayList of locus IDs
2 26 Feb 07 jari 98 //   * @param filePath
2 26 Feb 07 jari 99 //   */
2 26 Feb 07 jari 100 //  private HashMap getCategories(String fileName) {
2 26 Feb 07 jari 101 //    String term = fileName.substring(fileName.lastIndexOf(sep)+1, fileName.lastIndexOf("."));
2 26 Feb 07 jari 102 //    String line = "";
2 26 Feb 07 jari 103 //    String id = "";
2 26 Feb 07 jari 104 //    String cat = "";
2 26 Feb 07 jari 105 //    int ind;
2 26 Feb 07 jari 106 //    int in2;
2 26 Feb 07 jari 107 //    try {
2 26 Feb 07 jari 108 //      BufferedReader buff = new BufferedReader(new FileReader(fileName));
2 26 Feb 07 jari 109 //      
2 26 Feb 07 jari 110 //      while ((line = buff.readLine()) != null) {
2 26 Feb 07 jari 111 //        //System.out.println(line);
2 26 Feb 07 jari 112 //        ind = line.indexOf("\t");
2 26 Feb 07 jari 113 //        if (ind == -1)
2 26 Feb 07 jari 114 //          break;
2 26 Feb 07 jari 115 //        id = line.substring(0, ind);
2 26 Feb 07 jari 116 //        cat = line.substring(ind+1);
2 26 Feb 07 jari 117 //        in2 = cat.indexOf("\t");
2 26 Feb 07 jari 118 //        if (in2 == -1)
2 26 Feb 07 jari 119 //          in2 = cat.length();
2 26 Feb 07 jari 120 //        if (this.categoryMap.containsKey(id) == true) {       //if gene id already existed
2 26 Feb 07 jari 121 //          ((ArrayList)this.categoryMap.get(id)).add(term + "\t" + cat.substring(0,in2).trim());   //add the new category to the arraylist
2 26 Feb 07 jari 122 //        }
2 26 Feb 07 jari 123 //        else {                                           //if gene first appear
2 26 Feb 07 jari 124 //          ArrayList catList = new ArrayList();         //create a new arraylist for it
2 26 Feb 07 jari 125 //          catList.add(term + "\t" + cat.substring(0,in2).trim());    //add category in the arraylist
2 26 Feb 07 jari 126 //          this.categoryMap.put(id, catList);                //put the <id, categories> set in the categoryMap
2 26 Feb 07 jari 127 //        }
2 26 Feb 07 jari 128 //      }
2 26 Feb 07 jari 129 //    }catch (IOException e) {
2 26 Feb 07 jari 130 //      e.printStackTrace();
2 26 Feb 07 jari 131 //    }      
2 26 Feb 07 jari 132 //    return this.categoryMap;
2 26 Feb 07 jari 133 //  }
2 26 Feb 07 jari 134 //  
2 26 Feb 07 jari 135 //  /**
2 26 Feb 07 jari 136 //   * store implies category in a map using category (column 1) as key
2 26 Feb 07 jari 137 //   * implied categories (column 0) as value
2 26 Feb 07 jari 138 //   * <key, value> = <string, arraylist> 
2 26 Feb 07 jari 139 //   * @param filePath file path to the implies category
2 26 Feb 07 jari 140 //   * @return impliedCategory hashmap
2 26 Feb 07 jari 141 //   */
2 26 Feb 07 jari 142 //  private HashMap getImpliedCategory (String fileName) {
2 26 Feb 07 jari 143 //    String term = fileName.substring(fileName.lastIndexOf(sep)+1, fileName.lastIndexOf("."));
2 26 Feb 07 jari 144 //    int stringIndex = fileName.lastIndexOf(sep);
2 26 Feb 07 jari 145 //        String impliesFile = fileName.substring(0, stringIndex) + sep + "Implies" + sep;
2 26 Feb 07 jari 146 //        impliesFile += fileName.substring(stringIndex+1, fileName.length());
2 26 Feb 07 jari 147 //    String line = "";
2 26 Feb 07 jari 148 //    String imp = "";
2 26 Feb 07 jari 149 //    String cat = "";
2 26 Feb 07 jari 150 //    int ind;
2 26 Feb 07 jari 151 //    try {
2 26 Feb 07 jari 152 //      BufferedReader buff = new BufferedReader(new FileReader(impliesFile));
2 26 Feb 07 jari 153 //      while ((line = buff.readLine()) != null) {
2 26 Feb 07 jari 154 //        ind = line.indexOf("\t");           //in case incomplete entry
2 26 Feb 07 jari 155 //        if (ind == -1) 
2 26 Feb 07 jari 156 //          break;
2 26 Feb 07 jari 157 //        imp = term + "\t" + line.substring(0, ind);
2 26 Feb 07 jari 158 //        cat = term + "\t" + line.substring(ind+1).trim();
2 26 Feb 07 jari 159 //
2 26 Feb 07 jari 160 //        if (this.impliedCategory.containsKey(cat) == true) {       //if category already exists
2 26 Feb 07 jari 161 //          ((ArrayList)this.impliedCategory.get(cat)).add(imp);   //add the new implied category to the arraylist
2 26 Feb 07 jari 162 //        }
2 26 Feb 07 jari 163 //        else {                                           //if category first appear
2 26 Feb 07 jari 164 //          ArrayList impList = new ArrayList();         //create a new arraylist for it
2 26 Feb 07 jari 165 //          impList.add(imp);                            //add implied category in the arraylist
2 26 Feb 07 jari 166 //          this.impliedCategory.put(cat, impList);          //put the <category, implied categories> set in the categoryMap
2 26 Feb 07 jari 167 //        }
2 26 Feb 07 jari 168 //      }
2 26 Feb 07 jari 169 //    }catch (IOException e) {
2 26 Feb 07 jari 170 //      e.printStackTrace();
2 26 Feb 07 jari 171 //    }  
2 26 Feb 07 jari 172 //    return this.impliedCategory;
2 26 Feb 07 jari 173 //  }
2 26 Feb 07 jari 174 //  
2 26 Feb 07 jari 175 //
2 26 Feb 07 jari 176 //  /**
2 26 Feb 07 jari 177 //   * Search both the list of genes and find matching categories. 
2 26 Feb 07 jari 178 //   * Store all categories that has been hit at least once in categoryHitsMap.
2 26 Feb 07 jari 179 //   * value is an arraylist of genes
2 26 Feb 07 jari 180 //   *  
2 26 Feb 07 jari 181 //   * @param geneList
2 26 Feb 07 jari 182 //   * @return categoryHitsMap
2 26 Feb 07 jari 183 //   */
2 26 Feb 07 jari 184 //  private HashMap getCategoryHitsMap(ArrayList geneList) {
2 26 Feb 07 jari 185 //    String id;
2 26 Feb 07 jari 186 //    String category;
2 26 Feb 07 jari 187 //    String impcat;
2 26 Feb 07 jari 188 //    ArrayList categoryList;
2 26 Feb 07 jari 189 //    ArrayList impliedList;
2 26 Feb 07 jari 190 //    for (int i = 0; i < geneList.size(); i++) {    //iterate through geneList
2 26 Feb 07 jari 191 //      id = (String)geneList.get(i);
2 26 Feb 07 jari 192 //      if (this.categoryMap.containsKey(id)) {         //if gene is listed in categoryMap
2 26 Feb 07 jari 193 //        categoryList = (ArrayList)this.categoryMap.get(id);    //pull out all the categories it belongs to
2 26 Feb 07 jari 194 //        
2 26 Feb 07 jari 195 //        for (int j = 0; j < categoryList.size(); j++) {
2 26 Feb 07 jari 196 //          category = (String)categoryList.get(j);
2 26 Feb 07 jari 197 //          checkAndAddCategory(category, id);             //check if category already exist in hitMap
2 26 Feb 07 jari 198 //          if (this.impliedCategory.containsKey(category)) {
2 26 Feb 07 jari 199 //            impliedList = (ArrayList)this.impliedCategory.get(category);
2 26 Feb 07 jari 200 //            for(int k = 0; k < impliedList.size(); k++) {
2 26 Feb 07 jari 201 //              impcat = (String)impliedList.get(k);
2 26 Feb 07 jari 202 //              checkAndAddCategory(impcat, id);
2 26 Feb 07 jari 203 //            }
2 26 Feb 07 jari 204 //          }
2 26 Feb 07 jari 205 //        }
2 26 Feb 07 jari 206 //      }
2 26 Feb 07 jari 207 //    }
2 26 Feb 07 jari 208 //    return this.categoryHitsMap;
2 26 Feb 07 jari 209 //  }
2 26 Feb 07 jari 210 //
2 26 Feb 07 jari 211 //  /**
2 26 Feb 07 jari 212 //   * Check if category already exist. If yes, extrack the arraylist of 
2 26 Feb 07 jari 213 //   * genes and add in a new id. If not, create a new arraylist and push 
2 26 Feb 07 jari 214 //   * <category, list> in categoryHitsMap
2 26 Feb 07 jari 215 //   * @param category
2 26 Feb 07 jari 216 //   * @param id
2 26 Feb 07 jari 217 //   */
2 26 Feb 07 jari 218 //  private void checkAndAddCategory(String category, String id) {
2 26 Feb 07 jari 219 //    ArrayList hitList;
2 26 Feb 07 jari 220 //    if(this.categoryHitsMap.containsKey(category)) {
2 26 Feb 07 jari 221 //      hitList = (ArrayList)this.categoryHitsMap.get(category);
2 26 Feb 07 jari 222 //      hitList.add(id);
2 26 Feb 07 jari 223 //    }
2 26 Feb 07 jari 224 //    else {
2 26 Feb 07 jari 225 //      hitList = new ArrayList();
2 26 Feb 07 jari 226 //      hitList.add(id);
2 26 Feb 07 jari 227 //      this.categoryHitsMap.put(category, hitList);
2 26 Feb 07 jari 228 //    }
2 26 Feb 07 jari 229 //  }
2 26 Feb 07 jari 230 //  
2 26 Feb 07 jari 231 //  /**
2 26 Feb 07 jari 232 //   * returen a reference of categoryMap
2 26 Feb 07 jari 233 //   * @return categoryMap
2 26 Feb 07 jari 234 //   */
2 26 Feb 07 jari 235 //  private HashMap getCategoryMap() {
2 26 Feb 07 jari 236 //    return this.categoryMap;
2 26 Feb 07 jari 237 //  }
2 26 Feb 07 jari 238 //  
2 26 Feb 07 jari 239 //  public static void main(String[] args) {
2 26 Feb 07 jari 240 //    TEASEBench bench = new TEASEBench();
2 26 Feb 07 jari 241 //    ArrayList geneList = new ArrayList();
2 26 Feb 07 jari 242 //    geneList.add("6352");
2 26 Feb 07 jari 243 //    geneList.add("7849");
2 26 Feb 07 jari 244 //    String fileName = "C:/MeV3.1/data/ease/Data/Class/GO Biological Process.txt";
2 26 Feb 07 jari 245 //    HashMap map = bench.getCategories(fileName);
2 26 Feb 07 jari 246 //    HashMap map1 = bench.getImpliedCategory(fileName);
2 26 Feb 07 jari 247 //    HashMap hits = bench.getCategoryHitsMap(geneList);
2 26 Feb 07 jari 248 //    
2 26 Feb 07 jari 249 //
2 26 Feb 07 jari 250 //    Set keys = hits.keySet();
2 26 Feb 07 jari 251 //    Iterator it = keys.iterator();
2 26 Feb 07 jari 252 //
2 26 Feb 07 jari 253 //    while (it.hasNext()) {
2 26 Feb 07 jari 254 //      String id = (String)it.next();
2 26 Feb 07 jari 255 //      System.out.println(id + ": " + hits.get(id));
2 26 Feb 07 jari 256 //    }
2 26 Feb 07 jari 257 //
2 26 Feb 07 jari 258 //  }
2 26 Feb 07 jari 259     
2 26 Feb 07 jari 260     /** 
2 26 Feb 07 jari 261      * Add a new annotation file name into the annotation 
2 26 Feb 07 jari 262      * file name list. 
2 26 Feb 07 jari 263      */
2 26 Feb 07 jari 264     public void AddAnnotationFileName(String file_name) {
2 26 Feb 07 jari 265         annotation_file_names.addElement(file_name);
2 26 Feb 07 jari 266     }
2 26 Feb 07 jari 267     
2 26 Feb 07 jari 268     /**  
2 26 Feb 07 jari 269      * Obtain the categories from the annotation files and create a hashtable 
2 26 Feb 07 jari 270      * using these categories as keys.
2 26 Feb 07 jari 271      * <key, value>, key = unrepeated categories, value = hashtable using 
2 26 Feb 07 jari 272      * locus ID (integers) as keys, value = ""
2 26 Feb 07 jari 273      * store all categories found in the annotation file(s)
2 26 Feb 07 jari 274      * called by performSlideAnnotationAnalysis() in EASEAnalysis
2 26 Feb 07 jari 275      */
2 26 Feb 07 jari 276     public void GetCategories() {
2 26 Feb 07 jari 277         BufferedReader in = null;
2 26 Feb 07 jari 278         Hashtable hash_table = new Hashtable();
2 26 Feb 07 jari 279         Hashtable implied_associations = new Hashtable();
2 26 Feb 07 jari 280         String term;
2 26 Feb 07 jari 281         String line="", category="", file_name="";
2 26 Feb 07 jari 282         int idx;
2 26 Feb 07 jari 283         int c =0;
2 26 Feb 07 jari 284         int idx2;        
2 26 Feb 07 jari 285         
2 26 Feb 07 jari 286         try{
2 26 Feb 07 jari 287             for(Enumeration e = annotation_file_names.elements(); e.hasMoreElements();){
2 26 Feb 07 jari 288                 file_name = e.nextElement().toString();
2 26 Feb 07 jari 289                 in = new BufferedReader(new FileReader(file_name));
2 26 Feb 07 jari 290                 
2 26 Feb 07 jari 291                 //use last index of . in case a user has a . in the path.
2 26 Feb 07 jari 292                 term = file_name.substring(file_name.lastIndexOf(sep)+1, file_name.lastIndexOf("."));
2 26 Feb 07 jari 293                 
2 26 Feb 07 jari 294                 //store terms in total hits accumulators
2 26 Feb 07 jari 295                 this.sample_totals.put(term, new Hashtable());
2 26 Feb 07 jari 296                 this.pop_totals.put(term, new Hashtable());
2 26 Feb 07 jari 297                 
2 26 Feb 07 jari 298                 while((line = in.readLine()) != null){      
2 26 Feb 07 jari 299                     idx = line.indexOf("\t");
2 26 Feb 07 jari 300                     
2 26 Feb 07 jari 301                     if( idx >= line.length() || idx < 1){
2 26 Feb 07 jari 302                         continue;
2 26 Feb 07 jari 303                     }
2 26 Feb 07 jari 304                     
2 26 Feb 07 jari 305                     //put this in to guard against no trailing tab (jcb)
2 26 Feb 07 jari 306                     idx2 = line.indexOf("\t", idx+1);
2 26 Feb 07 jari 307                     if(idx2 >= line.length() || idx2 < 1)
2 26 Feb 07 jari 308                         idx2 = line.length();
2 26 Feb 07 jari 309                     
2 26 Feb 07 jari 310                     category = term + "\t" + line.substring(idx+1, idx2).trim();
2 26 Feb 07 jari 311                     
2 26 Feb 07 jari 312                     if(!categories.containsKey(category)){
2 26 Feb 07 jari 313                         categories.put(category, new Hashtable());   //key = locus ID, value = ""
2 26 Feb 07 jari 314                         ((Hashtable) categories.get(category)).put(line.substring(0,idx).trim(), "");
2 26 Feb 07 jari 315                     }else{
2 26 Feb 07 jari 316                         ((Hashtable) categories.get(category)).put(line.substring(0,idx).trim(), "");
2 26 Feb 07 jari 317                     }
2 26 Feb 07 jari 318                 }
2 26 Feb 07 jari 319             }
2 26 Feb 07 jari 320
2 26 Feb 07 jari 321             //System.out.println("categories size = "+categories.size());
2 26 Feb 07 jari 322             
2 26 Feb 07 jari 323             //(jcb)
2 26 Feb 07 jari 324             //create hash table for implies using (implies_associator)
2 26 Feb 07 jari 325             //This will then be used to add implied categories
2 26 Feb 07 jari 326             String fileName, impliesFile;
2 26 Feb 07 jari 327             int stringIndex;
2 26 Feb 07 jari 328             for(int i = 0; i < annotation_file_names.size(); i++){
2 26 Feb 07 jari 329                 fileName = (String)annotation_file_names.elementAt(i);
2 26 Feb 07 jari 330                 stringIndex = fileName.lastIndexOf(sep);
2 26 Feb 07 jari 331                 impliesFile = fileName.substring(0, stringIndex) + sep+"Implies"+sep;
2 26 Feb 07 jari 332                 impliesFile += fileName.substring(stringIndex+1, fileName.length());
2 26 Feb 07 jari 333                 
2 26 Feb 07 jari 334                 //System.out.println("implies file = "+impliesFile);
2 26 Feb 07 jari 335                 
2 26 Feb 07 jari 336                 File file = new File(impliesFile);
2 26 Feb 07 jari 337                 if(!file.exists() || !file.isFile())  //if implies file is missing move on
2 26 Feb 07 jari 338                     continue;
2 26 Feb 07 jari 339                 
2 26 Feb 07 jari 340                 in = new BufferedReader(new FileReader(impliesFile));
2 26 Feb 07 jari 341                 //term = fileName.substring(file_name.lastIndexOf("/")+1, file_name.indexOf("."));
2 26 Feb 07 jari 342                 term = fileName.substring(fileName.lastIndexOf(sep)+1, fileName.lastIndexOf("."));
2 26 Feb 07 jari 343                 
2 26 Feb 07 jari 344                 
2 26 Feb 07 jari 345                 while((line = in.readLine()) != null){
2 26 Feb 07 jari 346                     idx = line.indexOf('\t');
2 26 Feb 07 jari 347                     
2 26 Feb 07 jari 348                     if(idx >= line.length() || idx < 1)  //must include a tab
2 26 Feb 07 jari 349                         continue;
2 26 Feb 07 jari 350                     
2 26 Feb 07 jari 351                     if(!implied_associations.containsKey(term + "\t" +line.substring(0,idx).trim())){
2 26 Feb 07 jari 352                         implied_associations.put(term + "\t" +line.substring(0,idx).trim(), new Vector());
2 26 Feb 07 jari 353                         ((Vector)(implied_associations.get(term + "\t" +line.substring(0,idx).trim()))).addElement(term + "\t" + line.substring(idx, line.length()).trim());
2 26 Feb 07 jari 354                     } else {
2 26 Feb 07 jari 355                         ((Vector)(implied_associations.get(term + "\t" +line.substring(0,idx).trim()))).addElement(term + "\t" + line.substring(idx, line.length()).trim());
2 26 Feb 07 jari 356                     }
2 26 Feb 07 jari 357                 }
2 26 Feb 07 jari 358             }
2 26 Feb 07 jari 359             boolean end = false;
2 26 Feb 07 jari 360             //(jcb) append associated categories to the list
2 26 Feb 07 jari 361             for(int k = 0; k < 10 && !end; k++){  // !end will short circuit if stable
2 26 Feb 07 jari 362                 String cat="";
2 26 Feb 07 jari 363                 Hashtable catHash;
2 26 Feb 07 jari 364                 Vector impVector;
2 26 Feb 07 jari 365                 String impCat;
2 26 Feb 07 jari 366
2 26 Feb 07 jari 367                 end = true;  //start at true until no new indices are inserted
2 26 Feb 07 jari 368                 
2 26 Feb 07 jari 369                 for( Enumeration enum = implied_associations.keys(); enum.hasMoreElements(); ){
2 26 Feb 07 jari 370                     
2 26 Feb 07 jari 371                     cat = (String) enum.nextElement();
2 26 Feb 07 jari 372                     
2 26 Feb 07 jari 373                     //if the category is represented, add the implied associations if they don't exist
2 26 Feb 07 jari 374                     if(categories.containsKey(cat)){
2 26 Feb 07 jari 375                         catHash = (Hashtable)categories.get(cat);
2 26 Feb 07 jari 376                         
2 26 Feb 07 jari 377                         //associated categories
2 26 Feb 07 jari 378                         impVector = ((Vector)implied_associations.get(cat));
2 26 Feb 07 jari 379                         
2 26 Feb 07 jari 380                         for(int i = 0; i < impVector.size(); i++){
2 26 Feb 07 jari 381                             
2 26 Feb 07 jari 382                             impCat = ((String)impVector.elementAt(i));
2 26 Feb 07 jari 383                             
2 26 Feb 07 jari 384                             if(!categories.containsKey(impCat)){
2 26 Feb 07 jari 385                                 end = false;
2 26 Feb 07 jari 386                                 categories.put(impCat, new Hashtable());
2 26 Feb 07 jari 387                                 for(Enumeration categoryEnum = catHash.keys(); categoryEnum.hasMoreElements();){
2 26 Feb 07 jari 388                                     ((Hashtable)categories.get(impCat)).put((String)categoryEnum.nextElement(), "");
2 26 Feb 07 jari 389                                 }
2 26 Feb 07 jari 390                             } else {  //category exists, need to append locus link numbers
2 26 Feb 07 jari 391                                 for(Enumeration categoryEnum = catHash.keys(); categoryEnum.hasMoreElements();){
2 26 Feb 07 jari 392                                     String indexString = (String)categoryEnum.nextElement();
2 26 Feb 07 jari 393                                     if(!((Hashtable)categories.get(impCat)).containsKey(indexString) ){
2 26 Feb 07 jari 394                                         // ((Hashtable)categories.get(impCat)).put((String)categoryEnum.nextElement(), "");
2 26 Feb 07 jari 395                                         ((Hashtable)categories.get(impCat)).put(indexString, "");
2 26 Feb 07 jari 396                                         
2 26 Feb 07 jari 397                                         end = false;
2 26 Feb 07 jari 398                                     }
2 26 Feb 07 jari 399                                 }
2 26 Feb 07 jari 400                             }
2 26 Feb 07 jari 401                         }
2 26 Feb 07 jari 402                     }
2 26 Feb 07 jari 403                 }
2 26 Feb 07 jari 404             }
2 26 Feb 07 jari 405 //            System.out.println(categories.size());
2 26 Feb 07 jari 406 //            Set arr = (Set)categories.keySet(); //***************************************************
2 26 Feb 07 jari 407 //            Iterator it = arr.iterator();
2 26 Feb 07 jari 408 //            while (it.hasNext()) {
2 26 Feb 07 jari 409 //              Object ob = it.next();
2 26 Feb 07 jari 410 //              System.out.println(ob+" -----> "+categories.get(ob));
2 26 Feb 07 jari 411 //            }
2 26 Feb 07 jari 412             
2 26 Feb 07 jari 413         }catch(Exception error){
2 26 Feb 07 jari 414             System.out.println("Error occured collecting categories");
2 26 Feb 07 jari 415             System.out.println(error.getMessage());
2 26 Feb 07 jari 416             error.printStackTrace();
2 26 Feb 07 jari 417         }
2 26 Feb 07 jari 418 //        System.out.println("JEASESTatistics");  //*******************************************
2 26 Feb 07 jari 419 //        Set keySet = categories.keySet();
2 26 Feb 07 jari 420 //        Iterator it = keySet.iterator();
2 26 Feb 07 jari 421 //        while(it.hasNext()) {
2 26 Feb 07 jari 422 //          Hashtable hash = (Hashtable)categories.get(it.next());
2 26 Feb 07 jari 423 //          Set id = hash.keySet();
2 26 Feb 07 jari 424 //          Iterator i = id.iterator();
2 26 Feb 07 jari 425 //          while(i.hasNext())
2 26 Feb 07 jari 426 //            System.out.println(i.next());
2 26 Feb 07 jari 427 //        }
2 26 Feb 07 jari 428     }
2 26 Feb 07 jari 429
2 26 Feb 07 jari 430     
2 26 Feb 07 jari 431     /**  
2 26 Feb 07 jari 432      * Obtain the categories from the annotation files and create a hashtable 
2 26 Feb 07 jari 433      * using these categories as keys.
2 26 Feb 07 jari 434      * only store categories that are present in the population list
2 26 Feb 07 jari 435      * called by performClusterAnnotationAnalysis() in EASEAnalysis
2 26 Feb 07 jari 436      * 
2 26 Feb 07 jari 437      * @param popVector population gene list
2 26 Feb 07 jari 438      * */
2 26 Feb 07 jari 439     public void GetCategories(Vector popVector) {
2 26 Feb 07 jari 440         BufferedReader in = null;
2 26 Feb 07 jari 441         Hashtable hash_table = new Hashtable();
2 26 Feb 07 jari 442         Hashtable implied_associations = new Hashtable();
2 26 Feb 07 jari 443         String term;
2 26 Feb 07 jari 444         String line="", category="", file_name="";
2 26 Feb 07 jari 445         int idx;
2 26 Feb 07 jari 446     int c = 0;
2 26 Feb 07 jari 447     int idx2;        
2 26 Feb 07 jari 448         
2 26 Feb 07 jari 449         try{
2 26 Feb 07 jari 450             for(Enumeration e = this.annotation_file_names.elements(); e.hasMoreElements();){
2 26 Feb 07 jari 451                 file_name = e.nextElement().toString();
2 26 Feb 07 jari 452                 in = new BufferedReader(new FileReader(file_name));
2 26 Feb 07 jari 453                 
2 26 Feb 07 jari 454                 //use last index of . in case a user has a . in the path.
2 26 Feb 07 jari 455                 term = file_name.substring(file_name.lastIndexOf(this.sep)+1, file_name.lastIndexOf("."));
2 26 Feb 07 jari 456                 
2 26 Feb 07 jari 457                 //store terms in total hits accumulators
2 26 Feb 07 jari 458                 this.sample_totals.put(term, new Hashtable());
2 26 Feb 07 jari 459                 this.pop_totals.put(term, new Hashtable());
2 26 Feb 07 jari 460                 
2 26 Feb 07 jari 461                 while((line = in.readLine()) != null){
2 26 Feb 07 jari 462                     idx = line.indexOf("\t");
2 26 Feb 07 jari 463                     
2 26 Feb 07 jari 464                     if( idx >= line.length() || idx < 1){
2 26 Feb 07 jari 465                         continue;
2 26 Feb 07 jari 466                     }
2 26 Feb 07 jari 467                     
2 26 Feb 07 jari 468                     //put this in to guard against no trailing tab (jcb)
2 26 Feb 07 jari 469                     idx2 = line.indexOf("\t", idx+1);
2 26 Feb 07 jari 470                     if(idx2 >= line.length() || idx2 < 1)
2 26 Feb 07 jari 471                         idx2 = line.length();
2 26 Feb 07 jari 472                     
2 26 Feb 07 jari 473                     category = term + "\t" + line.substring(idx+1, idx2).trim();
2 26 Feb 07 jari 474                     if(popVector.contains(line.substring(0,idx).trim())) {
2 26 Feb 07 jari 475                         if(!this.categories.containsKey(category)){
2 26 Feb 07 jari 476                             this.categories.put(category, new Hashtable());
2 26 Feb 07 jari 477                             ((Hashtable) this.categories.get(category)).put(line.substring(0,idx).trim(), "");
2 26 Feb 07 jari 478                         }else{
2 26 Feb 07 jari 479                             ((Hashtable) this.categories.get(category)).put(line.substring(0,idx).trim(), "");
2 26 Feb 07 jari 480                         }
2 26 Feb 07 jari 481                     }
2 26 Feb 07 jari 482                 }
2 26 Feb 07 jari 483             }
2 26 Feb 07 jari 484                         
2 26 Feb 07 jari 485             //create hash table for implies using (implies_associator)
2 26 Feb 07 jari 486             //This will then be used to add implied categories
2 26 Feb 07 jari 487             String fileName, impliesFile;
2 26 Feb 07 jari 488             int stringIndex;
2 26 Feb 07 jari 489             for(int i = 0; i < annotation_file_names.size(); i++){
2 26 Feb 07 jari 490                 fileName = (String)annotation_file_names.elementAt(i);
2 26 Feb 07 jari 491                 stringIndex = fileName.lastIndexOf(sep);
2 26 Feb 07 jari 492                 impliesFile = fileName.substring(0, stringIndex) + sep+"Implies"+sep;
2 26 Feb 07 jari 493                 impliesFile += fileName.substring(stringIndex+1, fileName.length());
2 26 Feb 07 jari 494                  
2 26 Feb 07 jari 495                 File file = new File(impliesFile);
2 26 Feb 07 jari 496                 if(!file.exists() || !file.isFile())  //if implies file is missing move on
2 26 Feb 07 jari 497                     continue;
2 26 Feb 07 jari 498                 
2 26 Feb 07 jari 499                 in = new BufferedReader(new FileReader(impliesFile));
2 26 Feb 07 jari 500                 //term = fileName.substring(file_name.lastIndexOf("/")+1, file_name.indexOf("."));
2 26 Feb 07 jari 501                 term = fileName.substring(fileName.lastIndexOf(sep)+1, fileName.lastIndexOf("."));
2 26 Feb 07 jari 502                 
2 26 Feb 07 jari 503                 
2 26 Feb 07 jari 504                 while((line = in.readLine()) != null){
2 26 Feb 07 jari 505                     idx = line.indexOf('\t');
2 26 Feb 07 jari 506                     
2 26 Feb 07 jari 507                     if(idx >= line.length() || idx < 1)  //must include a tab
2 26 Feb 07 jari 508                         continue;
2 26 Feb 07 jari 509                     
2 26 Feb 07 jari 510                     if(!implied_associations.containsKey(term + "\t" +line.substring(0,idx).trim())){
2 26 Feb 07 jari 511                         implied_associations.put(term + "\t" +line.substring(0,idx).trim(), new Vector());
2 26 Feb 07 jari 512                         ((Vector)(implied_associations.get(term + "\t" +line.substring(0,idx).trim()))).addElement(term + "\t" + line.substring(idx, line.length()).trim());
2 26 Feb 07 jari 513                     } else {
2 26 Feb 07 jari 514                         ((Vector)(implied_associations.get(term + "\t" +line.substring(0,idx).trim()))).addElement(term + "\t" + line.substring(idx, line.length()).trim());
2 26 Feb 07 jari 515                     }
2 26 Feb 07 jari 516                 }
2 26 Feb 07 jari 517             }
2 26 Feb 07 jari 518             
2 26 Feb 07 jari 519             boolean end = false;
2 26 Feb 07 jari 520             
2 26 Feb 07 jari 521             //(jcb) append associated categories to the list
2 26 Feb 07 jari 522             for(int k = 0; k < 10 && !end; k++){  // !end will short circuit if stable
2 26 Feb 07 jari 523                 String cat="";
2 26 Feb 07 jari 524                 Hashtable catHash;
2 26 Feb 07 jari 525                 Vector impVector;
2 26 Feb 07 jari 526                 String impCat;
2 26 Feb 07 jari 527
2 26 Feb 07 jari 528                 end = true;  //start at true until no new indices are inserted
2 26 Feb 07 jari 529                 
2 26 Feb 07 jari 530                 for( Enumeration enum = implied_associations.keys(); enum.hasMoreElements(); ){
2 26 Feb 07 jari 531                     
2 26 Feb 07 jari 532                     cat = (String) enum.nextElement();
2 26 Feb 07 jari 533                     
2 26 Feb 07 jari 534                     //if the category is represented, add the implied associations if they don't exist
2 26 Feb 07 jari 535                     if(categories.containsKey(cat)){
2 26 Feb 07 jari 536                         catHash = (Hashtable)categories.get(cat);
2 26 Feb 07 jari 537                         
2 26 Feb 07 jari 538                         //associated categories
2 26 Feb 07 jari 539                         impVector = ((Vector)implied_associations.get(cat));
2 26 Feb 07 jari 540                         
2 26 Feb 07 jari 541                         for(int i = 0; i < impVector.size(); i++){
2 26 Feb 07 jari 542                             
2 26 Feb 07 jari 543                             impCat = ((String)impVector.elementAt(i));
2 26 Feb 07 jari 544                             
2 26 Feb 07 jari 545                             if(!categories.containsKey(impCat)){
2 26 Feb 07 jari 546                                 end = false;
2 26 Feb 07 jari 547                                 categories.put(impCat, new Hashtable());
2 26 Feb 07 jari 548                                 for(Enumeration categoryEnum = catHash.keys(); categoryEnum.hasMoreElements();){
2 26 Feb 07 jari 549                                     ((Hashtable)categories.get(impCat)).put((String)categoryEnum.nextElement(), "");
2 26 Feb 07 jari 550                                 }
2 26 Feb 07 jari 551                             } else {  //category exists, need to append locus link numbers
2 26 Feb 07 jari 552                                 for(Enumeration categoryEnum = catHash.keys(); categoryEnum.hasMoreElements();){
2 26 Feb 07 jari 553                                     String indexString = (String)categoryEnum.nextElement();
2 26 Feb 07 jari 554                                     if(!((Hashtable)categories.get(impCat)).containsKey(indexString) ){
2 26 Feb 07 jari 555                                         ((Hashtable)categories.get(impCat)).put(indexString, "");
2 26 Feb 07 jari 556                                         
2 26 Feb 07 jari 557                                         end = false;
2 26 Feb 07 jari 558                                     }
2 26 Feb 07 jari 559                                 }
2 26 Feb 07 jari 560                             }
2 26 Feb 07 jari 561                         }
2 26 Feb 07 jari 562                     }
2 26 Feb 07 jari 563                 }
2 26 Feb 07 jari 564             }
2 26 Feb 07 jari 565             
2 26 Feb 07 jari 566             
2 26 Feb 07 jari 567         }catch(Exception error){
2 26 Feb 07 jari 568             System.out.println("Error occured collecting categories");
2 26 Feb 07 jari 569             System.out.println(error.getMessage());
2 26 Feb 07 jari 570             error.printStackTrace();
2 26 Feb 07 jari 571         }
2 26 Feb 07 jari 572     }    
2 26 Feb 07 jari 573     
2 26 Feb 07 jari 574     /** 
2 26 Feb 07 jari 575      * Creates the tab-delimited results containing information on system, 
2 26 Feb 07 jari 576      * category, population hits, population total, sample hits
2 26 Feb 07 jari 577      * and sample totoal, and the one-tailed hypergeometric probability 
2 26 Feb 07 jari 578      * score of over-representation.
2 26 Feb 07 jari 579      * categories_list stores all categories hits in the sample
2 26 Feb 07 jari 580      * categories_population contains categories hits in the population file
2 26 Feb 07 jari 581      */
2 26 Feb 07 jari 582     public void ConstructResults() {
2 26 Feb 07 jari 583         
2 26 Feb 07 jari 584         String key="", list_hit="", population_hit="", temp="";
2 26 Feb 07 jari 585         this.records = new String[categories_list.size()][7];
2 26 Feb 07 jari 586         this.orders = new String[categories_list.size()];
2 26 Feb 07 jari 587         
2 26 Feb 07 jari 588         int c=0;
2 26 Feb 07 jari 589         //System.out.println(categories_list.isEmpty());
2 26 Feb 07 jari 590         for(Enumeration e = categories_list.keys(); e.hasMoreElements();){  //iterate through categories_list
2 26 Feb 07 jari 591             key = (String)(e.nextElement());                                //hashtable
2 26 Feb 07 jari 592             list_hit="";
2 26 Feb 07 jari 593             population_hit="";
2 26 Feb 07 jari 594
2 26 Feb 07 jari 595             temp = key.substring(0, key.indexOf('\t')).trim();
2 26 Feb 07 jari 596             records[c][0] = temp.substring(temp.lastIndexOf(sep)+1).trim();
2 26 Feb 07 jari 597             records[c][1] = key.substring(key.indexOf('\t')).trim();
2 26 Feb 07 jari 598             
2 26 Feb 07 jari 599             list_hit = (String)(categories_list.get(key));
2 26 Feb 07 jari 600             
2 26 Feb 07 jari 601             records[c][2] = list_hit;
2 26 Feb 07 jari 602             
2 26 Feb 07 jari 603             list_total = ((Hashtable)(sample_totals.get(temp))).size();
2 26 Feb 07 jari 604             
2 26 Feb 07 jari 605             records[c][3] = String.valueOf(list_total);
2 26 Feb 07 jari 606             
2 26 Feb 07 jari 607             if(categories_population.containsKey(key)){
2 26 Feb 07 jari 608                 population_hit = (String)(categories_population.get(key));
2 26 Feb 07 jari 609                 records[c][4] = population_hit;
2 26 Feb 07 jari 610             }else{
2 26 Feb 07 jari 611                 records[c][4] = "";
2 26 Feb 07 jari 612             }
2 26 Feb 07 jari 613             
2 26 Feb 07 jari 614             population_total = ((Hashtable)(pop_totals.get(temp))).size();
2 26 Feb 07 jari 615             
2 26 Feb 07 jari 616             records[c][5] = String.valueOf(population_total);
2 26 Feb 07 jari 617             
2 26 Feb 07 jari 618             if(!list_hit.equals("") && !population_hit.equals("")){
2 26 Feb 07 jari 619                 double p;
2 26 Feb 07 jari 620                 //System.out.println("population_total = "+ population_total+"   list_total = "+list_total);
2 26 Feb 07 jari 621                 if(reportEaseScore){  //report ease score (jcb) added option of EASEScore
2 26 Feb 07 jari 622                     if(Integer.parseInt(list_hit) > 1){
2 26 Feb 07 jari 623                         p = hgp_computation.SumHGP(population_total, Integer.parseInt(population_hit), list_total-1, Integer.parseInt(list_hit)-1);
2 26 Feb 07 jari 624                     } else{
2 26 Feb 07 jari 625                         p = 1.0;
2 26 Feb 07 jari 626                     }
2 26 Feb 07 jari 627                 } else {  //else report Fisher Exact Prob.
2 26 Feb 07 jari 628                     p = hgp_computation.SumHGP(population_total, Integer.parseInt(population_hit), list_total, Integer.parseInt(list_hit));
2 26 Feb 07 jari 629                 }
2 26 Feb 07 jari 630                 records[c][6] = String.valueOf(p);
2 26 Feb 07 jari 631             }else{
2 26 Feb 07 jari 632               
2 26 Feb 07 jari 633                 records[c][6] = "1.0";
2 26 Feb 07 jari 634             }
2 26 Feb 07 jari 635             c++;
2 26 Feb 07 jari 636         }
2 26 Feb 07 jari 637         
2 26 Feb 07 jari 638         //jcb
2 26 Feb 07 jari 639         //accumulates hits for the result matrix and the category names
2 26 Feb 07 jari 640         categoryNames = new String[hitAccumulator.size()];
2 26 Feb 07 jari 641         listHitMatrix = new String[categoryNames.length][];
2 26 Feb 07 jari 642         
2 26 Feb 07 jari 643         int cnt = 0;
2 26 Feb 07 jari 644         Vector locusVector;
2 26 Feb 07 jari 645         //System.out.println("JEASEStatistics 585");
2 26 Feb 07 jari 646         for(Enumeration e = hitAccumulator.keys(); e.hasMoreElements();){
2 26 Feb 07 jari 647             String cat = (String)(e.nextElement());
2 26 Feb 07 jari 648             categoryNames[cnt] = cat;
2 26 Feb 07 jari 649             //System.out.print(cat + ": ");
2 26 Feb 07 jari 650             locusVector = ((Vector)hitAccumulator.get(cat));
2 26 Feb 07 jari 651             listHitMatrix[cnt] = new String[locusVector.size()];
2 26 Feb 07 jari 652             for(int i = 0; i < listHitMatrix[cnt].length; i++){
2 26 Feb 07 jari 653                 listHitMatrix[cnt][i] = ((String)locusVector.elementAt(i));
2 26 Feb 07 jari 654
2 26 Feb 07 jari 655                 //System.out.print(listHitMatrix[cnt][i] + " ");  //*****************************
2 26 Feb 07 jari 656             }
2 26 Feb 07 jari 657             cnt++;
2 26 Feb 07 jari 658             //System.out.println();
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     
2 26 Feb 07 jari 664     /** Sort the results in the ascending order of p-score and write the tab-delimited 
2 26 Feb 07 jari 665      * result to a file and standard IO output
2 26 Feb 07 jari 666      */
2 26 Feb 07 jari 667     public void SortRecords() {
2 26 Feb 07 jari 668         BigDecimal big1, big2;
2 26 Feb 07 jari 669         String holder;
2 26 Feb 07 jari 670         
2 26 Feb 07 jari 671         for(int i0=0; i0 < records.length; i0++){
2 26 Feb 07 jari 672             for(int i=0; i < records.length-1 - i0; i++){
2 26 Feb 07 jari 673                 big1 = new BigDecimal(records[i][6]);
2 26 Feb 07 jari 674                 big2 = new BigDecimal(records[i+1][6]);
2 26 Feb 07 jari 675                 
2 26 Feb 07 jari 676                 if(big1.compareTo(big2) > 0){
2 26 Feb 07 jari 677                   for (int j = 0; j < 7; j++) {
2 26 Feb 07 jari 678                       holder = records[i][j];
2 26 Feb 07 jari 679                       records[i][j] = records[i+1][j];
2 26 Feb 07 jari 680                       records[i+1][j] = holder;
2 26 Feb 07 jari 681                   }
2 26 Feb 07 jari 682                 }
2 26 Feb 07 jari 683             }
2 26 Feb 07 jari 684         }
2 26 Feb 07 jari 685     }
2 26 Feb 07 jari 686     
2 26 Feb 07 jari 687     /** 
2 26 Feb 07 jari 688      * Writes the tab-delimited results to the specified output file and to 
2 26 Feb 07 jari 689      * the standout IO output as well 
2 26 Feb 07 jari 690      */
2 26 Feb 07 jari 691     public void DisplayResults() {
2 26 Feb 07 jari 692         try{
2 26 Feb 07 jari 693             File output_file = new File(output_file_name);
2 26 Feb 07 jari 694             FileWriter output_writer = new FileWriter(output_file); //, false);
2 26 Feb 07 jari 695             
2 26 Feb 07 jari 696             for(int i=0; i < records.length; i++){
2 26 Feb 07 jari 697               for (int j = 0; j < 6; j++) {
2 26 Feb 07 jari 698                 System.out.print(records[i][j] + "\t");
2 26 Feb 07 jari 699                   output_writer.write(records[i][j] + "\t");
2 26 Feb 07 jari 700                   output_writer.write("\n");
2 26 Feb 07 jari 701               }
2 26 Feb 07 jari 702             }
2 26 Feb 07 jari 703             output_writer.close();
2 26 Feb 07 jari 704         }catch(Exception e){
2 26 Feb 07 jari 705             System.out.println(e.getMessage());
2 26 Feb 07 jari 706         }
2 26 Feb 07 jari 707     }
2 26 Feb 07 jari 708     
2 26 Feb 07 jari 709     
2 26 Feb 07 jari 710     /**
2 26 Feb 07 jari 711      * (jcb)Modified Hit collection methods to accept a vector argument of annotation keys
2 26 Feb 07 jari 712      * Modification of code to enable integration into MeV Ease module.
2 26 Feb 07 jari 713      */
2 26 Feb 07 jari 714     
2 26 Feb 07 jari 715     /** 
2 26 Feb 07 jari 716      * Get the number of the genes in the population for each category 
2 26 Feb 07 jari 717      * that exists in the annotation files.
2 26 Feb 07 jari 718      * @param list population list.
2 26 Feb 07 jari 719      */
2 26 Feb 07 jari 720     public void GetPopulationHitsByCategory(Vector list) {
2 26 Feb 07 jari 721         BufferedReader in = null;
2 26 Feb 07 jari 722         String category="", hits="", key="", locus_id;
2 26 Feb 07 jari 723         Hashtable locus_ids = new Hashtable();
2 26 Feb 07 jari 724         Hashtable hash_table = new Hashtable();
2 26 Feb 07 jari 725         Hashtable count_ids = new Hashtable();
2 26 Feb 07 jari 726         this.pop_totals = clear(this.pop_totals);
2 26 Feb 07 jari 727         
2 26 Feb 07 jari 728         try{
2 26 Feb 07 jari 729             int size = list.size();
2 26 Feb 07 jari 730             for(int i = 0; i < size; i++)   //used locus_ids hashtable to store all elements in population list
2 26 Feb 07 jari 731                 locus_ids.put((String)list.elementAt(i), "");
2 26 Feb 07 jari 732             
2 26 Feb 07 jari 733             for(Enumeration enum1 = locus_ids.keys(); enum1.hasMoreElements();){  //iterate through population list
2 26 Feb 07 jari 734                 locus_id = (String)(enum1.nextElement());
2 26 Feb 07 jari 735                 
2 26 Feb 07 jari 736                 for(Enumeration enum = categories.keys(); enum.hasMoreElements();){  //iterate through categories
2 26 Feb 07 jari 737                     key = (String)(enum.nextElement());
2 26 Feb 07 jari 738                     hash_table = (Hashtable) (categories.get(key));
2 26 Feb 07 jari 739                     
2 26 Feb 07 jari 740                     if(hash_table.containsKey(locus_id)){
2 26 Feb 07 jari 741                         if(!categories_population.containsKey(key)){
2 26 Feb 07 jari 742                             categories_population.put(key, "1");
2 26 Feb 07 jari 743                         }else{
2 26 Feb 07 jari 744                             hits = String.valueOf(Integer.parseInt((String) categories_population.get(key)) + 1);
2 26 Feb 07 jari 745                             categories_population.put(key, hits);
2 26 Feb 07 jari 746                         }
2 26 Feb 07 jari 747                         count_ids.put(locus_id, "");
2 26 Feb 07 jari 748                         
2 26 Feb 07 jari 749                         //accumulate the pop total hits
2 26 Feb 07 jari 750                         key = key.substring(0, key.indexOf("\t")).trim();
2 26 Feb 07 jari 751                         ((Hashtable)pop_totals.get(key)).put(locus_id,"");
2 26 Feb 07 jari 752                     }
2 26 Feb 07 jari 753                 }
2 26 Feb 07 jari 754             }
2 26 Feb 07 jari 755             population_total = count_ids.size();
2 26 Feb 07 jari 756         }catch(Exception e){
2 26 Feb 07 jari 757             System.out.println(e.getMessage());
2 26 Feb 07 jari 758         }
2 26 Feb 07 jari 759 //        Iterator it = (categories_population.keySet()).iterator();
2 26 Feb 07 jari 760 //        while (it.hasNext())
2 26 Feb 07 jari 761 //          System.out.print(it.next()+ "  ");
2 26 Feb 07 jari 762         
2 26 Feb 07 jari 763     }
2 26 Feb 07 jari 764     
2 26 Feb 07 jari 765     /**
2 26 Feb 07 jari 766      * Keep the key set but reset all value to blank
2 26 Feb 07 jari 767      * @param hash
2 26 Feb 07 jari 768      * @return hash 
2 26 Feb 07 jari 769      */
2 26 Feb 07 jari 770     public Hashtable clear(Hashtable hash) {
2 26 Feb 07 jari 771       Iterator it = (hash.keySet()).iterator();
2 26 Feb 07 jari 772       while(it.hasNext()) {
2 26 Feb 07 jari 773         hash.put(it.next(), new Hashtable());
2 26 Feb 07 jari 774       }
2 26 Feb 07 jari 775       return hash;
2 26 Feb 07 jari 776     }
2 26 Feb 07 jari 777     /** 
2 26 Feb 07 jari 778      * Get the number of the genes in the sample for each category 
2 26 Feb 07 jari 779      * that exists in the annotation files.
2 26 Feb 07 jari 780      * sample list
2 26 Feb 07 jari 781      * @param list List indices.
2 26 Feb 07 jari 782      */
2 26 Feb 07 jari 783     public void GetListHitsByCategory(Vector list) {
2 26 Feb 07 jari 784       initiation();
2 26 Feb 07 jari 785     this.categories_list = new Hashtable();
2 26 Feb 07 jari 786         BufferedReader in = null;
2 26 Feb 07 jari 787         String category="", hits="", key="", locus_id;
2 26 Feb 07 jari 788         Hashtable locus_ids = new Hashtable();
2 26 Feb 07 jari 789         Hashtable hash_table = new Hashtable();
2 26 Feb 07 jari 790         Hashtable count_ids = new Hashtable();
2 26 Feb 07 jari 791         this.sample_totals = clear(this.sample_totals);
2 26 Feb 07 jari 792         
2 26 Feb 07 jari 793         int c = 0;
2 26 Feb 07 jari 794         try{
2 26 Feb 07 jari 795             int size = list.size();
2 26 Feb 07 jari 796             for(int i = 0; i < size; i++)   //use a hashtable to store all elements in the sample list
2 26 Feb 07 jari 797                 locus_ids.put((String)list.elementAt(i), "");
2 26 Feb 07 jari 798             
2 26 Feb 07 jari 799             for(Enumeration enum1 = locus_ids.keys(); enum1.hasMoreElements();){   //iterate through samples ids.
2 26 Feb 07 jari 800                 locus_id = (String)(enum1.nextElement());    //locus_id is hashtable
2 26 Feb 07 jari 801                 
2 26 Feb 07 jari 802                 for(Enumeration enum = this.categories.keys(); enum.hasMoreElements();){  //iterate through categories
2 26 Feb 07 jari 803                     key = (String)(enum.nextElement());                                   //hashtable 
2 26 Feb 07 jari 804                     hash_table = (Hashtable) (this.categories.get(key));
2 26 Feb 07 jari 805                     
2 26 Feb 07 jari 806                     if(hash_table.containsKey(locus_id)){                   //if sample id is present in the list of genes
2 26 Feb 07 jari 807                         if(!this.categories_list.containsKey(key)){
2 26 Feb 07 jari 808                             this.categories_list.put(key, "1");
2 26 Feb 07 jari 809                             //jcb
2 26 Feb 07 jari 810                             this.hitAccumulator.put(key, new Vector());
2 26 Feb 07 jari 811                             ((Vector)this.hitAccumulator.get(key)).add(locus_id);
2 26 Feb 07 jari 812                             //end mod
2 26 Feb 07 jari 813                         }else {
2 26 Feb 07 jari 814                             hits = String.valueOf(Integer.parseInt((String) (this.categories_list.get(key))) + 1);
2 26 Feb 07 jari 815                             this.categories_list.put(key, hits);
2 26 Feb 07 jari 816                             //jcb
2 26 Feb 07 jari 817                             ((Vector)this.hitAccumulator.get(key)).add(locus_id);
2 26 Feb 07 jari 818                             //end mod
2 26 Feb 07 jari 819                         }
2 26 Feb 07 jari 820                         count_ids.put(locus_id, "");
2 26 Feb 07 jari 821                         
2 26 Feb 07 jari 822                         //accumulate the sample total hits
2 26 Feb 07 jari 823                         key = key.substring(0, key.indexOf("\t")).trim();
2 26 Feb 07 jari 824                         ((Hashtable)this.sample_totals.get(key)).put(locus_id,"");
2 26 Feb 07 jari 825                     }
2 26 Feb 07 jari 826                 }
2 26 Feb 07 jari 827             }
2 26 Feb 07 jari 828             this.list_total = count_ids.size();    
2 26 Feb 07 jari 829             //System.out.println("list_total = " + list_total);
2 26 Feb 07 jari 830         }catch(Exception e){
2 26 Feb 07 jari 831             System.out.println(e.getMessage());
2 26 Feb 07 jari 832         }
2 26 Feb 07 jari 833 //        System.out.println("categories_list is empty" + categories_list.isEmpty());
2 26 Feb 07 jari 834 //      Iterator it = (categories_list.keySet()).iterator();
2 26 Feb 07 jari 835 //      while (it.hasNext())
2 26 Feb 07 jari 836 //        System.out.println(it.next()+ "  ");
2 26 Feb 07 jari 837     }
2 26 Feb 07 jari 838     
2 26 Feb 07 jari 839     String [][] getResults(){
2 26 Feb 07 jari 840         return this.records;
2 26 Feb 07 jari 841     }
2 26 Feb 07 jari 842     
2 26 Feb 07 jari 843     String [][] getSurveyResults(){
2 26 Feb 07 jari 844         return this.records;
2 26 Feb 07 jari 845     }
2 26 Feb 07 jari 846     
2 26 Feb 07 jari 847     String [][] getListHitMatrix(){
2 26 Feb 07 jari 848         return this.listHitMatrix;
2 26 Feb 07 jari 849     }
2 26 Feb 07 jari 850     
2 26 Feb 07 jari 851     String [] getCategoryNames(){
2 26 Feb 07 jari 852         return this.categoryNames;
2 26 Feb 07 jari 853     }
2 26 Feb 07 jari 854     
2 26 Feb 07 jari 855     /** Resets the list total and list hit accumulators.
2 26 Feb 07 jari 856      */    
2 26 Feb 07 jari 857     public void resetForNewList(){
2 26 Feb 07 jari 858         this.categories_list = new Hashtable();
2 26 Feb 07 jari 859         this.sample_totals = new Hashtable();
2 26 Feb 07 jari 860         String file_name, term;
2 26 Feb 07 jari 861    
2 26 Feb 07 jari 862         for(Enumeration e = annotation_file_names.elements(); e.hasMoreElements();){
2 26 Feb 07 jari 863             file_name = e.nextElement().toString();
2 26 Feb 07 jari 864             term = file_name.substring(file_name.lastIndexOf(sep)+1, file_name.lastIndexOf("."));
2 26 Feb 07 jari 865             
2 26 Feb 07 jari 866             //store terms in total hits accumulators
2 26 Feb 07 jari 867             this.sample_totals.put(term, new Hashtable());
2 26 Feb 07 jari 868         }
2 26 Feb 07 jari 869     }
2 26 Feb 07 jari 870     
2 26 Feb 07 jari 871     
2 26 Feb 07 jari 872     /**
2 26 Feb 07 jari 873      * Modified Hit collection methods to accept a vector argument of annotation keys
2 26 Feb 07 jari 874      * Modification of code to enable integration into TIGR MeV EASE module.
2 26 Feb 07 jari 875      */
2 26 Feb 07 jari 876     
2 26 Feb 07 jari 877     /** 
2 26 Feb 07 jari 878      * Get the number of the genes in the population for each category that exists in the annotation files.
2 26 Feb 07 jari 879      * for slide annotation survey
2 26 Feb 07 jari 880      * @param list Input population list.
2 26 Feb 07 jari 881      */
2 26 Feb 07 jari 882     public void GetPopulationHitsByCategoryForSurvey(Vector list) {
2 26 Feb 07 jari 883       initiation();
2 26 Feb 07 jari 884       this.categories_population = new Hashtable();
2 26 Feb 07 jari 885         String category="";
2 26 Feb 07 jari 886         String hits="";
2 26 Feb 07 jari 887         String key="";
2 26 Feb 07 jari 888         String locus_id;       
2 26 Feb 07 jari 889         Hashtable locus_ids = new Hashtable();  //hashtable uses locus IDs in the list as key, "" as values
2 26 Feb 07 jari 890         Hashtable hash_table = new Hashtable();  
2 26 Feb 07 jari 891         Hashtable count_ids = new Hashtable();  //no duplicate locus id in the key set
2 26 Feb 07 jari 892         //jcb
2 26 Feb 07 jari 893         String impliedCategory="";
2 26 Feb 07 jari 894         Vector impliedVector;
2 26 Feb 07 jari 895         this.pop_totals = clear(this.pop_totals);
2 26 Feb 07 jari 896         
2 26 Feb 07 jari 897         
2 26 Feb 07 jari 898         int c = 0;
2 26 Feb 07 jari 899         
2 26 Feb 07 jari 900         try{
2 26 Feb 07 jari 901 //          //************************************************************************
2 26 Feb 07 jari 902 //            Hashtable hash = (Hashtable)categories.get("biochemical function  RNA polymerase subunit");
2 26 Feb 07 jari 903 //            Iterator it = hash.keySet().iterator();
2 26 Feb 07 jari 904 //            while (it.hasNext()){
2 26 Feb 07 jari 905 //              Object thing = it.next();
2 26 Feb 07 jari 906 //              System.out.println(thing);
2 26 Feb 07 jari 907 //            }
2 26 Feb 07 jari 908
2 26 Feb 07 jari 909           int size = list.size();
2 26 Feb 07 jari 910             for(int i = 0; i < size; i++)
2 26 Feb 07 jari 911                 locus_ids.put((String)list.elementAt(i), "");
2 26 Feb 07 jari 912             
2 26 Feb 07 jari 913             for(Enumeration enum1 = locus_ids.keys(); enum1.hasMoreElements();){
2 26 Feb 07 jari 914                 locus_id = (String)(enum1.nextElement());
2 26 Feb 07 jari 915
2 26 Feb 07 jari 916                 for(Enumeration enum = categories.keys(); enum.hasMoreElements();){
2 26 Feb 07 jari 917                     key = (String)(enum.nextElement());
2 26 Feb 07 jari 918                     hash_table = (Hashtable) (categories.get(key));
2 26 Feb 07 jari 919
2 26 Feb 07 jari 920                     //if locus ID in the list can be found in the categories list
2 26 Feb 07 jari 921                     if(hash_table.containsKey(locus_id)){
2 26 Feb 07 jari 922                         if(!categories_population.containsKey(key)){
2 26 Feb 07 jari 923                             categories_population.put(key, "1");   //if first locus id in this category
2 26 Feb 07 jari 924                             //System.out.println("TEASEBench 1042, hitAccumulator add");
2 26 Feb 07 jari 925                             hitAccumulator.put(key, new Vector());
2 26 Feb 07 jari 926                             ((Vector)hitAccumulator.get(key)).add(locus_id); //put in a list of locus id
2 26 Feb 07 jari 927                         }else{
2 26 Feb 07 jari 928                             hits = String.valueOf(Integer.parseInt((String)categories_population.get(key)) + 1);
2 26 Feb 07 jari 929                             categories_population.put(key, hits);
2 26 Feb 07 jari 930 //                            System.out.println("hitAccumulator is null? "+(hitAccumulator == null));
2 26 Feb 07 jari 931 //                            System.out.println("key is null? "+(key == null));
2 26 Feb 07 jari 932 //                            System.out.println("locus_id is null? "+(locus_id == null));
2 26 Feb 07 jari 933 //                            System.out.println("list is null? "+(hitAccumulator.get(key) == null));
2 26 Feb 07 jari 934                             ((Vector)hitAccumulator.get(key)).add(locus_id);
2 26 Feb 07 jari 935                         }
2 26 Feb 07 jari 936                         count_ids.put(locus_id, "");
2 26 Feb 07 jari 937                         //accumulate the pop total hits
2 26 Feb 07 jari 938                         key = key.substring(0, key.indexOf("\t")).trim();
2 26 Feb 07 jari 939                         ((Hashtable)pop_totals.get(key)).put(locus_id,"");
2 26 Feb 07 jari 940                     }
2 26 Feb 07 jari 941                 }
2 26 Feb 07 jari 942             }
2 26 Feb 07 jari 943 //            System.out.println("cout_ids is empty? "  +count_ids.isEmpty());
2 26 Feb 07 jari 944 //            Set keys = count_ids.keySet();
2 26 Feb 07 jari 945 //            Iterator it = keys.iterator();
2 26 Feb 07 jari 946 //            while (it.hasNext()) {
2 26 Feb 07 jari 947 //              Object o = it.next();
2 26 Feb 07 jari 948 //              System.out.println("key: "+o + "  value: "+ count_ids.get(o));
2 26 Feb 07 jari 949 //            }
2 26 Feb 07 jari 950             population_total = count_ids.size(); 
2 26 Feb 07 jari 951             //System.out.println("population total: " + population_total);  //*******************************
2 26 Feb 07 jari 952             
2 26 Feb 07 jari 953         }catch(Exception e){
2 26 Feb 07 jari 954             e.printStackTrace();
2 26 Feb 07 jari 955         }
2 26 Feb 07 jari 956     }
2 26 Feb 07 jari 957     
2 26 Feb 07 jari 958     
2 26 Feb 07 jari 959     /** 
2 26 Feb 07 jari 960      * Creates the tab-delimited results containing information on system, category, 
2 26 Feb 07 jari 961      * population hits, population total, sample hits and sample totoal, and the 
2 26 Feb 07 jari 962      * one-tailed hypergeometric probability score of over-representation.
2 26 Feb 07 jari 963      * for slide annotation survey
2 26 Feb 07 jari 964      */
2 26 Feb 07 jari 965     public void ConstructSurveyResults() {
2 26 Feb 07 jari 966         
2 26 Feb 07 jari 967         String key="", list_hit="", population_hit="", temp="";        
2 26 Feb 07 jari 968         
2 26 Feb 07 jari 969         records = new String[categories_population.size()][4];   //records is String[][]
2 26 Feb 07 jari 970         orders = new String[categories_population.size()];       //orders is String[]
2 26 Feb 07 jari 971         
2 26 Feb 07 jari 972         int c=0;
2 26 Feb 07 jari 973      //   System.out.println("TEASEBench 1101  "+categories_population.size());
2 26 Feb 07 jari 974         for(Enumeration e = categories_population.keys(); e.hasMoreElements();){
2 26 Feb 07 jari 975             key = (String)(e.nextElement());
2 26 Feb 07 jari 976             list_hit="";
2 26 Feb 07 jari 977             population_hit="";
2 26 Feb 07 jari 978             
2 26 Feb 07 jari 979             temp = key.substring(0, key.indexOf('\t')).trim();
2 26 Feb 07 jari 980             records[c][0] = temp.substring(temp.lastIndexOf(sep)+1).trim();
2 26 Feb 07 jari 981             records[c][1] = key.substring(key.indexOf('\t')).trim();
2 26 Feb 07 jari 982             
2 26 Feb 07 jari 983             list_hit = (String)(categories_population.get(key));
2 26 Feb 07 jari 984             
2 26 Feb 07 jari 985             records[c][2] = list_hit;
2 26 Feb 07 jari 986             
2 26 Feb 07 jari 987             population_total = ((Hashtable)(pop_totals.get(temp))).size();
2 26 Feb 07 jari 988             
2 26 Feb 07 jari 989             records[c][3] = String.valueOf(population_total);
2 26 Feb 07 jari 990             //System.out.println(records[c][3]);
2 26 Feb 07 jari 991             c++;
2 26 Feb 07 jari 992         }
2 26 Feb 07 jari 993         
2 26 Feb 07 jari 994         //jcb
2 26 Feb 07 jari 995         //accumulates hits for the result matrix.
2 26 Feb 07 jari 996         categoryNames = new String[hitAccumulator.size()];   //keys  -> category names
2 26 Feb 07 jari 997         listHitMatrix = new String[categoryNames.length][];  //values  -> list of hits
2 26 Feb 07 jari 998         
2 26 Feb 07 jari 999         int cnt = 0;
2 26 Feb 07 jari 1000         Vector locusVector;
2 26 Feb 07 jari 1001         
2 26 Feb 07 jari 1002         for(Enumeration e = hitAccumulator.keys(); e.hasMoreElements();){
2 26 Feb 07 jari 1003             String cat = (String)(e.nextElement());
2 26 Feb 07 jari 1004             categoryNames[cnt] = cat;   //categoryNames[0] = name of a category
2 26 Feb 07 jari 1005             locusVector = ((Vector)hitAccumulator.get(cat));
2 26 Feb 07 jari 1006             listHitMatrix[cnt] = new String[locusVector.size()];  //listHitMatrix[0][] = list of hits of
2 26 Feb 07 jari 1007             for(int i = 0; i < listHitMatrix[cnt].length; i++){   //corresponding category
2 26 Feb 07 jari 1008                 listHitMatrix[cnt][i] = ((String)locusVector.elementAt(i));
2 26 Feb 07 jari 1009             }
2 26 Feb 07 jari 1010             cnt++;
2 26 Feb 07 jari 1011         }
2 26 Feb 07 jari 1012         
2 26 Feb 07 jari 1013 //        for (int i = 0; i < categoryNames.length; i++) {   //*****************************************
2 26 Feb 07 jari 1014 //          System.out.print(categoryNames[i]+" ");
2 26 Feb 07 jari 1015 //          for (int j = 0; j < listHitMatrix[i].length; j++)
2 26 Feb 07 jari 1016 //            System.out.print(listHitMatrix[i][j]+" ");
2 26 Feb 07 jari 1017 //          System.out.println();
2 26 Feb 07 jari 1018 //        }
2 26 Feb 07 jari 1019     }
2 26 Feb 07 jari 1020 }