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

[ADAM-1481] Refactor ADAMContext loadXxx methods for consistency #1487

Merged
merged 8 commits into from Apr 19, 2017
Expand Up @@ -52,15 +52,15 @@ class JavaADAMContext(val ac: ADAMContext) extends Serializable {
}

/**
* Loads in sequence fragments.
* Loads in nucleotide contig fragments.
*
* Can load from FASTA or from Parquet encoded NucleotideContigFragments.
*
* @param pathName Path to load the file from.
* @return Returns a NucleotideContigFragment RDD.
*/
def loadSequences(pathName: java.lang.String): NucleotideContigFragmentRDD = {
ac.loadSequences(pathName)
def loadContigFragments(pathName: java.lang.String): NucleotideContigFragmentRDD = {
ac.loadContigFragments(pathName)
}

/**
Expand All @@ -84,7 +84,7 @@ class JavaADAMContext(val ac: ADAMContext) extends Serializable {
}

/**
* Loads in a coverage file. This method can load BED, NarrowPeak, GFF3, GTF/GFF2, IntervalList and ADAM files.
* Loads in a coverage file. This method can load BED6/12, NarrowPeak, GFF3, GTF/GFF2, IntervalList and ADAM files.
*
* @param pathName Path to load the file from.
* @return Returns a Coverage RDD.
Expand Down
Expand Up @@ -38,6 +38,6 @@ public static NucleotideContigFragmentRDD conduit(final NucleotideContigFragment

// create a new adam context and load the file
JavaADAMContext jac = new JavaADAMContext(ac);
return jac.loadSequences(fileName);
return jac.loadContigFragments(fileName);
}
}
Expand Up @@ -39,7 +39,7 @@ class JavaADAMContextSuite extends ADAMFunSuite {

sparkTest("can read and write a small FASTA file") {
val path = copyResource("chr20.250k.fa.gz")
val aRdd = sc.loadSequences(path)
val aRdd = sc.loadContigFragments(path)
assert(aRdd.jrdd.count() === 26)

val newRdd = JavaADAMContigConduit.conduit(aRdd, sc)
Expand Down
Expand Up @@ -50,7 +50,7 @@ class ADAM2Fasta(val args: ADAM2FastaArgs) extends BDGSparkCommand[ADAM2FastaArg
override def run(sc: SparkContext): Unit = {

log.info("Loading ADAM nucleotide contig fragments from disk.")
val contigFragments = sc.loadSequences(args.inputPath)
val contigFragments = sc.loadContigFragments(args.inputPath)

log.info("Merging fragments and writing FASTA to disk.")
val contigs = contigFragments.mergeFragments()
Expand Down
Expand Up @@ -49,7 +49,7 @@ class CountContigKmers(protected val args: CountContigKmersArgs) extends BDGSpar
def run(sc: SparkContext) {

// read from disk
val fragments = sc.loadSequences(args.inputPath)
val fragments = sc.loadContigFragments(args.inputPath)

// count kmers
val countedKmers = fragments.countKmers(args.kmerLength)
Expand Down
329 changes: 165 additions & 164 deletions adam-core/src/main/scala/org/bdgenomics/adam/rdd/ADAMContext.scala

Large diffs are not rendered by default.

145 changes: 145 additions & 0 deletions adam-core/src/main/scala/org/bdgenomics/adam/util/FileExtensions.scala
@@ -0,0 +1,145 @@
/**
* Licensed to Big Data Genomics (BDG) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The BDG licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bdgenomics.adam.util

/**
* Match path names to file extensions.
*/
object FileExtensions {

/**
* @param pathName The path name to match.
* @return Returns true if the path name matches a 2bit format file extension.
*/
private[adam] def is2BitExt(pathName: String): Boolean = {
pathName.endsWith(".2bit")
}

/**
* @param pathName The path name to match.
* @return Returns true if the path name matches a BAM/CRAM/SAM format file extension.
*/
private[adam] def isBamExt(pathName: String): Boolean = {
pathName.endsWith(".bam") ||
pathName.endsWith(".cram") ||
pathName.endsWith(".sam")
}

/**
* @param pathName The path name to match.
* @return Returns true if the path name matches a BED6/12 format file extension.
*/
private[adam] def isBedExt(pathName: String): Boolean = {
pathName.endsWith(".bed") ||
pathName.endsWith(".bed.gz") ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an aside, for all of the compressed file format checks, we should use Hadoop's CompressionCodecFactory to check if the path is a compressed file. This allows users to swap in additional compression codecs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I suppose we could do an isCompressed check in ADAMContext, since it has access to the Hadoop configuration, then trim off the extension before checking with FileExtensions

pathName.endsWith(".bed.bz2")
}

/**
* @param pathName The path name to match.
* @return Returns true if the path name matches a FASTA format file extension.
*/
private[adam] def isFastaExt(pathName: String): Boolean = {
pathName.endsWith(".fa") ||
pathName.endsWith(".fa.gz") ||
pathName.endsWith(".fa.bz2") ||
pathName.endsWith(".fasta") ||
pathName.endsWith(".fasta.gz") ||
pathName.endsWith(".fasta.bz2")
}

/**
* @param pathName The path name to match.
* @return Returns true if the path name matches a FASTQ format file extension.
*/
private[adam] def isFastqExt(pathName: String): Boolean = {
pathName.endsWith(".fq") ||
pathName.endsWith(".fq.gz") ||
pathName.endsWith(".fq.bz2") ||
pathName.endsWith(".fastq") ||
pathName.endsWith(".fastq.gz") ||
pathName.endsWith(".fastq.bz2")
}

/**
* @param pathName The path name to match.
* @return Returns true if the path name matches a GFF3 format file extension.
*/
private[adam] def isGff3Ext(pathName: String): Boolean = {
pathName.endsWith(".gff3") ||
pathName.endsWith(".gff3.gz") ||
pathName.endsWith(".gff3.bz2")
}

/**
* @param pathName The path name to match.
* @return Returns true if the path name matches a GFF2/GTF format file extension.
*/
private[adam] def isGtfExt(pathName: String): Boolean = {
pathName.endsWith(".gff") ||
pathName.endsWith(".gff.gz") ||
pathName.endsWith(".gff.bz2") ||
pathName.endsWith(".gtf") ||
pathName.endsWith(".gtf.gz") ||
pathName.endsWith(".gtf.bz2")
}

/**
* @param pathName The path name to match.
* @return Returns true if the path name matches an interleaved FASTQ format file extension.
*/
private[adam] def isInterleavedFastqExt(pathName: String): Boolean = {
pathName.endsWith(".ifq") ||
pathName.endsWith(".ifq.gz") ||
pathName.endsWith(".ifq.bz2")
}

/**
* @param pathName The path name to match.
* @return Returns true if the path name matches an IntervalList format file extension.
*/
private[adam] def isIntervalListExt(pathName: String): Boolean = {
pathName.endsWith(".interval_list") ||
pathName.endsWith(".interval_list.gz") ||
pathName.endsWith(".interval_list.bz2")
}

/**
* @param pathName The path name to match.
* @return Returns true if the path name matches a NarrowPeak format file extension.
*/
private[adam] def isNarrowPeakExt(pathName: String): Boolean = {
pathName.endsWith(".narrowpeak") ||
pathName.endsWith(".narrowpeak.gz") ||
pathName.endsWith(".narrowpeak.bz2") ||
pathName.endsWith(".narrowPeak") ||
pathName.endsWith(".narrowPeak.gz") ||
pathName.endsWith(".narrowPeak.bz2")
}

/**
* @param pathName The path name to match.
* @return Returns true if the path name matches a VCF format file extension.
*/
private[adam] def isVcfExt(pathName: String): Boolean = {
pathName.endsWith(".vcf") ||
pathName.endsWith(".vcf.gz") ||
pathName.endsWith(".vcf.bgzf") ||
pathName.endsWith(".vcf.bgz")
}
}
Expand Up @@ -209,7 +209,7 @@ class FastaConverterSuite extends ADAMFunSuite {

sparkTest("convert reference fasta file") {
//Loading "human_g1k_v37_chr1_59kb.fasta"
val referenceSequences = sc.loadSequences(chr1File, maximumFragmentLength = 10).rdd.collect()
val referenceSequences = sc.loadContigFragments(chr1File, maximumFragmentLength = 10).rdd.collect()
assert(referenceSequences.forall(_.getContig.getContigName.toString == "1"))
assert(referenceSequences.slice(0, referenceSequences.length - 2).forall(_.getFragmentSequence.length == 10))

Expand Down