-
Notifications
You must be signed in to change notification settings - Fork 311
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
Resolves various single file save/header issues #1104
Merged
heuermh
merged 7 commits into
bigdatagenomics:master
from
fnothaft:issues/1009-single-file-save
Sep 7, 2016
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d5221a5
[ADAM-1009] Port single file save code over to VCF.
fnothaft 06180e0
[ADAM-1059] Implemented single file save with SAM header for Interval…
fnothaft 8cec2c0
[ADAM-1058] Add support for -single across all Feature formats.
fnothaft 48caa3f
[ADAM-676] Clean up header issues for sharded files.
fnothaft c66da78
[ADAM-1106] Improve feature unit test coverage.
fnothaft 3b99446
Fixed samtools reading issue.
fnothaft 79749ae
Addressing review comments.
fnothaft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
adam-core/src/main/scala/org/bdgenomics/adam/rdd/FileMerger.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/** | ||
* 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.rdd | ||
|
||
import htsjdk.samtools.util.BlockCompressedStreamConstants | ||
import java.io.{ InputStream, OutputStream } | ||
import org.apache.hadoop.fs.{ FileSystem, Path } | ||
import org.bdgenomics.utils.misc.Logging | ||
import scala.annotation.tailrec | ||
|
||
/** | ||
* Helper object to merge sharded files together. | ||
*/ | ||
private[rdd] object FileMerger extends Logging { | ||
|
||
/** | ||
* Merges together sharded files, while preserving partition ordering. | ||
* | ||
* @param fs The file system implementation to use. | ||
* @param outputPath The location to write the merged file at. | ||
* @param tailPath The location where the sharded files have been written. | ||
* @param optHeaderPath Optionally, the location where a header file has | ||
* been written. | ||
* @param writeEmptyGzipBlock If true, we write an empty GZIP block at the | ||
* end of the merged file. | ||
* @param bufferSize The size in bytes of the buffer used for copying. | ||
*/ | ||
def mergeFiles(fs: FileSystem, | ||
outputPath: Path, | ||
tailPath: Path, | ||
optHeaderPath: Option[Path] = None, | ||
writeEmptyGzipBlock: Boolean = false, | ||
bufferSize: Int = 1024) { | ||
|
||
// get a list of all of the files in the tail file | ||
val tailFiles = fs.globStatus(new Path("%s/part-*".format(tailPath))) | ||
.toSeq | ||
.map(_.getPath) | ||
.sortBy(_.getName) | ||
.toArray | ||
|
||
// doing this correctly is surprisingly hard | ||
// specifically, copy merge does not care about ordering, which is | ||
// fine if your files are unordered, but if the blocks in the file | ||
// _are_ ordered, then hahahahahahahahahaha. GOOD. TIMES. | ||
// | ||
// fortunately, the blocks in our file are ordered | ||
// the performance of this section is hilarious | ||
// | ||
// specifically, the performance is hilariously bad | ||
// | ||
// but! it is correct. | ||
|
||
// open our output file | ||
val os = fs.create(outputPath) | ||
|
||
// here is a byte array for copying | ||
val ba = new Array[Byte](bufferSize) | ||
|
||
@tailrec def copy(is: InputStream, | ||
los: OutputStream) { | ||
|
||
// make a read | ||
val bytesRead = is.read(ba) | ||
|
||
// did our read succeed? if so, write to output stream | ||
// and continue | ||
if (bytesRead >= 0) { | ||
los.write(ba, 0, bytesRead) | ||
|
||
copy(is, los) | ||
} | ||
} | ||
|
||
// optionally copy the header | ||
optHeaderPath.foreach(p => { | ||
log.info("Copying header file (%s)".format(p)) | ||
|
||
// open our input file | ||
val is = fs.open(p) | ||
|
||
// until we are out of bytes, copy | ||
copy(is, os) | ||
|
||
// close our input stream | ||
is.close() | ||
}) | ||
|
||
// loop over allllll the files and copy them | ||
val numFiles = tailFiles.length | ||
var filesCopied = 1 | ||
tailFiles.toSeq.foreach(p => { | ||
|
||
// print a bit of progress logging | ||
log.info("Copying file %s, file %d of %d.".format( | ||
p.toString, | ||
filesCopied, | ||
numFiles)) | ||
|
||
// open our input file | ||
val is = fs.open(p) | ||
|
||
// until we are out of bytes, copy | ||
copy(is, os) | ||
|
||
// close our input stream | ||
is.close() | ||
|
||
// increment file copy count | ||
filesCopied += 1 | ||
}) | ||
|
||
// finish the file off | ||
if (writeEmptyGzipBlock) { | ||
os.write(BlockCompressedStreamConstants.EMPTY_GZIP_BLOCK); | ||
} | ||
|
||
// flush and close the output stream | ||
os.flush() | ||
os.close() | ||
|
||
// delete temp files | ||
optHeaderPath.foreach(headPath => fs.delete(headPath, true)) | ||
fs.delete(tailPath, true) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I have this correctly, on either
INPUT
orOUTPUT
if the file extension is not recognized as a plain-text feature file, then it is assumed to be parquet. That way this one command can convert both ways. Theusage
strings below should be updated.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct. I'll update the usage strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM