Ignore:
Timestamp:
May 17, 2011, 1:44:07 PM (8 years ago)
Author:
robert@…
Message:

Implemented importing of classifications

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/services/nl/tno/massSequencing/files/ExcelService.groovy

    r52 r58  
    11package nl.tno.massSequencing.files
    22
     3import groovy.lang.Closure;
     4
     5import java.io.File;
    36import java.text.DecimalFormat
    47import java.text.Format
    58import java.text.NumberFormat
     9import java.util.Map;
     10
    611import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    712import org.apache.poi.hssf.usermodel.HSSFWorkbook;
     
    1419 *
    1520 */
    16 class ExcelService {
    17     static transactional = true
     21class ExcelService implements nl.tno.massSequencing.imports.Importer {
     22    static transactional = false
    1823       
    1924        /**
     
    254259
    255260        }
     261       
     262       
     263        /**
     264         * Determines whether a file can be processed.
     265         * @param filetype      Filetype of the file
     266         * @see determineFileType()
     267         * @return
     268         */
     269        public boolean canParseFileType( String filetype ) {
     270                switch( filetype ) {
     271                        case "excel":
     272                                return true;
     273                        default:
     274                                return false;
     275                }
     276        }
     277
     278        /**
     279         * Parses the given excel file
     280         * @param file                  File to parse
     281         * @param filetype              Type of the given file (only excel is accepted)
     282         * @param onProgress    Closure to execute when progress indicators should be updated.
     283         *                                              Has 2 parameters: progress that indicates the number of bytes that have been processed. The second parameter determines
     284         *                                              the number of bytes that has to be processed extra after this action (e.g. when a zip file is extracted, the extracted
     285         *                                              files should be added to the total number of bytes to be processed)
     286         * @return                              Structure with information about the parsed files. The 'success' files are
     287         *                                              moved to the given directory
     288         *
     289         * Examples:
     290         *                      [filename: 'abc.fasta', type: FASTA, numSequences: 190]
     291         *                      [filename: 'cde.qual', type: QUAL, numSequences: 140, avgQuality: 29]
     292         *
     293         *                      [filename: 'test.zip', type: ZIP, extraFiles: [newfile1.xls, newfile2.xls, newfile3.xls]]
     294         *
     295         *                      [filename: 'testing.xls', type: 'unknown', message: 'Type not recognized']
     296         *
     297         */
     298        public Map parseFile( File file, String filetype, Closure onProgress ) {
     299                switch( filetype ) {
     300                        case "excel":
     301                                return parseExcel( file, onProgress );
     302                        default:
     303                                return [ success: false, type: filetype, message: 'Filetype could not be parsed.' ]
     304                }
     305        }
     306       
     307        /**
     308        * Parses a given excel file with a match between filenames and samples
     309        * @param file                   File to parse
     310        * @param onProgress             Closure to execute when progress indicators should be updated.
     311        *                                               Has 2 parameters: numFilesProcessed and numBytesProcessed that indicate the number
     312        *                                               of files and bytes that have been processed in this file (so the first parameter should
     313        *                                               only be 1 when the file is finished)
     314        * @return                               List structure. The matches array contains an array of matches between filenames and sample(name)s.
     315        *                                               The extension for all files are removed in the 'basename' parameter, in order to improve matching.
     316        *                                               Examples:
     317        *
     318        *   [ success: true, filename: 'abc.xls', type: 'excel', matches: [ [ filename: 's1.qual', basename: 's1', sample: 'sample a' ], [ filename: 's9.fna', basename: 's9', sample: 'sample b' ] ]
     319        *   [ success: false, filename: 'def.xls', type: 'excel', message: 'File is not a valid XLS file' ]
     320        */
     321   protected def parseExcel( File file, Closure onProgress ) {
     322           long startTime = System.nanoTime();
     323           log.trace "Start parsing XLS " + file.getName()
     324
     325           def matches = []
     326
     327           // Read excel file
     328           def wb;
     329           try {
     330                   wb = open( file );
     331           } catch( Exception e ) {
     332                   // If an exception occurs, the file can't be opened. Return the error message
     333                   return [ success: false, type: "excel", filename: file.getName(), message: "Excel file could not be opened or parsed."]
     334           }
     335
     336           // Read all data into an array, and the header in a separate array
     337           def header = readRow( wb, 0, 0 );
     338           def data = readData( wb, 0, 1 );
     339
     340           // Update progress and return
     341           onProgress( file.size(), 0 );
     342           return [ success: true, type: "excel", filename: file.getName(), header: header, data: data ];
     343   }
     344
     345       
    256346}
Note: See TracChangeset for help on using the changeset viewer.