plugins/base1/se.lu.onk/trunk/BaseFile_deprecated/src/basefile/BASEFileReader.java

Code
Comments
Other
Rev Date Author Line
6 10 Oct 05 enell 1 /* (non-javadoc)
6 10 Oct 05 enell 2  * BASEFileReader.java is part of BASEFile.
6 10 Oct 05 enell 3  *
6 10 Oct 05 enell 4  * BASEFile is free software; you can redistribute it and/or modify
6 10 Oct 05 enell 5  * it under the terms of the GNU General Public License as published by
6 10 Oct 05 enell 6  * the Free Software Foundation; either version 2 of the License, or
6 10 Oct 05 enell 7  * (at your option) any later version.
6 10 Oct 05 enell 8  * 
6 10 Oct 05 enell 9  * BASEFile is distributed in the hope that it will be useful,
6 10 Oct 05 enell 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
6 10 Oct 05 enell 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6 10 Oct 05 enell 12  * GNU General Public License for more details.
6 10 Oct 05 enell 13  * 
6 10 Oct 05 enell 14  * You should have received a copy of the GNU General Public License
6 10 Oct 05 enell 15  * along with BASEFile; if not, write to the Free Software
6 10 Oct 05 enell 16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
6 10 Oct 05 enell 17  * 
6 10 Oct 05 enell 18  * johan.enell@onk.lu.se
6 10 Oct 05 enell 19  * Johan Enell, Dept Oncology, Lund University, S-221 85 Lund, Sweden
6 10 Oct 05 enell 20  */
6 10 Oct 05 enell 21 package basefile;
6 10 Oct 05 enell 22
15 14 Oct 05 enell 23 import java.io.File;
15 14 Oct 05 enell 24 import java.io.FileNotFoundException;
15 14 Oct 05 enell 25 import java.io.FileReader;
6 10 Oct 05 enell 26 import java.io.IOException;
6 10 Oct 05 enell 27 import java.io.LineNumberReader;
6 10 Oct 05 enell 28 import java.util.HashMap;
6 10 Oct 05 enell 29 import java.util.Vector;
6 10 Oct 05 enell 30
6 10 Oct 05 enell 31 /**
6 10 Oct 05 enell 32  * Read sections from a file created from the database BASE. This class is
6 10 Oct 05 enell 33  * designed to read from System.in and will read section wise.
6 10 Oct 05 enell 34  * 
6 10 Oct 05 enell 35  * @author Johan Enell
6 10 Oct 05 enell 36  */
6 10 Oct 05 enell 37 public class BASEFileReader
6 10 Oct 05 enell 38 {
6 10 Oct 05 enell 39
6 10 Oct 05 enell 40   /**
6 10 Oct 05 enell 41    * The active baseFile.BASEFileSection that the reader is reading from.
6 10 Oct 05 enell 42    */
6 10 Oct 05 enell 43   private BASEFileSection section = null;
6 10 Oct 05 enell 44
6 10 Oct 05 enell 45   private LineNumberReader str;
6 10 Oct 05 enell 46
6 10 Oct 05 enell 47   private boolean dataEnd;
6 10 Oct 05 enell 48
6 10 Oct 05 enell 49   private String buf;
6 10 Oct 05 enell 50
15 14 Oct 05 enell 51   private File inFile;
15 14 Oct 05 enell 52
6 10 Oct 05 enell 53   /**
6 10 Oct 05 enell 54    * Create a new baseFile reader.
6 10 Oct 05 enell 55    * 
6 10 Oct 05 enell 56    * @param in
6 10 Oct 05 enell 57    *            the stream from were the reader gets the data.
15 14 Oct 05 enell 58    * @throws FileNotFoundException If the infile cant be found
6 10 Oct 05 enell 59    */
248 28 Mar 07 enell 60   @Deprecated
15 14 Oct 05 enell 61   public BASEFileReader(File in) throws FileNotFoundException
6 10 Oct 05 enell 62   {
15 14 Oct 05 enell 63     inFile = in;
15 14 Oct 05 enell 64     str = new LineNumberReader(new FileReader(in));
6 10 Oct 05 enell 65     dataEnd = true;
6 10 Oct 05 enell 66   }
6 10 Oct 05 enell 67
6 10 Oct 05 enell 68   /**
6 10 Oct 05 enell 69    * Read the next baseFile section in the baseFile.
6 10 Oct 05 enell 70    * 
6 10 Oct 05 enell 71    * @return the section read.
6 10 Oct 05 enell 72    * @throws BadSectionException
6 10 Oct 05 enell 73    *             If the baseFile section is badly written
6 10 Oct 05 enell 74    * @throws BadFormatException
6 10 Oct 05 enell 75    *             If the baseFile is badly formatted (ie. first line is not
6 10 Oct 05 enell 76    *             BASEFil)
6 10 Oct 05 enell 77    * @throws IOException
6 10 Oct 05 enell 78    *             If an I/O error occurs.
6 10 Oct 05 enell 79    */
6 10 Oct 05 enell 80   public BASEFileSection readSection()
18 18 Oct 05 enell 81     throws BadSectionException, BadFormatException, IOException
6 10 Oct 05 enell 82   {
6 10 Oct 05 enell 83     return readSection(false);
6 10 Oct 05 enell 84   }
6 10 Oct 05 enell 85
6 10 Oct 05 enell 86   /**
6 10 Oct 05 enell 87    * Read the next baseFile section in the baseFile.
6 10 Oct 05 enell 88    * 
6 10 Oct 05 enell 89    * @param beginFile
6 10 Oct 05 enell 90    *            Is set true if it is the begining of the baseFile.
6 10 Oct 05 enell 91    * @return the section read or null if there is no more sections
6 10 Oct 05 enell 92    * @throws BadSectionException
6 10 Oct 05 enell 93    *             If the baseFile section is badly written
6 10 Oct 05 enell 94    * @throws BadFormatException
6 10 Oct 05 enell 95    *             If the baseFile is badly formatted (ie. first line is not
6 10 Oct 05 enell 96    *             BASEFil)
6 10 Oct 05 enell 97    * @throws IOException
6 10 Oct 05 enell 98    *             If an I/O error occurs.
6 10 Oct 05 enell 99    */
249 28 Mar 07 enell 100   @Deprecated
6 10 Oct 05 enell 101   public BASEFileSection readSection(boolean beginFile)
185 12 Oct 06 enell 102     throws BadFormatException, IOException
6 10 Oct 05 enell 103   {
103 01 Jun 06 enell 104     if (section == null)
103 01 Jun 06 enell 105     {
103 01 Jun 06 enell 106       section = new BASEFileSection();
103 01 Jun 06 enell 107     }
103 01 Jun 06 enell 108     else
103 01 Jun 06 enell 109     {
103 01 Jun 06 enell 110       section.clear();
103 01 Jun 06 enell 111     }
103 01 Jun 06 enell 112     
97 20 Apr 06 enell 113     section.setStartLine(str.getLineNumber() + 1);
6 10 Oct 05 enell 114     skipData();
97 20 Apr 06 enell 115     section.setStartLine(str.getLineNumber() + 1);
6 10 Oct 05 enell 116
18 18 Oct 05 enell 117     if (!section.setHeaders(readHeaders(beginFile)))
18 18 Oct 05 enell 118     {
18 18 Oct 05 enell 119       return null;
18 18 Oct 05 enell 120     }
6 10 Oct 05 enell 121
97 20 Apr 06 enell 122     section.setDataStartLine(str.getLineNumber() + 1);
6 10 Oct 05 enell 123
664 16 Apr 08 jari 124     if (section.getType()==null || section.getType().length()==0) return null;
6 10 Oct 05 enell 125     return section;
6 10 Oct 05 enell 126   }
103 01 Jun 06 enell 127   
103 01 Jun 06 enell 128   /**
103 01 Jun 06 enell 129    * TODO: Write javadoc for readSpotSection()
103 01 Jun 06 enell 130    * @param <R>
103 01 Jun 06 enell 131    * @param <S>
103 01 Jun 06 enell 132    * @return
103 01 Jun 06 enell 133    * @throws BadFormatException 
103 01 Jun 06 enell 134    * @throws IOException 
103 01 Jun 06 enell 135    */
247 27 Mar 07 enell 136   public <R, S> BASEFileSpotSection<R, S> readSpotSection()
248 28 Mar 07 enell 137     throws BASEFileException, IOException
103 01 Jun 06 enell 138   {
711 29 May 08 jari 139     section = readSection(false);
104 01 Jun 06 enell 140     while (section != null && !section.isType("spots"))
103 01 Jun 06 enell 141     {
103 01 Jun 06 enell 142       section = readSection(false);
103 01 Jun 06 enell 143     }
711 29 May 08 jari 144     return (section == null) ? null : new BASEFileSpotSection<R, S>(section);
103 01 Jun 06 enell 145   }
247 27 Mar 07 enell 146   
247 27 Mar 07 enell 147   /**
711 29 May 08 jari 148    * TODO: Write javadoc for readAssaySection()
247 27 Mar 07 enell 149    * @param <R>
247 27 Mar 07 enell 150    * @param <S>
247 27 Mar 07 enell 151    * @return
247 27 Mar 07 enell 152    * @throws IOException 
247 27 Mar 07 enell 153    * @throws BadSectionException 
247 27 Mar 07 enell 154    */
247 27 Mar 07 enell 155   public BASEFileAssaySection readAssaySection()
247 27 Mar 07 enell 156     throws BASEFileException, IOException
247 27 Mar 07 enell 157   {
248 28 Mar 07 enell 158     section = readSection(false); 
247 27 Mar 07 enell 159     while (section != null && !section.isType("assays"))
247 27 Mar 07 enell 160     {
247 27 Mar 07 enell 161       section = readSection(false);
247 27 Mar 07 enell 162     }
248 28 Mar 07 enell 163     if (section == null)
248 28 Mar 07 enell 164     {
248 28 Mar 07 enell 165       throw new BASEFileException("Couldn't find assays section in file "+this.inFile.getAbsolutePath());
248 28 Mar 07 enell 166     }
247 27 Mar 07 enell 167     return new BASEFileAssaySection(section);
247 27 Mar 07 enell 168   }
18 18 Oct 05 enell 169
15 14 Oct 05 enell 170   public void resetDataPosition(BASEFileSection section)
15 14 Oct 05 enell 171   {
15 14 Oct 05 enell 172     try
15 14 Oct 05 enell 173     {
15 14 Oct 05 enell 174       str.close();
15 14 Oct 05 enell 175       str = new LineNumberReader(new FileReader(inFile));
97 20 Apr 06 enell 176       while (str.getLineNumber() < section.getDataStartLine())
15 14 Oct 05 enell 177       {
15 14 Oct 05 enell 178         str.readLine();
15 14 Oct 05 enell 179       }
18 18 Oct 05 enell 180       dataEnd = false;
15 14 Oct 05 enell 181     }
15 14 Oct 05 enell 182     catch (IOException e)
15 14 Oct 05 enell 183     {
15 14 Oct 05 enell 184       //Should not happend
15 14 Oct 05 enell 185       e.printStackTrace();
15 14 Oct 05 enell 186     }
15 14 Oct 05 enell 187   }
6 10 Oct 05 enell 188
6 10 Oct 05 enell 189   /**
6 10 Oct 05 enell 190    * This will read a singel line from the active section.
6 10 Oct 05 enell 191    * 
6 10 Oct 05 enell 192    * @return The data row as an String[]. Returns <code>null</code> if the
6 10 Oct 05 enell 193    *         section end or the file end is reached.
6 10 Oct 05 enell 194    * @throws IOException
6 10 Oct 05 enell 195    *             If an I/O error occurs.
6 10 Oct 05 enell 196    */
6 10 Oct 05 enell 197   public String[] readDataRow() throws IOException
6 10 Oct 05 enell 198   {
6 10 Oct 05 enell 199     Vector<String> vec = new Vector<String>();
6 10 Oct 05 enell 200
6 10 Oct 05 enell 201     if (!readLine() || dataEnd) return null;
6 10 Oct 05 enell 202
6 10 Oct 05 enell 203     int tok = buf.indexOf("\t"), otok = 0;
6 10 Oct 05 enell 204
6 10 Oct 05 enell 205     while (tok < buf.length())
6 10 Oct 05 enell 206     {
6 10 Oct 05 enell 207       tok = buf.indexOf("\t", otok);
6 10 Oct 05 enell 208       if (tok == -1)
6 10 Oct 05 enell 209       {
6 10 Oct 05 enell 210         tok = buf.length();
6 10 Oct 05 enell 211       }
6 10 Oct 05 enell 212       vec.add(buf.substring(otok, tok));
6 10 Oct 05 enell 213       otok = tok + 1;
6 10 Oct 05 enell 214     }
6 10 Oct 05 enell 215
31 08 Nov 05 enell 216     return vec.toArray(new String[vec.size()]);
6 10 Oct 05 enell 217   }
6 10 Oct 05 enell 218
6 10 Oct 05 enell 219   /**
6 10 Oct 05 enell 220    * This will read a singel line from the active section.
6 10 Oct 05 enell 221    * 
6 10 Oct 05 enell 222    * @param nbr
6 10 Oct 05 enell 223    *            The number of fields that the row consists of
6 10 Oct 05 enell 224    * @return The data row as an String[]. Returns <code>null</code> if the
6 10 Oct 05 enell 225    *         section end or the file end is reached.
6 10 Oct 05 enell 226    * @throws IOException
6 10 Oct 05 enell 227    *             If an I/O error occurs.
6 10 Oct 05 enell 228    * @throws TooFewFieldsException
6 10 Oct 05 enell 229    *             If <code>nbr</code> is less than the number of fields in
6 10 Oct 05 enell 230    *             the row.
6 10 Oct 05 enell 231    * @throws TooManyFieldsException
6 10 Oct 05 enell 232    *             If <code>nbr</code> is more than the number of fields in
6 10 Oct 05 enell 233    *             the row.
6 10 Oct 05 enell 234    */
6 10 Oct 05 enell 235   public String[] readDataRow(int nbr)
245 13 Mar 07 enell 236     throws BASEFileException, TooFewFieldsException, TooManyFieldsException
6 10 Oct 05 enell 237   {
6 10 Oct 05 enell 238     String[] vec = new String[nbr];
6 10 Oct 05 enell 239     dataEnd = false;
6 10 Oct 05 enell 240
18 18 Oct 05 enell 241     if (nbr < 0)
18 18 Oct 05 enell 242     {
18 18 Oct 05 enell 243       throw new TooFewFieldsException("Too few fields in " + section.getType() + ", line " + str.getLineNumber());
18 18 Oct 05 enell 244     }
245 13 Mar 07 enell 245     try
245 13 Mar 07 enell 246     {
245 13 Mar 07 enell 247       if (!readLine() || dataEnd) return null;
6 10 Oct 05 enell 248
245 13 Mar 07 enell 249       int tok = buf.indexOf("\t"), otok = 0;
6 10 Oct 05 enell 250
245 13 Mar 07 enell 251       while (tok < buf.length())
6 10 Oct 05 enell 252       {
245 13 Mar 07 enell 253         tok = buf.indexOf("\t", otok);
245 13 Mar 07 enell 254         if (tok == -1)
245 13 Mar 07 enell 255         {
245 13 Mar 07 enell 256           tok = buf.length();
245 13 Mar 07 enell 257         }
245 13 Mar 07 enell 258         vec[vec.length - nbr] = buf.substring(otok, tok);
245 13 Mar 07 enell 259         otok = tok + 1;
245 13 Mar 07 enell 260         nbr--;
6 10 Oct 05 enell 261       }
6 10 Oct 05 enell 262     }
245 13 Mar 07 enell 263     catch (Exception e)
245 13 Mar 07 enell 264     {
245 13 Mar 07 enell 265       throw new BASEFileException("At line " + str.getLineNumber(), e);
245 13 Mar 07 enell 266     }
6 10 Oct 05 enell 267
18 18 Oct 05 enell 268     if (nbr < 0)
18 18 Oct 05 enell 269     {
18 18 Oct 05 enell 270       throw new TooFewFieldsException("Too few fields in " + section.getType() + ", line " + str.getLineNumber());
18 18 Oct 05 enell 271     }
18 18 Oct 05 enell 272     if (nbr > 0)
18 18 Oct 05 enell 273     {
18 18 Oct 05 enell 274       throw new TooManyFieldsException("Too many fields in " + section.getType() + ", line " + str.getLineNumber());
18 18 Oct 05 enell 275     }
6 10 Oct 05 enell 276     return vec;
6 10 Oct 05 enell 277   }
6 10 Oct 05 enell 278
6 10 Oct 05 enell 279   /**
6 10 Oct 05 enell 280    * Read a line from the stream.
6 10 Oct 05 enell 281    * 
6 10 Oct 05 enell 282    * @return False if there is no more datarows in the section.
6 10 Oct 05 enell 283    * @throws IOException
6 10 Oct 05 enell 284    *             If an I/O error occurs.
6 10 Oct 05 enell 285    */
6 10 Oct 05 enell 286   private boolean readLine() throws IOException
6 10 Oct 05 enell 287   {
6 10 Oct 05 enell 288     buf = str.readLine();
6 10 Oct 05 enell 289     if (buf == null)
6 10 Oct 05 enell 290     {
6 10 Oct 05 enell 291       dataEnd = true;
185 12 Oct 06 enell 292       return false;
6 10 Oct 05 enell 293     }
6 10 Oct 05 enell 294     else if (buf.equals(""))
6 10 Oct 05 enell 295     {
6 10 Oct 05 enell 296       dataEnd = true;
185 12 Oct 06 enell 297       return false;
6 10 Oct 05 enell 298     }
6 10 Oct 05 enell 299     return true;
6 10 Oct 05 enell 300   }
6 10 Oct 05 enell 301
6 10 Oct 05 enell 302   /**
6 10 Oct 05 enell 303    * Skips all datarows so an new section can be read.
6 10 Oct 05 enell 304    * 
6 10 Oct 05 enell 305    * @return False if end of file is reached.
6 10 Oct 05 enell 306    * @throws IOException
6 10 Oct 05 enell 307    *             If an I/O error occurs.
6 10 Oct 05 enell 308    */
6 10 Oct 05 enell 309   private boolean skipData() throws IOException
6 10 Oct 05 enell 310   {
6 10 Oct 05 enell 311     if (!dataEnd)
6 10 Oct 05 enell 312     {
6 10 Oct 05 enell 313       if (!readLine()) return false;
6 10 Oct 05 enell 314       while (!dataEnd)
6 10 Oct 05 enell 315       {
6 10 Oct 05 enell 316         if (!readLine()) return false;
6 10 Oct 05 enell 317       }
6 10 Oct 05 enell 318     }
6 10 Oct 05 enell 319     return true;
6 10 Oct 05 enell 320   }
6 10 Oct 05 enell 321
6 10 Oct 05 enell 322   /**
6 10 Oct 05 enell 323    * Reads the headers in a section.
6 10 Oct 05 enell 324    * 
6 10 Oct 05 enell 325    * @param begin
6 10 Oct 05 enell 326    *            is true if it is the begining of the baseFile.
6 10 Oct 05 enell 327    * @return A HashMap with the fields as keys.
6 10 Oct 05 enell 328    * @throws IOException
6 10 Oct 05 enell 329    *             If an I/O error occurs.
6 10 Oct 05 enell 330    * @throws BadFormatException
6 10 Oct 05 enell 331    *             If the baseFile is badly formatted (ie. first line is not
6 10 Oct 05 enell 332    *             BASEFil)
6 10 Oct 05 enell 333    */
6 10 Oct 05 enell 334   private HashMap<String, String> readHeaders(boolean begin)
18 18 Oct 05 enell 335     throws IOException, BadFormatException
6 10 Oct 05 enell 336   {
6 10 Oct 05 enell 337     HashMap<String, String> nameValue = new HashMap<String, String>();
6 10 Oct 05 enell 338     if (begin)
6 10 Oct 05 enell 339     {
6 10 Oct 05 enell 340       if (!readLine()) return null;
18 18 Oct 05 enell 341       if (!buf.startsWith("BASEfile"))
18 18 Oct 05 enell 342       {
18 18 Oct 05 enell 343         throw new BadFormatException("The file must start with 'BASEfile'");
18 18 Oct 05 enell 344       }
6 10 Oct 05 enell 345     }
6 10 Oct 05 enell 346     if (!readLine()) return null;
6 10 Oct 05 enell 347     while (buf != null && buf.equals(""))
6 10 Oct 05 enell 348       if (!readLine()) return null;
6 10 Oct 05 enell 349     dataEnd = false;
6 10 Oct 05 enell 350     while (buf != null && !buf.equals("%"))
6 10 Oct 05 enell 351     {
6 10 Oct 05 enell 352       int tab = buf.indexOf("\t");
6 10 Oct 05 enell 353       if (tab != -1)
6 10 Oct 05 enell 354       {
6 10 Oct 05 enell 355         nameValue.put(buf.substring(0, tab), buf.substring(tab + 1));
6 10 Oct 05 enell 356       }
6 10 Oct 05 enell 357       else if (buf.length() > 0) nameValue.put(buf, "");
6 10 Oct 05 enell 358       if (!readLine()) return null;
6 10 Oct 05 enell 359     }
6 10 Oct 05 enell 360     return nameValue;
6 10 Oct 05 enell 361   }
6 10 Oct 05 enell 362
6 10 Oct 05 enell 363   /**
6 10 Oct 05 enell 364    * @return Returns the section.
6 10 Oct 05 enell 365    */
6 10 Oct 05 enell 366   public Object getSection()
6 10 Oct 05 enell 367   {
6 10 Oct 05 enell 368     return section;
6 10 Oct 05 enell 369   }
6 10 Oct 05 enell 370
6 10 Oct 05 enell 371 }