public static Path index ( Path wigPath ) throws Exception { Path dbPath = wigPath . getParent ( ) . resolve ( WIG_DB ) ; ChunkFrequencyManager chunkFrequencyManager = new ChunkFrequencyManager ( dbPath ) ; // get the chunk size int chunkSize = chunkFrequencyManager . getChunkSize ( ) ; String chromosome = null ; int step , span = 1 , start = 1 , end ; int startChunk , endChunk , partial ; boolean empty = true ; List < Integer > values = new ArrayList <> ( ) ; // reader BufferedReader bufferedReader = FileUtils . newBufferedReader ( wigPath ) ; // main loop String line = bufferedReader . readLine ( ) ; while ( line != null ) { // check for header lines if ( WigUtils . isHeaderLine ( line ) ) { if ( ! empty ) { // save values for the current chromosome into the database System . out . println ( "\tStoring " + values . size ( ) + " values for " + chromosome ) ; // if (chromosome.equals("chr1")) { computeAndSaveMeanValues ( values , wigPath , chromosome , chunkSize , chunkFrequencyManager ) ; // } } System . out . println ( "Loading wig data:" + line ) ; if ( WigUtils . isVariableStep ( line ) ) { throw new UnsupportedOperationException ( "Wig coverage file with 'variableStep'" + " is not supported yet." ) ; } // update some values step = WigUtils . getStep ( line ) ; span = WigUtils . getSpan ( line ) ; start = WigUtils . getStart ( line ) ; chromosome = WigUtils . getChromosome ( line ) ; empty = true ; values = new ArrayList <> ( ) ; // sanity check if ( start <= 0 ) { throw new UnsupportedOperationException ( "Wig coverage file with" + " 'start' <= 0, it must be greater than 0." ) ; } if ( start != 1 ) { // we have to put zeros until the start position for ( int i = 0 ; i < start ; i ++ ) { values . add ( 0 ) ; } } if ( step != 1 ) { throw new UnsupportedOperationException ( "Wig coverage file with" + " 'step' != 1 is not supported yet." ) ; } // next line... line = bufferedReader . readLine ( ) ; } else { if ( values != null ) { end = start + span - 1 ; startChunk = start / chunkSize ; endChunk = end / chunkSize ; for ( int chunk = startChunk , pos = startChunk * chunkSize ; chunk <= endChunk ; chunk ++ , pos += chunkSize ) { // compute how many values are within the current chunk // and update the chunk partial = Math . min ( end , pos + chunkSize ) - Math . max ( start , pos ) ; values . add ( partial * Integer . parseInt ( line ) ) ; empty = false ; } start += span ; } // next line... line = bufferedReader . readLine ( ) ; } } if ( ! empty ) { // save values for the current chromosome into the database System . out . println ( "\tStoring " + values . size ( ) + " values for " + chromosome ) ; // if (chromosome.equals("chr1")) { computeAndSaveMeanValues ( values , wigPath , chromosome , chunkSize , chunkFrequencyManager ) ; // } } return dbPath ; }
Index the entire Wig file content in a SQLite database managed by the ChunkFrequencyManager .