public static void calculateCoverate ( Path bamPath , Path sqlPath ) throws IOException , AlignmentCoverageException { BamManager bamManager = new BamManager ( bamPath ) ; // Check if the bam index (.bai) does not exit, then create it if ( ! bamPath . getParent ( ) . resolve ( bamPath . getFileName ( ) . toString ( ) + ".bai" ) . toFile ( ) . exists ( ) ) { bamManager . createIndex ( ) ; } // Calculate coverage and store in SQLite SAMFileHeader fileHeader = BamUtils . getFileHeader ( bamPath ) ; // long start = System.currentTimeMillis(); initDatabase ( fileHeader . getSequenceDictionary ( ) . getSequences ( ) , sqlPath ) ; // System.out.println("SQLite database initialization, in " + ((System.currentTimeMillis() - start) / 1000.0f) // + " s."); Path coveragePath = sqlPath . toAbsolutePath ( ) . resolve ( bamPath . getFileName ( ) + COVERAGE_SUFFIX ) ; AlignmentOptions options = new AlignmentOptions ( ) ; options . setContained ( false ) ; Iterator < SAMSequenceRecord > iterator = fileHeader . getSequenceDictionary ( ) . getSequences ( ) . iterator ( ) ; PrintWriter writer = new PrintWriter ( coveragePath . toFile ( ) ) ; StringBuilder line ; // start = System.currentTimeMillis(); while ( iterator . hasNext ( ) ) { SAMSequenceRecord next = iterator . next ( ) ; for ( int i = 0 ; i < next . getSequenceLength ( ) ; i += MINOR_CHUNK_SIZE ) { Region region = new Region ( next . getSequenceName ( ) , i + 1 , Math . min ( i + MINOR_CHUNK_SIZE , next . getSequenceLength ( ) ) ) ; RegionCoverage regionCoverage = bamManager . coverage ( region , null , options ) ; int meanDepth = Math . min ( regionCoverage . meanCoverage ( ) , 255 ) ; // File columns: chunk chromosome start end coverage // chunk format: chrom_id_suffix, where: // id: int value starting at 0 // suffix: chunkSize + k // eg. 3_4_1k line = new StringBuilder ( ) ; line . append ( region . getChromosome ( ) ) . append ( "_" ) ; line . append ( i / MINOR_CHUNK_SIZE ) . append ( "_" ) . append ( MINOR_CHUNK_SIZE / 1000 ) . append ( "k" ) ; line . append ( "\t" ) . append ( region . getChromosome ( ) ) ; line . append ( "\t" ) . append ( region . getStart ( ) ) ; line . append ( "\t" ) . append ( region . getEnd ( ) ) ; line . append ( "\t" ) . append ( meanDepth ) ; writer . println ( line . toString ( ) ) ; } } writer . close ( ) ; // System.out.println("Mean coverage file creation, in " + ((System.currentTimeMillis() - start) / 1000.0f) + " s."); // save file to db // start = System.currentTimeMillis(); insertCoverageDB ( bamPath , sqlPath ) ; // System.out.println("SQLite database population, in " + ((System.currentTimeMillis() - start) / 1000.0f) + " s."); }
Calculate the coverage for the input BAM file . The coverage is stored in a SQLite database located in the sqlPath output directory .