Search in sources :

Example 1 with VcfRecord

use of org.molgenis.vcf.VcfRecord in project molgenis by molgenis.

the class VcfToEntityTest method testToEntityMissingValues.

// https://github.com/molgenis/molgenis/issues/5329
@Test
public void testToEntityMissingValues() throws IOException {
    String vcfHeaders = "##fileformat=VCFv4.1\n" + "##INFO=<ID=GoNL_AF,Number=.,Type=Float,Description=\"The allele frequency for variants seen in the population used for the GoNL project\">\n" + "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\n";
    VcfMeta vcfMeta = parseHeaders(vcfHeaders);
    VcfToEntity vcfToEntity = new VcfToEntity("entityTypeName", vcfMeta, vcfAttrs, entityTypeFactory, attrMetaFactory);
    VcfRecord record = new VcfRecord(vcfMeta, new String[] { "1", "54728", ".", "G", "T,C", ".", ".", "GoNL_AF=0.01,." });
    Entity entity = vcfToEntity.toEntity(record);
    assertEquals(entity.getString("GoNL_AF"), "0.01,.");
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) VcfRecord(org.molgenis.vcf.VcfRecord) VcfMeta(org.molgenis.vcf.meta.VcfMeta) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 2 with VcfRecord

use of org.molgenis.vcf.VcfRecord in project molgenis by molgenis.

the class VcfToEntityTest method testToEntityAlternativeAlleles.

@Test
public void testToEntityAlternativeAlleles() throws IOException {
    VcfRecord record = new VcfRecord(vcfMetaSmall, new String[] { "10", "12345", "id3", "A", "A,C,G,T,N,*", "7.9123", "pass", "DF;DF2;CHAR=-" });
    Entity entity = vcfToEntitySmall.toEntity(record);
    Entity expected = new DynamicEntity(vcfToEntitySmall.getEntityType());
    expected.set("#CHROM", "10");
    expected.set("ALT", "A,C,G,T,N,*");
    expected.set("POS", 12345);
    expected.set("REF", "A");
    expected.set("FILTER", "pass");
    expected.set("QUAL", "7.9123");
    expected.set("ID", "id3");
    expected.set("INTERNAL_ID", entity.get("INTERNAL_ID"));
    expected.set("DF", true);
    expected.set("DF2", true);
    expected.set("CHAR", "-");
    assertTrue(EntityUtils.equals(entity, expected));
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) VcfRecord(org.molgenis.vcf.VcfRecord) DynamicEntity(org.molgenis.data.support.DynamicEntity) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 3 with VcfRecord

use of org.molgenis.vcf.VcfRecord in project molgenis by molgenis.

the class VcfRepository method iterator.

/**
 * Returns an iterator for this repository.
 * <p>
 * Use with caution! Multiple iterators will all point to the same line in the VCF file, leading to unpredictable
 * behaviour. If you want to get the EntityType of this repository and you can't access getEntityType(),
 * convert the iterator to a PeekingIterator and peek the first Entity.
 */
@Override
public Iterator<Entity> iterator() {
    Iterator<VcfRecord> vcfRecordIterator = Iterators.unmodifiableIterator(vcfReaderFactory.get().iterator());
    VcfToEntity vcfToEntity = vcfToEntitySupplier.get();
    return Iterators.transform(vcfRecordIterator, vcfToEntity::toEntity);
}
Also used : VcfRecord(org.molgenis.vcf.VcfRecord) VcfToEntity(org.molgenis.data.vcf.format.VcfToEntity)

Example 4 with VcfRecord

use of org.molgenis.vcf.VcfRecord in project molgenis by molgenis.

the class VcfToEntity method writeInfoFieldsToEntity.

