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

Validate IO in SortBam to provide nicer exceptions #994

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Changes from all 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
8 changes: 5 additions & 3 deletions src/main/scala/com/fulcrumgenomics/bam/SortBam.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@
@arg(flag='s', doc="Order into which to sort the records.") val sortOrder: SamOrder = SamOrder.Coordinate,
@arg(flag='m', doc="Max records in RAM.") val maxRecordsInRam: Int = SamWriter.DefaultMaxRecordsInRam
) extends FgBioTool with LazyLogging {

Io.assertReadable(input)
Io.assertCanWriteFile(output)

Check warning on line 64 in src/main/scala/com/fulcrumgenomics/bam/SortBam.scala

View check run for this annotation

Codecov / codecov/patch

src/main/scala/com/fulcrumgenomics/bam/SortBam.scala#L63-L64

Added lines #L63 - L64 were not covered by tests
Comment on lines +63 to +64
Copy link
Contributor

Choose a reason for hiding this comment

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

Naive question, what changes when these are moved to the constructor/class body?

Since the constructor doesn't do anything else, and nothing happens in execute before the assert, I would have thought that asserting at either location would be functionally the same

Copy link
Member Author

Choose a reason for hiding this comment

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

Exceptions in the constructor are ValidationExceptions, and any exceptions (typically validation errors of the arguments) are nicely formatted on the command line, versus a huge stacktrace. And it will print out the usage too. But none of those things will happen when running the tool


override def execute(): Unit = {
Io.assertReadable(input)
Io.assertCanWriteFile(output)

val in = SamSource(input)
val out = SamWriter(output, in.header.clone(), sort=Some(sortOrder), maxRecordsInRam=maxRecordsInRam)
out ++= in
out.close()
in.safelyClose()

Check warning on line 71 in src/main/scala/com/fulcrumgenomics/bam/SortBam.scala

View check run for this annotation

Codecov / codecov/patch

src/main/scala/com/fulcrumgenomics/bam/SortBam.scala#L71

Added line #L71 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

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

Also a naive question, why doesn't out also take safelyClose()?

Copy link
Member Author

Choose a reason for hiding this comment

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

The idea is that we care that the output files are closed, but do not want the tool to fail/crash/except when we're cleaning up resources after the main effort of the tool has completed, so we do not propagate errors during closing inputs in this case.

}
}
Loading