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");
}
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;
}
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;
}
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));
}
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");
}
}
Aggregations