Search in sources :

Example 96 with DynamicEntity

use of org.molgenis.data.support.DynamicEntity in project molgenis by molgenis.

the class ExcelWriterTest method addCellProcessor_data.

@Test
public void addCellProcessor_data() throws IOException {
    CellProcessor processor = when(mock(CellProcessor.class).processData()).thenReturn(true).getMock();
    OutputStream os = mock(OutputStream.class);
    ExcelWriter excelWriter = new ExcelWriter(os, attrMetaFactory);
    excelWriter.addCellProcessor(processor);
    try {
        Entity entity = new DynamicEntity(mock(EntityType.class)) {

            @Override
            protected void validateValueType(String attrName, Object value) {
            // noop
            }
        };
        entity.set("col1", "val1");
        entity.set("col2", "val2");
        Writable writable = excelWriter.createWritable("test", Arrays.asList("col1", "col2"));
        writable.add(entity);
    } finally {
        excelWriter.close();
    }
    verify(processor).process("val1");
    verify(processor).process("val2");
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) DynamicEntity(org.molgenis.data.support.DynamicEntity) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CellProcessor(org.molgenis.data.file.processor.CellProcessor) Writable(org.molgenis.data.Writable) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 97 with DynamicEntity

use of org.molgenis.data.support.DynamicEntity in project molgenis by molgenis.

the class VcfToEntity method createSampleEntities.

private List<Entity> createSampleEntities(VcfRecord vcfRecord, String entityPosAlt, String entityId) {
    List<Entity> samples = new ArrayList<>();
    Iterator<VcfSample> sampleIterator = vcfRecord.getSamples().iterator();
    if (vcfRecord.getNrSamples() > 0) {
        Iterator<String> sampleNameIterator = vcfMeta.getSampleNames().iterator();
        for (int j = 0; sampleIterator.hasNext(); ++j) {
            String[] format = vcfRecord.getFormat();
            VcfSample sample = sampleIterator.next();
            Entity sampleEntity = new DynamicEntity(sampleEntityType);
            for (int i = 0; i < format.length; i = i + 1) {
                String strValue = sample.getData(i);
                Object value = null;
                EntityType sampleEntityType = sampleEntity.getEntityType();
                Attribute attr = sampleEntityType.getAttribute(format[i]);
                if (attr != null) {
                    if (strValue != null) {
                        value = getTypedValue(strValue, attr);
                    }
                } else {
                    if (Arrays.equals(EMPTY_FORMAT, format)) {
                        LOG.debug("Found a dot as format, assuming no samples present");
                    } else {
                        throw new MolgenisDataException("Sample entity contains an attribute [" + format[i] + "] which is not specified in vcf headers");
                    }
                }
                sampleEntity.set(format[i], value);
            }
            sampleEntity.set(ID, entityId + j);
            // FIXME remove entity ID from Sample label after #1400 is fixed, see also:
            // jquery.molgenis.table.js line 152
            String original_name = sampleNameIterator.next();
            sampleEntity.set(NAME, entityPosAlt + "_" + original_name);
            sampleEntity.set(ORIGINAL_NAME, original_name);
            samples.add(sampleEntity);
        }
    }
    return samples;
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) DynamicEntity(org.molgenis.data.support.DynamicEntity) Attribute(org.molgenis.data.meta.model.Attribute) EntityType(org.molgenis.data.meta.model.EntityType) MolgenisDataException(org.molgenis.data.MolgenisDataException) VcfSample(org.molgenis.vcf.VcfSample)

Example 98 with DynamicEntity

use of org.molgenis.data.support.DynamicEntity in project molgenis by molgenis.

the class VcfToEntity method toEntity.

