Skip to content

Commit

Permalink
Replace toPath().toString() with getURIString() (#1719)
Browse files Browse the repository at this point in the history
Replace toPath().toString() with toUriString() this should prevent the scheme from being cutoff unintentionally in beta VCFDecoder. Refactor optional handling for clarity
  • Loading branch information
lbergelson authored Oct 7, 2024
1 parent 4c1c766 commit 1cb5f07
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
22 changes: 9 additions & 13 deletions src/main/java/htsjdk/beta/codecs/variants/vcf/VCFDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import htsjdk.variant.vcf.VCFHeader;

import java.io.IOException;
import java.nio.channels.SeekableByteChannel;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

/**
* InternalAPII
Expand Down Expand Up @@ -197,7 +199,7 @@ private static FeatureReader<VariantContext> getVCFReader(
"The provided %s resource (%s) must be a readable/input resource",
BundleResourceType.CT_VARIANT_CONTEXTS,
variantsResource));
} else if (!variantsResource.getIOPath().isPresent()) {
} else if (variantsResource.getIOPath().isEmpty()) {
throw new HtsjdkUnsupportedOperationException("VCF reader from stream not implemented");
}
final IOPath variantsIOPath = variantsResource.getIOPath().get();
Expand All @@ -206,25 +208,19 @@ private static FeatureReader<VariantContext> getVCFReader(
//TODO: this resolves the index automatically. it should check to make sure the provided index
// matches the one that is automatically resolved, otherwise throw since the request will not be honored
return AbstractFeatureReader.getFeatureReader(
variantsIOPath.toPath().toString(),
indexIOPath.isPresent() ?
indexIOPath.get().toPath().toString() :
null,
variantsIOPath.getURIString(),
indexIOPath.map(IOPath::getURIString).orElse(null),
vcfCodec,
indexIOPath.isPresent(),
decoderOptions.getVariantsChannelTransformer().isPresent() ?
decoderOptions.getVariantsChannelTransformer().get() :
null,
decoderOptions.getIndexChannelTransformer().isPresent() ?
decoderOptions.getIndexChannelTransformer().get() :
null
decoderOptions.getVariantsChannelTransformer().orElse(null),
decoderOptions.getIndexChannelTransformer().orElse(null)
);
}

// the underlying readers can't handle index streams, so for now we can only handle IOPaths
private static Optional<IOPath> getIndexIOPath(final Bundle inputBundle) {
final Optional<BundleResource> optIndexResource = inputBundle.get(BundleResourceType.CT_VARIANTS_INDEX);
if (!optIndexResource.isPresent()) {
if (optIndexResource.isEmpty()) {
return Optional.empty();
}
final BundleResource indexResource = optIndexResource.get();
Expand All @@ -234,7 +230,7 @@ private static Optional<IOPath> getIndexIOPath(final Bundle inputBundle) {
BundleResourceType.CT_VARIANTS_INDEX,
indexResource));
}
if (!indexResource.getIOPath().isPresent()) {
if (indexResource.getIOPath().isEmpty()) {
throw new HtsjdkUnsupportedOperationException("Reading a VCF index from a stream not implemented");
}
return indexResource.getIOPath();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package htsjdk.beta.codecs.variants.vcf;

import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import com.google.common.jimfs.SystemJimfsFileSystemProvider;
import htsjdk.HtsjdkTest;
import htsjdk.beta.codecs.variants.vcf.vcfv3_2.VCFCodecV3_2;
import htsjdk.beta.codecs.variants.vcf.vcfv3_3.VCFCodecV3_3;
Expand All @@ -26,6 +29,7 @@
import htsjdk.beta.plugin.variants.VariantsEncoder;
import htsjdk.beta.plugin.variants.VariantsFormats;
import htsjdk.samtools.util.IOUtil;
import htsjdk.tribble.TestUtils;
import htsjdk.variant.variantcontext.VariantContext;
import htsjdk.variant.vcf.VCFHeader;
import org.testng.Assert;
Expand All @@ -36,6 +40,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.function.Function;
Expand All @@ -60,11 +67,26 @@ private Object[][] vcfReadWriteTests() {
};
}

@Test
public void testRoundTripNio() throws IOException {
try(FileSystem fs = Jimfs.newFileSystem("test", Configuration.unix())){
final IOPath outputPath = IOUtils.createTempPath("roundTripVCFThroughPath", ".vcf");
Path tribbleFileInJimfs = TestUtils.getTribbleFileInJimfs(TEST_VCF_WITH_INDEX.getRawInputString(), TEST_VCF_INDEX.getRawInputString(), fs);
HtsPath vcf = new HtsPath(tribbleFileInJimfs.toUri().toString());
checkCodecAndReadWriteVcf( vcf , VCFCodecV4_0.VCF_V40_VERSION , outputPath);
}
}

@Test(dataProvider = "vcfReadWriteTests")
public void testRoundTripVCFThroughPath(final IOPath inputPath, final HtsVersion expectedCodecVersion) {
final IOPath outputPath = IOUtils.createTempPath("roundTripVCFThroughPath", ".vcf");

checkCodecAndReadWriteVcf(inputPath, expectedCodecVersion, outputPath);
}

private void checkCodecAndReadWriteVcf(IOPath inputPath, HtsVersion expectedCodecVersion, IOPath outputPath) {
// some test files require "AllowMissingFields" options for writing
final VariantsEncoderOptions variantsEncoderOptions = new VariantsEncoderOptions().setAllowFieldsMissingFromHeader(true);
final IOPath outputPath = IOUtils.createTempPath("roundTripVCFThroughPath", ".vcf");

try (final VariantsDecoder variantsDecoder = HtsDefaultRegistry.getVariantsResolver().getVariantsDecoder(inputPath);
final VariantsEncoder variantsEncoder = HtsDefaultRegistry.getVariantsResolver().getVariantsEncoder(
Expand Down

0 comments on commit 1cb5f07

Please sign in to comment.