plugins/base2/net.sf.basedb.illumina/trunk/src/net/sf/basedb/illumina/filehandler/SnpCvsValidator.java

Code
Comments
Other
Rev Date Author Line
1385 02 Sep 11 martin 1 /**
1385 02 Sep 11 martin 2   $Id:
1385 02 Sep 11 martin 3
1385 02 Sep 11 martin 4   Copyright (C) 2011 Martin Svensson
1385 02 Sep 11 martin 5
1385 02 Sep 11 martin 6   This file is part of BASE - BioArray Software Environment.
1385 02 Sep 11 martin 7   Available at http://base.thep.lu.se/
1385 02 Sep 11 martin 8
1385 02 Sep 11 martin 9   BASE is free software; you can redistribute it and/or
1385 02 Sep 11 martin 10   modify it under the terms of the GNU General Public License
1385 02 Sep 11 martin 11   as published by the Free Software Foundation; either version 3
1385 02 Sep 11 martin 12   of the License, or (at your option) any later version.
1385 02 Sep 11 martin 13
1385 02 Sep 11 martin 14   BASE is distributed in the hope that it will be useful,
1385 02 Sep 11 martin 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
1385 02 Sep 11 martin 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1385 02 Sep 11 martin 17   GNU General Public License for more details.
1385 02 Sep 11 martin 18
1385 02 Sep 11 martin 19   You should have received a copy of the GNU General Public License
1385 02 Sep 11 martin 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
1385 02 Sep 11 martin 21 */
1385 02 Sep 11 martin 22 package net.sf.basedb.illumina.filehandler;
1385 02 Sep 11 martin 23
1385 02 Sep 11 martin 24 import java.io.BufferedReader;
1385 02 Sep 11 martin 25 import java.io.IOException;
1385 02 Sep 11 martin 26 import java.io.InputStream;
1385 02 Sep 11 martin 27 import java.io.InputStreamReader;
1385 02 Sep 11 martin 28 import java.util.ArrayList;
1385 02 Sep 11 martin 29 import java.util.List;
1385 02 Sep 11 martin 30
1385 02 Sep 11 martin 31 import net.sf.basedb.core.ArrayDesign;
1385 02 Sep 11 martin 32 import net.sf.basedb.core.File;
1385 02 Sep 11 martin 33 import net.sf.basedb.core.InvalidDataException;
1385 02 Sep 11 martin 34 import net.sf.basedb.core.InvalidUseOfNullException;
1385 02 Sep 11 martin 35
1385 02 Sep 11 martin 36 /**
1385 02 Sep 11 martin 37
1385 02 Sep 11 martin 38   @author Martin
1385 02 Sep 11 martin 39   @since 1.7
1385 02 Sep 11 martin 40   @base.modified $Date: 2011-05-06 14:09:13 +0200 (Fri, 06 May 2011) $ 
1385 02 Sep 11 martin 41 */
1385 02 Sep 11 martin 42 public class SnpCvsValidator
1385 02 Sep 11 martin 43 {
1385 02 Sep 11 martin 44   // Max number of lines to parse
1385 02 Sep 11 martin 45   private final int linesToParse = 100;
1385 02 Sep 11 martin 46
1385 02 Sep 11 martin 47   // The start of the heading that holds the count of SNP.
1385 02 Sep 11 martin 48   private String snpCountHeading = "SNP Count,";
1385 02 Sep 11 martin 49
1385 02 Sep 11 martin 50   // List to hold the parsed lines.
1385 02 Sep 11 martin 51   private List<String> parsedHeadings = null;
1385 02 Sep 11 martin 52
1385 02 Sep 11 martin 53   // Required column names
1385 02 Sep 11 martin 54   private String[] requiredColumns = { "IlmnID,", ",AddressA_ID," };
1385 02 Sep 11 martin 55   
1385 02 Sep 11 martin 56   public SnpCvsValidator()
1385 02 Sep 11 martin 57   {}
1385 02 Sep 11 martin 58   
1385 02 Sep 11 martin 59   public void resetMetadata(ArrayDesign design) 
1385 02 Sep 11 martin 60   {
1385 02 Sep 11 martin 61     design.setNumFileFeatures(0);
1385 02 Sep 11 martin 62     
1385 02 Sep 11 martin 63   }
1385 02 Sep 11 martin 64   
1385 02 Sep 11 martin 65   public void copyMetaData(File snpCvsFile, ArrayDesign design)
1385 02 Sep 11 martin 66     throws InvalidDataException
1385 02 Sep 11 martin 67   {
1385 02 Sep 11 martin 68     if (snpCvsFile != null)
1385 02 Sep 11 martin 69     {
1385 02 Sep 11 martin 70       parsedHeadings = parseHeadings(snpCvsFile, linesToParse);
1385 02 Sep 11 martin 71       if (parsedHeadings != null)
1385 02 Sep 11 martin 72       {      
1385 02 Sep 11 martin 73         int numFeatures = getNumFeatures(parsedHeadings);
1385 02 Sep 11 martin 74         design.setNumFileFeatures(numFeatures);
1385 02 Sep 11 martin 75       }
1385 02 Sep 11 martin 76     }
1385 02 Sep 11 martin 77   
1385 02 Sep 11 martin 78   }
1385 02 Sep 11 martin 79   
1385 02 Sep 11 martin 80   private List<String> parseHeadings(File manifestFile, int range)
1385 02 Sep 11 martin 81   {
1385 02 Sep 11 martin 82     if (manifestFile == null)
1385 02 Sep 11 martin 83     {
1385 02 Sep 11 martin 84       throw new InvalidUseOfNullException("manifestFile");
1385 02 Sep 11 martin 85     }
1385 02 Sep 11 martin 86     if (!manifestFile.getLocation().isDownloadable())
1385 02 Sep 11 martin 87     {
1385 02 Sep 11 martin 88       throw new InvalidDataException("Can't download file data for file '" + 
1385 02 Sep 11 martin 89           manifestFile.getName() + "'; location=" + manifestFile.getLocation());
1385 02 Sep 11 martin 90     }
1385 02 Sep 11 martin 91     InputStream in = manifestFile.getDownloadStream(0);
1385 02 Sep 11 martin 92     
1385 02 Sep 11 martin 93     BufferedReader reader = new BufferedReader(new InputStreamReader(in));
1385 02 Sep 11 martin 94     List<String> headings = new ArrayList<String>();
1385 02 Sep 11 martin 95     String line = null;
1385 02 Sep 11 martin 96     boolean valid = false;
1385 02 Sep 11 martin 97     do
1385 02 Sep 11 martin 98     {
1385 02 Sep 11 martin 99       try
1385 02 Sep 11 martin 100       {
1385 02 Sep 11 martin 101         line = reader.readLine();
1385 02 Sep 11 martin 102         headings.add(line);
1385 02 Sep 11 martin 103         if (line.matches("\\[Assay\\]"))
1385 02 Sep 11 martin 104         {
1385 02 Sep 11 martin 105           line = reader.readLine();
1385 02 Sep 11 martin 106           valid = true;
1385 02 Sep 11 martin 107           for (String column : requiredColumns)
1385 02 Sep 11 martin 108           {
1385 02 Sep 11 martin 109             valid = line.contains(column) ? valid : false;
1385 02 Sep 11 martin 110           }
1385 02 Sep 11 martin 111         }
1385 02 Sep 11 martin 112       }
1385 02 Sep 11 martin 113       catch(IOException ioex)
1385 02 Sep 11 martin 114       {
1385 02 Sep 11 martin 115         line = null;
1385 02 Sep 11 martin 116       }      
1385 02 Sep 11 martin 117     }while(!valid && (headings.size() < range) && line != null);
1385 02 Sep 11 martin 118     if (!valid)
1385 02 Sep 11 martin 119     {
1385 02 Sep 11 martin 120       throw new InvalidDataException("Could not find required parts in the file");
1385 02 Sep 11 martin 121     }    
1385 02 Sep 11 martin 122     return headings;
1385 02 Sep 11 martin 123   }
1385 02 Sep 11 martin 124
1385 02 Sep 11 martin 125   private int getNumFeatures(List<String> headings)
1385 02 Sep 11 martin 126   {
1385 02 Sep 11 martin 127     int numFeatures = 0;
1385 02 Sep 11 martin 128     if (headings != null && !(headings.size() < 1))
1385 02 Sep 11 martin 129     {
1385 02 Sep 11 martin 130       for (String s : headings)
1385 02 Sep 11 martin 131       {
1385 02 Sep 11 martin 132         String num = null;
1385 02 Sep 11 martin 133         if (s.startsWith(snpCountHeading))
1385 02 Sep 11 martin 134         {
1385 02 Sep 11 martin 135           num = s.substring(snpCountHeading.length());
1385 02 Sep 11 martin 136         }
1385 02 Sep 11 martin 137         if (num != null)
1385 02 Sep 11 martin 138         {
1385 02 Sep 11 martin 139           try
1385 02 Sep 11 martin 140           {
1385 02 Sep 11 martin 141             numFeatures = Integer.parseInt(num.trim());
1385 02 Sep 11 martin 142           } 
1385 02 Sep 11 martin 143           catch (NumberFormatException nex)
1385 02 Sep 11 martin 144           {
1385 02 Sep 11 martin 145             throw new InvalidDataException(nex);
1385 02 Sep 11 martin 146           }
1385 02 Sep 11 martin 147         }
1385 02 Sep 11 martin 148       }
1385 02 Sep 11 martin 149     }
1385 02 Sep 11 martin 150     return numFeatures;
1385 02 Sep 11 martin 151   }
1385 02 Sep 11 martin 152 }