Skip to content

Commit

Permalink
Complete path support with samtools#810
Browse files Browse the repository at this point in the history
  • Loading branch information
magicDGS committed Mar 21, 2018
1 parent 66fd3d6 commit 4c67b5e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 25 deletions.
12 changes: 5 additions & 7 deletions src/main/java/htsjdk/tribble/writer/FeatureWriterFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,16 @@ public <F extends Feature> FeatureWriter<F> makeWriter(final String outputFileNa
}

final IndexCreator idxCreator;
final String indexFile;
final Path indexPath;
if (blockCompressed) {
idxCreator = new TabixIndexCreator(seqDict, encoder.getTabixFormat());
indexFile = Tribble.tabixIndexFile(outputFileName);
indexPath = Tribble.tabixIndexPath(outputPath);
} else {
// TODO: https://github.com/samtools/htsjdk/pull/810
// TODO: the dynamic index should use the corresponding Path constructor
idxCreator = new DynamicIndexCreator(outputPath.toFile(), iba);
indexFile = Tribble.indexFile(outputFileName);
idxCreator = new DynamicIndexCreator(outputPath, iba);
indexPath = Tribble.indexPath(outputPath);
}

return maybeAsync(new IndexingFeatureWriter<>(encoder, outputStream, idxCreator, indexFile, seqDict));
return maybeAsync(new IndexingFeatureWriter<>(encoder, outputStream, idxCreator, indexPath, seqDict));

} catch (final IOException e) {
throw new TribbleException("Unable to create writer for " + outputFileName, e);
Expand Down
23 changes: 5 additions & 18 deletions src/main/java/htsjdk/tribble/writer/IndexingFeatureWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* Feature writer class for indexing on the fly.
Expand All @@ -51,7 +52,7 @@ final class IndexingFeatureWriter<F extends Feature> extends FeatureWriterImpl<F

private final LocationAware location;
private final IndexCreator indexer;
private final String indexPath;
private final Path indexPath;
private final SAMSequenceDictionary refDict;

/**
Expand All @@ -60,11 +61,11 @@ final class IndexingFeatureWriter<F extends Feature> extends FeatureWriterImpl<F
* @param encoder encoder for features.
* @param outputStream the underlying output stream.
* @param idxCreator indexer.
* @param indexPath the file to write the index.
* @param indexPath the path to write the index on.
* @param refDict dictionary to write in the index. May be {@code null}.
*/
public IndexingFeatureWriter(final FeatureEncoder<F> encoder, final OutputStream outputStream,
final IndexCreator idxCreator, final String indexPath,
final IndexCreator idxCreator, final Path indexPath,
final SAMSequenceDictionary refDict) {
super(encoder, asLocationAwareStream(outputStream));
this.indexer = idxCreator;
Expand Down Expand Up @@ -98,21 +99,7 @@ public void close() throws IOException {
indexer.setIndexSequenceDictionary(refDict);
}
final Index index = indexer.finalizeIndex(location.getPosition());
// TODO: uncomment next line after https://github.com/samtools/htsjdk/pull/810
// index.write(indexPath);
writeIndex(index);
index.write(indexPath);
super.close();
}

// TODO: remove comment and usage after https://github.com/samtools/htsjdk/pull/810
private final void writeIndex(final Index index) throws IOException {
OutputStream outputStream = Files.newOutputStream(IOUtil.getPath(indexPath));
if (index instanceof TabixIndex) {
outputStream = new BlockCompressedOutputStream(outputStream, null);
} else {
outputStream = new BufferedOutputStream(outputStream);
}
index.write(new LittleEndianOutputStream(outputStream));
outputStream.close();
}
}

0 comments on commit 4c67b5e

Please sign in to comment.