Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load vcf bgz #238

Merged
merged 7 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/main/java/au/csiro/variantspark/utils/FileUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package au.csiro.variantspark.utils;

import java.io.*;
import java.util.zip.GZIPInputStream;
import java.io.IOException;
import htsjdk.samtools.util.BlockCompressedInputStream;

public class FileUtils {

/**
*
* @param file: an input file
* @return true if input file is BGZIP by check the first two byte of input file
*/
public static boolean isBGZFile(String filePath) {
/**
* .vcf.bgz is type of GZP file, work well with BlockCompressedInputStream
* .vcf.gz is also GZP file but get java.lang.OutOfMemoryError at java.io.InputStreamReader.read(InputStreamReader.java:184)
* .vcf.bz2 is not GZP file and get java.lang.OutOfMemoryError at java.io.InputStreamReader.read(InputStreamReader.java:184)
* .vcf is not GZP file and get htsjdk.samtools.SAMFormatException: at header from java.io.BufferedReader.readLine(BufferedReader.java:389)
*/
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(filePath))) {
//bufferedInputStream.mark(100); // mark the current position
ChristinaXu2017 marked this conversation as resolved.
Show resolved Hide resolved
boolean isValid = BlockCompressedInputStream.isValidFile(bufferedInputStream);
//bufferedInputStream.reset(); // reset back to the marked position
ChristinaXu2017 marked this conversation as resolved.
Show resolved Hide resolved
return isValid;
} catch (IOException e) {
// Handle the exception
return false;
}
}

/**
*
* @param file: an input file
* @return true if input file is Gzip by check the first two byte of input file
* @throws IOException
*/
public static boolean isInputGZip(final File file) throws IOException {
ChristinaXu2017 marked this conversation as resolved.
Show resolved Hide resolved
//final PushbackInputStream pb = new PushbackInputStream(input, 2);

try(final InputStream input = new FileInputStream(file)){
int header = input.read(); //read ID1
if(header == -1) return false;

int b = input.read(); //read ID2
if(b == -1) return false;

//ID2 * 256 + ID1 = 35615
if( ( (b << 8) | header) == GZIPInputStream.GZIP_MAGIC)
return true;
}

return false;
}
}
19 changes: 16 additions & 3 deletions src/main/scala/au/csiro/variantspark/cli/args/SparkArgs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,28 @@ package au.csiro.variantspark.cli.args

import org.kohsuke.args4j.Option
import au.csiro.pbdava.ssparkle.spark.SparkApp
import au.csiro.variantspark.utils._
import org.apache.spark.rdd.RDD
import htsjdk.samtools.util.BlockCompressedInputStream
import org.apache.hadoop.fs.Path
import java.io.File

trait SparkArgs extends SparkApp {

@Option(name = "-sp", required = false, usage = "Spark parallelism (def=<default-spark-par>)",
aliases = Array("--spark-par"))
val sparkPar: Int = 0

def textFile(inputFile: String): RDD[String] =
sc.textFile(inputFile, if (sparkPar > 0) sparkPar else sc.defaultParallelism)

def textFile(inputFile: String): RDD[String] = {
val isBGZ = FileUtils.isBGZFile(inputFile)
println(inputFile + " is loading to spark RDD, isBGZFile: " + isBGZ)
if (isBGZ) {
val path = new Path(inputFile)
val fs = path.getFileSystem(sc.hadoopConfiguration)
val bgzInputStream = new BlockCompressedInputStream(fs.open(path))
sc.parallelize(Stream.continually(bgzInputStream.readLine()).takeWhile(_ != null).toList)
} else {
sc.textFile(inputFile, if (sparkPar > 0) sparkPar else sc.defaultParallelism)
ChristinaXu2017 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Loading