private void writeInfoFieldsToEntity(VcfRecord vcfRecord, Entity entity) {
    // have to contain all flag fields.
    for (String vcfInfoFlagFieldKey : vcfInfoFlagFieldKeys) {
        entity.set(toAttributeName(vcfInfoFlagFieldKey), false);
    }
    for (VcfInfo vcfInfo : vcfRecord.getInformation()) {
        if (// value not available
        vcfInfo.getKey().equals(".")) {
            continue;
        }
        Object val;
        if (vcfInfoFlagFieldKeys.contains(vcfInfo.getKey())) {
            val = true;
        } else {
            Object vcfInfoVal = vcfInfo.getVal();
            if (vcfInfoVal == null) {
                val = null;
            } else if (vcfInfoVal instanceof List<?>) {
                List<?> vcfInfoValTokens = (List<?>) vcfInfoVal;
                // TODO Use list data type once available (see http://www.molgenis.org/ticket/2681)
                val = vcfInfoValTokens.stream().map(vcfInfoValToken -> vcfInfoValToken != null ? vcfInfoValToken.toString() : ".").collect(joining(","));
            } else if (vcfInfoVal instanceof Float) {
                if (Float.isNaN((Float) vcfInfoVal)) {
                    val = null;
                } else {
                    val = new BigDecimal(String.valueOf(vcfInfoVal)).doubleValue();
                }
            } else if (vcfInfoVal instanceof Character) {
                val = vcfInfoVal.toString();
            } else {
                // VCF value type matches type expected for this MOLGENIS attribute type
                val = vcfInfoVal;
            }
        }
        entity.set(toAttributeName(vcfInfo.getKey()), val);
    }
}
Also used : VcfInfo(org.molgenis.vcf.VcfInfo) java.util(java.util) NameValidator(org.molgenis.data.validation.meta.NameValidator) LoggerFactory(org.slf4j.LoggerFactory) VcfMetaFormat(org.molgenis.vcf.meta.VcfMetaFormat) GenotypeDataException(org.molgenis.genotype.GenotypeDataException) StringUtils(org.apache.commons.lang3.StringUtils) Attribute(org.molgenis.data.meta.model.Attribute) BigDecimal(java.math.BigDecimal) Iterables.size(com.google.common.collect.Iterables.size) Lists(com.google.common.collect.Lists) VcfAttributes(org.molgenis.data.vcf.model.VcfAttributes) NAME(org.molgenis.data.vcf.VcfRepository.NAME) DynamicEntity(org.molgenis.data.support.DynamicEntity) EntityUtils.getTypedValue(org.molgenis.data.util.EntityUtils.getTypedValue) VcfMetaInfo(org.molgenis.vcf.meta.VcfMetaInfo) Objects.requireNonNull(java.util.Objects.requireNonNull) EntityTypeFactory(org.molgenis.data.meta.model.EntityTypeFactory) VcfMeta(org.molgenis.vcf.meta.VcfMeta) VcfRepository(org.molgenis.data.vcf.VcfRepository) VcfInfo(org.molgenis.vcf.VcfInfo) Collectors.toSet(java.util.stream.Collectors.toSet) AttributeType(org.molgenis.data.meta.AttributeType) AttributeFactory(org.molgenis.data.meta.model.AttributeFactory) Logger(org.slf4j.Logger) VcfRecord(org.molgenis.vcf.VcfRecord) EntityType(org.molgenis.data.meta.model.EntityType) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) VcfSample(org.molgenis.vcf.VcfSample) Allele(org.molgenis.genotype.Allele) StreamSupport.stream(java.util.stream.StreamSupport.stream) VcfUtils(org.molgenis.data.vcf.utils.VcfUtils) Maps.newHashMapWithExpectedSize(com.google.common.collect.Maps.newHashMapWithExpectedSize) AttributeRole(org.molgenis.data.meta.model.EntityType.AttributeRole) MolgenisDataException(org.molgenis.data.MolgenisDataException) ORIGINAL_NAME(org.molgenis.data.vcf.VcfRepository.ORIGINAL_NAME) Entity(org.molgenis.data.Entity) BigDecimal(java.math.BigDecimal)

Example 5 with VcfRecord

use of org.molgenis.vcf.VcfRecord in project molgenis by molgenis.

the class VcfToEntityTest method testToEntity.

@Test
public void testToEntity() throws IOException {
    VcfRecord record = new VcfRecord(vcfMetaSmall, new String[] { "10", "12345", "id3", "A", "C", "7.9123", "pass", "DF;;CHAR=-" });
    Entity entity = vcfToEntitySmall.toEntity(record);
    Entity expected = new DynamicEntity(vcfToEntitySmall.getEntityType());
    expected.set("#CHROM", "10");
    expected.set("ALT", "C");
    expected.set("POS", 12345);
    expected.set("REF", "A");
    expected.set("FILTER", "pass");
    expected.set("QUAL", "7.9123");
    expected.set("ID", "id3");
    expected.set("INTERNAL_ID", entity.get("INTERNAL_ID"));
    expected.set("DF", true);
    // Flag fields whose flag is not present are set to false
    expected.set("DF2", false);
    expected.set("CHAR", "-");
    assertTrue(EntityUtils.equals(entity, expected));
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) VcfRecord(org.molgenis.vcf.VcfRecord) DynamicEntity(org.molgenis.data.support.DynamicEntity) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Aggregations

VcfRecord (org.molgenis.vcf.VcfRecord)5 Entity (org.molgenis.data.Entity)4 DynamicEntity (org.molgenis.data.support.DynamicEntity)4 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)3 Test (org.testng.annotations.Test)3 VcfMeta (org.molgenis.vcf.meta.VcfMeta)2 Iterables.size (com.google.common.collect.Iterables.size)1 Lists (com.google.common.collect.Lists)1 Maps.newHashMapWithExpectedSize (com.google.common.collect.Maps.newHashMapWithExpectedSize)1 String.format (java.lang.String.format)1 BigDecimal (java.math.BigDecimal)1 java.util (java.util)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Collectors.joining (java.util.stream.Collectors.joining)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 StreamSupport.stream (java.util.stream.StreamSupport.stream)1 StringUtils (org.apache.commons.lang3.StringUtils)1 MolgenisDataException (org.molgenis.data.MolgenisDataException)1 AttributeType (org.molgenis.data.meta.AttributeType)1 Attribute (org.molgenis.data.meta.model.Attribute)1