Add fromPath to IntervalList. #846

Merged
merged 2 commits into from Apr 20, 2017
Jump to file or symbol
Failed to load files and symbols.
+30 −3
Split
@@ -778,9 +778,14 @@ public static File createTempDir(final String prefix, final String suffix) {
/** Checks that a file exists and is readable, and then returns a buffered reader for it. */
public static BufferedReader openFileForBufferedReading(final File file) {
- return new BufferedReader(new InputStreamReader(openFileForReading(file)), Defaults.NON_ZERO_BUFFER_SIZE);
+ return openFileForBufferedReading(file.toPath());
}
+ /** Checks that a path exists and is readable, and then returns a buffered reader for it. */
+ public static BufferedReader openFileForBufferedReading(final Path path) {
+ return new BufferedReader(new InputStreamReader(openFileForReading(path)), Defaults.NON_ZERO_BUFFER_SIZE);
+ }
+
/** Takes a string and replaces any characters that are not safe for filenames with an underscore */
public static String makeFileNameSafe(final String str) {
return str.trim().replaceAll("[\\s!\"#$%&'()*/:;<=>?@\\[\\]\\\\^`{|}~]", "_");
@@ -34,6 +34,7 @@
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -397,12 +398,21 @@ public static IntervalList copyOf(final IntervalList list){
* @return an IntervalList object that contains the headers and intervals from the file
*/
public static IntervalList fromFile(final File file) {
- final BufferedReader reader= IOUtil.openFileForBufferedReading(file);
+ return fromPath(file.toPath());
+ }
+
+ /**
+ * Parses an interval list from a path.
+ * @param path the path containing the intervals
+ * @return an IntervalList object that contains the headers and intervals from the path
+ */
+ public static IntervalList fromPath(final Path path) {
+ final BufferedReader reader = IOUtil.openFileForBufferedReading(path);
final IntervalList list = fromReader(reader);
try {
reader.close();
} catch (final IOException e) {
- throw new SAMException(String.format("Failed to close file %s after reading",file));
@lbergelson

lbergelson Apr 7, 2017

Contributor

You might want to use the more ugly path.toUri().toString() because the path.toString() tends to drop the scheme.

+ throw new SAMException(String.format("Failed to close file %s after reading", path.toUri().toString()));
}
return list;
@@ -25,14 +25,17 @@
package htsjdk.samtools.util;
import htsjdk.samtools.SAMFileHeader;
+import htsjdk.samtools.SAMSequenceDictionary;
import htsjdk.samtools.SAMSequenceRecord;
+import htsjdk.samtools.SamFileHeaderMerger;
import htsjdk.variant.vcf.VCFFileReader;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.io.File;
+import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -75,6 +78,15 @@ public IntervalListTest() {
list3.add(new Interval("3", 50, 470));
}
+ @Test
+ public void testIntervalListFrom() {
+ final String testPath = "src/test/resources/htsjdk/samtools/intervallist/IntervalListFromVCFTestComp.interval_list";
+ final IntervalList fromFileList = IntervalList.fromFile(new File(testPath));
+ final IntervalList fromPathList = IntervalList.fromPath(Paths.get(testPath));
+ fromFileList.getHeader().getSequenceDictionary().assertSameDictionary(fromPathList.getHeader().getSequenceDictionary());
+ Assert.assertEquals(CollectionUtil.makeCollection(fromFileList.iterator()), CollectionUtil.makeCollection(fromPathList.iterator()));
+ }
+
@DataProvider(name = "intersectData")
public Object[][] intersectData() {
final IntervalList intersect123 = new IntervalList(fileHeader);