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

Changes to FastaReferenceWriter #1285

Merged
merged 3 commits into from
Feb 18, 2019
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ test {
exclude "broken"
exclude "defaultReference"
exclude "ftp"
exclude "http"
exclude "sra"
exclude "ena"

Expand All @@ -127,11 +128,12 @@ task testFTP(type: Test) {
}

task testExternalApis(type: Test) {
description = "Run the SRA and ENA tests (tests that interact with external APIs)"
description = "Run the SRA, ENA, and HTTP tests (tests that interact with external APIs)"
jvmArgs += '-Dsamjdk.sra_libraries_download=true'

tags {
include "sra"
include "http"
include "ena"
exclude "slow"
exclude "broken"
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/htsjdk/samtools/SAMSequenceDictionaryCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
package htsjdk.samtools;

import htsjdk.samtools.util.LineReader;
import java.io.BufferedWriter;

import java.io.Writer;

/**
* "On the fly" codec SAMSequenceDictionaryCodec.
Expand Down Expand Up @@ -67,7 +68,7 @@ public class SAMSequenceDictionaryCodec {

private final SAMTextHeaderCodec codec;

public SAMSequenceDictionaryCodec(final BufferedWriter writer) {
public SAMSequenceDictionaryCodec(final Writer writer) {
codec = new SAMTextHeaderCodec();
codec.setmFileHeader(EMPTY_HEADER);
codec.setWriter(writer);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/htsjdk/samtools/SAMTextHeaderCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class SAMTextHeaderCodec {
private ValidationStringency validationStringency = ValidationStringency.SILENT;

// These attributes are populated when generating text
private BufferedWriter writer;
private Writer writer;

private static final String TAG_KEY_VALUE_SEPARATOR = ":";
private static final char TAG_KEY_VALUE_SEPARATOR_CHAR = ':';
Expand All @@ -73,7 +73,7 @@ public class SAMTextHeaderCodec {
public static final String COMMENT_PREFIX = HEADER_LINE_START + HeaderRecordType.CO.name() + FIELD_SEPARATOR;
private static final Log log = Log.getInstance(SAMTextHeaderCodec.class);

void setWriter(final BufferedWriter writer) {
void setWriter(final Writer writer) {
this.writer = writer;
}

Expand Down
18 changes: 10 additions & 8 deletions src/main/java/htsjdk/samtools/reference/FastaReferenceWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.*;

Expand Down Expand Up @@ -183,15 +184,14 @@ public final class FastaReferenceWriter implements AutoCloseable {
* @param dictOutput the (uncompressed) output stream to the dictFile, if requested, {@code null} if none should be generated.
* @throws IllegalArgumentException if {@code fastaFile} is {@code null} or {@code basesPerLine} is 0 or negative.
*/
protected FastaReferenceWriter(final int basesPerLine,
FastaReferenceWriter(final int basesPerLine,
final OutputStream fastaOutput,
final OutputStream indexOutput,
final OutputStream dictOutput) {
this.defaultBasePerLine = basesPerLine;
this.fastaStream = new CountingOutputStream(fastaOutput);
this.indexWriter = indexOutput == null ? NullWriter.NULL_WRITER : new OutputStreamWriter(indexOutput, CHARSET);
final BufferedWriter dictWriter = new BufferedWriter(dictOutput == null ? NullWriter.NULL_WRITER : new OutputStreamWriter(dictOutput, CHARSET));
this.dictWriter = dictWriter;
this.dictWriter = dictOutput == null ? NullWriter.NULL_WRITER : new OutputStreamWriter(dictOutput, CHARSET);
this.dictCodec = new SAMSequenceDictionaryCodec(dictWriter);
this.dictCodec.encodeHeaderLine(false);
}
Expand Down Expand Up @@ -441,7 +441,7 @@ private void writeDictEntry() {
*/
public FastaReferenceWriter appendBases(final String basesBases)
throws IOException {
return appendBases(basesBases.getBytes("ASCII"));
return appendBases(basesBases.getBytes(StandardCharsets.US_ASCII));
}

/**
Expand Down Expand Up @@ -592,20 +592,22 @@ private void assertIsNotClosed() {
* </p>
*
* @throws IOException if such exception is thrown when closing output writers and output streams.
* @throws IllegalStateException if closing without writing any sequences or closing when writing a sequence is in progress
*/
@Override
public void close() throws IOException {
try {
if (!closed) {
if (!closed) {
try {
closeSequence();
if (sequenceNames.isEmpty()) {
throw new IllegalStateException("no sequences were added to the reference");
}
} finally {
closed = true;
fastaStream.close();
indexWriter.close();
dictWriter.close();
}
} finally {
closed = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import htsjdk.utils.ValidationUtils;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
Expand Down Expand Up @@ -211,13 +212,13 @@ public FastaReferenceWriter build() throws IOException {
checkBasesPerLine(basesPerLine);

if (fastaFile != null) {
fastaOutput = Files.newOutputStream(fastaFile);
fastaOutput = new BufferedOutputStream(Files.newOutputStream(fastaFile));
}
if (indexFile != null) {
indexOutput = Files.newOutputStream(indexFile);
indexOutput = new BufferedOutputStream(Files.newOutputStream(indexFile));
}
if (dictFile != null) {
dictOutput = Files.newOutputStream(dictFile);
dictOutput = new BufferedOutputStream(Files.newOutputStream(dictFile));
}

return new FastaReferenceWriter(basesPerLine, fastaOutput, indexOutput, dictOutput);
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/htsjdk/samtools/util/HttpUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@

import htsjdk.HtsjdkTest;

@Test(groups = "http")
public class HttpUtilsTest extends HtsjdkTest {
@DataProvider(name = "existing_urls")
public Object[][] testExistingURLsData() {
return new Object[][]{
{"http://broadinstitute.github.io/picard/testdata/index_test.bam"},
{"http://ftp.1000genomes.ebi.ac.uk/vol1/ftp/current.tree"}
{"http://ftp.1000genomes.ebi.ac.uk/vol1/ftp/README_using_1000genomes_cram.md"}
};
}

Expand Down