Skip to content

Commit

Permalink
Updates after rebase on new content type strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmnbroad committed Aug 19, 2024
1 parent 7910a15 commit f29a23a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 56 deletions.
53 changes: 24 additions & 29 deletions src/main/java/htsjdk/beta/plugin/variants/VariantsBundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
/**
* A {@link Bundle} for variants and variants-related resources that are backed by on disk files. A {@link
* htsjdk.beta.plugin.variants.VariantsBundle} has a primary resource with content type {@link
* BundleResourceType#VARIANT_CONTEXTS}; and an optional index resource. A VariantsBundle can also contain
* additional resources.
* BundleResourceType#PRIMARY_CT_VARIANT_CONTEXTS}; and an optional index resource. A VariantsBundle can also
* contain additional resources.
*
* Note that this class is merely a convenience class for the case where the variants are backed by files on disk.
* A bundle that contains variants and related resources can be created manually using the {@link Bundle} class.
Expand All @@ -37,10 +37,7 @@ public class VariantsBundle extends Bundle implements Serializable {
* @param vcfPath An {@link IOPath}-derived object that represents a source of variants.
*/
public VariantsBundle(final IOPath vcfPath) {
this(List.of(
new IOPathResource(
ValidationUtils.nonNull(vcfPath, "IOPath must not be null"),
BundleResourceType.VARIANT_CONTEXTS)));
this(List.of(toInputResource(BundleResourceType.CT_VARIANT_CONTEXTS, vcfPath)));
}

/**
Expand All @@ -51,45 +48,43 @@ public VariantsBundle(final IOPath vcfPath) {
*/
public VariantsBundle(final IOPath vcfPath, final IOPath indexPath) {
this(List.of(
new IOPathResource(
ValidationUtils.nonNull(vcfPath, "IOPath must not be null"),
BundleResourceType.VARIANT_CONTEXTS),
new IOPathResource(ValidationUtils.nonNull(
indexPath, "IOPath must not be null"),
BundleResourceType.VARIANTS_INDEX)));
toInputResource(BundleResourceType.CT_VARIANT_CONTEXTS, vcfPath),
toInputResource(BundleResourceType.CT_VARIANTS_INDEX, indexPath)));
}

/**
* Create a {@link htsjdk.beta.plugin.variants.VariantsBundle} using the resources in an existing bundle. A
* resource with content type {@link BundleResourceType#VARIANTS_VCF} must be present in the resources, or
* this constructor will throw.
* resource with content type {@link BundleResourceType#CT_VARIANT_CONTEXTS} must be present in the
* resources, or this constructor will throw.
*
* @param resources collection of {@link BundleResource}. the collection must include a resource with
* content type {@link BundleResourceType#VARIANTS_VCF}.
* @throws IllegalArgumentException if no resource with content type {@link BundleResourceType#VARIANTS_VCF} is
* included in the input {@link BundleResource} collection
* content type {@link BundleResourceType#CT_VARIANT_CONTEXTS}.
* @throws IllegalArgumentException if no resource with content type
* {@link BundleResourceType#CT_VARIANT_CONTEXTS} is included in the input {@link BundleResource}
* collection.
*/
public VariantsBundle(final Collection<BundleResource> resources) {
super(BundleResourceType.VARIANT_CONTEXTS, resources);
super(BundleResourceType.CT_VARIANT_CONTEXTS, resources);
}

/**
* return the {@link BundleResourceType#VARIANTS_VCF} {@link BundleResource} for this {@link htsjdk.beta.plugin.variants.VariantsBundle}
*
* @return the {@link BundleResourceType#VARIANTS_VCF} {@link BundleResource} for this {@link htsjdk.beta.plugin.variants.VariantsBundle}
* @return the {@link BundleResourceType#CT_VARIANT_CONTEXTS} {@link BundleResource} for this
* {@link htsjdk.beta.plugin.variants.VariantsBundle}
*/
public BundleResource getVariants() {
return getOrThrow(BundleResourceType.VARIANT_CONTEXTS);
return getOrThrow(BundleResourceType.CT_VARIANT_CONTEXTS);
}

