Skip to content

Commit

Permalink
Removing IntervalListWriter toFile call
Browse files Browse the repository at this point in the history
* fixes #1297
* nitpicky formatting and cleanup
  • Loading branch information
lbergelson committed Feb 26, 2019
1 parent a6c5837 commit d6b44c6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 38 deletions.
22 changes: 9 additions & 13 deletions src/main/java/htsjdk/samtools/util/IntervalListWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
* to write, such that they all cannot be held in memory, for example in an {@link IntervalList}.
*/
public class IntervalListWriter implements Closeable {
private static final char TAB = '\t';

private final BufferedWriter out;
private final FormatUtil format = new FormatUtil();

/** Creates a new writer, writing a header to the file.
* @param path a path to write to. If exists it will be overwritten.
Expand All @@ -52,8 +52,8 @@ public IntervalListWriter(final Path path) {
* @param path a file to write to. If exists it will be overwritten.
* @param header the header to write.
*/
public IntervalListWriter(final Path path, final SAMFileHeader header) {
out = IOUtil.openFileForBufferedWriting(path.toFile());
public IntervalListWriter(final Path path, final SAMFileHeader header) {
out = IOUtil.openFileForBufferedWriting(path);

// Write out the header
if (header != null) {
Expand All @@ -67,23 +67,19 @@ public IntervalListWriter(final Path path, final SAMFileHeader header) {
*/
public void write(final Interval interval) throws IOException {
out.write(interval.getContig());
out.write('\t');
out.write(TAB);
out.write(Integer.toString(interval.getStart()));
out.write('\t');
out.write(TAB);
out.write(Integer.toString(interval.getEnd()));
out.write('\t');
out.write(TAB);
out.write(interval.isPositiveStrand() ? '+' : '-');
out.write('\t');
if(interval.getName() != null){
out.write(interval.getName());
}
else{
out.write(".");
}
out.write(TAB);
out.write(interval.getName() != null ? interval.getName() : ".");
out.newLine();
}

/** Closes the writer. */
@Override
public void close() throws IOException {
out.flush();
out.close();
Expand Down
31 changes: 15 additions & 16 deletions src/test/java/htsjdk/samtools/util/IntervalCodecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,26 @@ public void testEndToEnd() throws IOException {
expectedList.add(new Interval("chr1", 50, 150, true, "number-5"));
expectedList.add(new Interval("chr1", 150, 250, false, "number-6"));

final OutputStream outputStream = new FileOutputStream(tempFile);
final IntervalCodec writeCodec = new IntervalCodec(this.dict);
writeCodec.setOutputStream(outputStream);
for (final Interval interval : expectedList.getIntervals()) {
writeCodec.encode(interval);
try(final OutputStream outputStream = new FileOutputStream(tempFile)) {
final IntervalCodec writeCodec = new IntervalCodec(this.dict);
writeCodec.setOutputStream(outputStream);
for (final Interval interval : expectedList.getIntervals()) {
writeCodec.encode(interval);
}
}
outputStream.close();

final IntervalCodec readCodec = new IntervalCodec(this.dict);
final InputStream inputStream = new FileInputStream(tempFile);
readCodec.setInputStream(inputStream);
while (true) {
final Interval interval = readCodec.decode();
if (interval == null) {
break;
}
else {
actualList.add(interval);
try(final InputStream inputStream = new FileInputStream(tempFile)) {
readCodec.setInputStream(inputStream);
while (true) {
final Interval interval = readCodec.decode();
if (interval == null) {
break;
} else {
actualList.add(interval);
}
}
}
inputStream.close();

Assert.assertEquals(
actualList.getIntervals(),
Expand Down
32 changes: 23 additions & 9 deletions src/test/java/htsjdk/samtools/util/IntervalListWriterTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package htsjdk.samtools.util;

import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import com.google.common.jimfs.SystemJimfsFileSystemProvider;
import htsjdk.HtsjdkTest;
import htsjdk.samtools.SAMFileHeader;
import htsjdk.samtools.SAMSequenceDictionary;
Expand All @@ -10,6 +13,9 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;

public class IntervalListWriterTest extends HtsjdkTest {
private SAMSequenceDictionary dict;
Expand All @@ -26,7 +32,18 @@ void setup() {
public void testEndToEnd() throws IOException {
final File tempFile = File.createTempFile("IntervalListWriterTest.", ".interval_list");
tempFile.deleteOnExit();
testEndToEnd(tempFile.toPath());
}

@Test
public void testEndToEndOnPath() throws IOException {
try(FileSystem jimfs = Jimfs.newFileSystem(Configuration.unix())) {
final Path tempFile = Files.createTempFile(jimfs.getRootDirectories().iterator().next(), "IntervalListWriterTest.", ".interval_list");
testEndToEnd(tempFile);
}
}

private void testEndToEnd(Path tempFile) throws IOException {
final IntervalList expectedList = new IntervalList(this.dict);

expectedList.add(new Interval("chr1", 50, 150));
Expand All @@ -36,19 +53,16 @@ public void testEndToEnd() throws IOException {
expectedList.add(new Interval("chr1", 50, 150, true, "number-5"));
expectedList.add(new Interval("chr1", 150, 250, false, "number-6"));

final IntervalListWriter writer = new IntervalListWriter(tempFile.toPath(), new SAMFileHeader(this.dict));
for (final Interval interval : expectedList.getIntervals()) {
writer.write(interval);
try (final IntervalListWriter writer = new IntervalListWriter(tempFile, new SAMFileHeader(this.dict))) {
for (final Interval interval : expectedList.getIntervals()) {
writer.write(interval);
}
}
writer.close();

final IntervalList actualList = IntervalList.fromFile(tempFile);
final IntervalList actualList = IntervalList.fromPath(tempFile);
final SAMSequenceDictionary actualDict = actualList.getHeader().getSequenceDictionary();
final SAMSequenceDictionary expectedDict = expectedList.getHeader().getSequenceDictionary();
Assert.assertTrue(actualDict.isSameDictionary(expectedDict));
Assert.assertEquals(
actualList.getIntervals(),
expectedList.getIntervals()
);
Assert.assertEquals(actualList.getIntervals(), expectedList.getIntervals());
}
}

0 comments on commit d6b44c6

Please sign in to comment.