public Entity toEntity(VcfRecord vcfRecord) {
    Entity entity = new DynamicEntity(entityType);
    entity.set(CHROM, vcfRecord.getChromosome());
    entity.set(ALT, StringUtils.join(Lists.transform(vcfRecord.getAlternateAlleles(), Allele::toString), ','));
    entity.set(POS, vcfRecord.getPosition());
    entity.set(REF, vcfRecord.getReferenceAllele().toString());
    entity.set(FILTER, vcfRecord.getFilterStatus());
    entity.set(QUAL, vcfRecord.getQuality());
    entity.set(ID, StringUtils.join(vcfRecord.getIdentifiers(), ','));
    String id = VcfUtils.createId(entity);
    entity.set(INTERNAL_ID, id);
    writeInfoFieldsToEntity(vcfRecord, entity);
    if (sampleEntityType != null) {
        List<Entity> samples = createSampleEntities(vcfRecord, entity.get(POS) + "_" + entity.get(ALT), id);
        entity.set(SAMPLES, samples);
    }
    return entity;
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) Allele(org.molgenis.genotype.Allele) DynamicEntity(org.molgenis.data.support.DynamicEntity)

Example 99 with DynamicEntity

use of org.molgenis.data.support.DynamicEntity 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)

Example 100 with DynamicEntity

use of org.molgenis.data.support.DynamicEntity in project molgenis by molgenis.

the class VcfWriterUtilsIT method vcfWriterAnnotateTest.

@Test
public void vcfWriterAnnotateTest() throws IOException, MolgenisInvalidFormatException {
    entity1.set("ANNO", "TEST_test21");
    entity2.set("ANNO", "TEST_test22");
    final File outputVCFFile = File.createTempFile("output", ".vcf");
    try {
        BufferedWriter outputVCFWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputVCFFile), UTF_8));
        File inputVcfFile = new File(ResourceUtils.getFile(getClass(), "/testWriter.vcf").getPath());
        File resultVCFWriter = new File(ResourceUtils.getFile(getClass(), "/result_vcfWriter.vcf").getPath());
        VcfWriterUtils.writeVcfHeader(inputVcfFile, outputVCFWriter, newArrayList(Collections.singletonList(attributeFactory.create().setName("ANNO"))));
        for (Entity entity : entities) {
            Entity mapEntity = new DynamicEntity(annotatedEntityType);
            mapEntity.set(entity);
            writeToVcf(mapEntity, new ArrayList<>(), new ArrayList<>(), outputVCFWriter);
            outputVCFWriter.newLine();
        }
        outputVCFWriter.close();
        assertTrue(FileUtils.contentEqualsIgnoreEOL(resultVCFWriter, outputVCFFile, "UTF8"));
    } finally {
        boolean outputVCFFileIsDeleted = outputVCFFile.delete();
        LOG.info("Result test file named: " + outputVCFFile.getName() + " is " + (outputVCFFileIsDeleted ? "" : "not ") + "deleted");
    }
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) DynamicEntity(org.molgenis.data.support.DynamicEntity) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Aggregations

DynamicEntity (org.molgenis.data.support.DynamicEntity)161 Entity (org.molgenis.data.Entity)123 Test (org.testng.annotations.Test)104 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)50 EntityType (org.molgenis.data.meta.model.EntityType)48 Attribute (org.molgenis.data.meta.model.Attribute)38 AttributeMapping (org.molgenis.semanticmapper.mapping.model.AttributeMapping)14 BeforeMethod (org.testng.annotations.BeforeMethod)14 ExplainedAttribute (org.molgenis.semanticsearch.explain.bean.ExplainedAttribute)7 EntityMapping (org.molgenis.semanticmapper.mapping.model.EntityMapping)5 BeforeClass (org.testng.annotations.BeforeClass)5 ArrayList (java.util.ArrayList)4 MolgenisDataException (org.molgenis.data.MolgenisDataException)4 EntityWithComputedAttributes (org.molgenis.data.support.EntityWithComputedAttributes)4 ExplainedQueryString (org.molgenis.semanticsearch.explain.bean.ExplainedQueryString)4 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)4 StringWriter (java.io.StringWriter)3 List (java.util.List)3 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)3 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)3