/**
* Get the optional {@link BundleResourceType#VARIANTS_INDEX} resource for this {@link htsjdk.beta.plugin.variants.VariantsBundle}.
* Get the optional {@link BundleResourceType#CT_VARIANTS_INDEX} resource for this
* {@link htsjdk.beta.plugin.variants.VariantsBundle}.
*
* @return the optional {@link BundleResourceType#VARIANTS_INDEX} resrouce for this {@link htsjdk.beta.plugin.variants.VariantsBundle},
* or Optional.empty() if no index resource is present in the bundle.
* @return the optional {@link BundleResourceType#CT_VARIANTS_INDEX} resource for this
* {@link htsjdk.beta.plugin.variants.VariantsBundle}, or Optional.empty() if no index resource is present in
* the bundle.
*/
public Optional<BundleResource> getIndex() {
return get(BundleResourceType.VARIANTS_INDEX);
return get(BundleResourceType.CT_VARIANTS_INDEX);
}

/**
Expand Down Expand Up @@ -210,10 +205,10 @@ private static <T extends IOPath> Optional<Tuple<String, String>> getInferredCon
if (extension.isPresent()) {
final String ext = extension.get();
if (ext.equals(FileExtensions.VCF)) {
return Optional.of(new Tuple<>(BundleResourceType.VARIANT_CONTEXTS, "VCF"));
return Optional.of(new Tuple<>(BundleResourceType.CT_VARIANT_CONTEXTS, BundleResourceType.FMT_VARIANTS_VCF));
} else if (ext.equals(FileExtensions.COMPRESSED_VCF)) {
return Optional.of(new Tuple<>(BundleResourceType.VARIANT_CONTEXTSS, "VCF"));
} //TODO...finish this
return Optional.of(new Tuple<>(BundleResourceType.CT_VARIANT_CONTEXTS, BundleResourceType.FMT_VARIANTS_VCF));
}
}
return Optional.empty();
}
Expand Down
1 change: 1 addition & 0 deletions src/test/java/htsjdk/beta/io/bundle/BundleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;

// Example JSON :
Expand Down
66 changes: 39 additions & 27 deletions src/test/java/htsjdk/beta/plugin/variants/VariantsBundleTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package htsjdk.beta.plugin.variants;

import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import htsjdk.HtsjdkTest;
import htsjdk.beta.io.IOPathUtils;
import htsjdk.beta.io.bundle.*;
import htsjdk.io.HtsPath;
import htsjdk.io.IOPath;
import htsjdk.tribble.TestUtils;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -48,12 +45,17 @@ public void testVariantsBundleVCFWithIndex() {
public void testNoVCFInSerializedBundle() {
final String vcfJSON = """
{
"schemaVersion":"0.1.0",
"schemaVersion":"%s",
"schemaName":"htsbundle",
"ALIGNED_READS":{"path":"my.cram","format":"READS_CRAM"},
"primary":"ALIGNED_READS"
"%s":{"path":"my.cram","format":"%s"},
"primary":"%s"
}
""".formatted();
""".formatted(
BundleJSON.JSON_SCHEMA_VERSION,
BundleResourceType.CT_ALIGNED_READS,
BundleResourceType.FMT_READS_CRAM,
BundleResourceType.CT_ALIGNED_READS
);
try {
VariantsBundle.getVariantsBundleFromString(vcfJSON);
} catch (final IllegalArgumentException e) {
Expand All @@ -80,56 +82,66 @@ public Object[][] getRoundTripJSONTestData() {
"""
{
"schemaName":"htsbundle",
"schemaVersion":"0.1.0",
"VARIANT_CONTEXTS":{"path":"%s"},
"primary":"VARIANT_CONTEXTS"
"schemaVersion":"%s",
"%s":{"path":"%s"},
"primary":"%s"
}
""".formatted(VCF_FILE),
""".formatted(
BundleJSON.JSON_SCHEMA_VERSION,
BundleResourceType.CT_VARIANT_CONTEXTS,
VCF_FILE,
BundleResourceType.CT_VARIANT_CONTEXTS),
new VariantsBundle(new HtsPath(VCF_FILE))
},
{
// vcf only, with format included
"""
{
"schemaVersion":"0.1.0",
"schemaVersion":"%s",
"schemaName":"htsbundle",
"VARIANT_CONTEXTS":{"path":"%s","format":"VCF"},
"primary":"VARIANT_CONTEXTS"
"%s":{"path":"%s","format":"%s"},
"primary":"%s"
}
""".formatted(VCF_FILE),
""".formatted(
BundleJSON.JSON_SCHEMA_VERSION,
BundleResourceType.CT_VARIANT_CONTEXTS, VCF_FILE, BundleResourceType.FMT_VARIANTS_VCF,
BundleResourceType.CT_VARIANT_CONTEXTS),
// VariantsBundle doesn't automatically infer format, so create one manually
new VariantsBundle(
new BundleBuilder().addPrimary(
new IOPathResource(
new HtsPath(VCF_FILE),
BundleResourceType.VARIANT_CONTEXTS,
BundleResourceType.VARIANTS_VCF))
BundleResourceType.CT_VARIANT_CONTEXTS,
BundleResourceType.FMT_VARIANTS_VCF))
.build().getResources())
},
{
// vcf with an index, with format included
"""
{
"schemaVersion":"0.1.0",
"schemaVersion":"%s",
"schemaName":"htsbundle",
"VARIANT_CONTEXTS":{"path":"%s","format":"VCF"},
"VARIANTS_INDEX":{"path":"%s","format":"IDX"},
"primary":"VARIANT_CONTEXTS"
"%s":{"path":"%s","format":"%s"},
"%s":{"path":"%s"},
"primary":"%s"
}
""".formatted(VCF_FILE, VCF_INDEX_FILE),
""".formatted(
BundleJSON.JSON_SCHEMA_VERSION,
BundleResourceType.CT_VARIANT_CONTEXTS, VCF_FILE, BundleResourceType.FMT_VARIANTS_VCF,
BundleResourceType.CT_VARIANTS_INDEX, VCF_INDEX_FILE,
BundleResourceType.CT_VARIANT_CONTEXTS),
// VariantsBundle doesn't automatically infer format, so create one manually
new VariantsBundle(
new BundleBuilder()
.addPrimary(
new IOPathResource(
new HtsPath(VCF_FILE),
BundleResourceType.VARIANT_CONTEXTS,
BundleResourceType.VARIANTS_VCF))
BundleResourceType.CT_VARIANT_CONTEXTS,
BundleResourceType.FMT_VARIANTS_VCF))
.addSecondary(
new IOPathResource(
new HtsPath(VCF_INDEX_FILE),
BundleResourceType.VARIANTS_INDEX,
"IDX"))
BundleResourceType.CT_VARIANTS_INDEX))
.build().getResources())
},
};
Expand All @@ -151,7 +163,7 @@ public void testGetVariantsBundleFromPath(
IOPathUtils.writeStringToPath(jsonFilePath, jsonString);
final VariantsBundle bundleFromPath = VariantsBundle.getVariantsBundleFromPath(jsonFilePath);

Assert.assertTrue(BundleTest.equalsIgnoreOrder(bundleFromPath, expectedVariantsBundle));
Assert.assertTrue(Bundle.equalsIgnoreOrder(bundleFromPath, expectedVariantsBundle));
Assert.assertTrue(bundleFromPath.getVariants().getIOPath().isPresent());
Assert.assertEquals(bundleFromPath.getVariants().getIOPath().get(), expectedVariantsBundle.getVariants().getIOPath().get());
}
Expand Down

0 comments on commit f29a23a

Please sign in to comment.