private Map<String, Object> createEntityResponse(
     Entity entity, Fetch fetch, boolean includeMetaData) {
   Map<String, Object> responseData = new LinkedHashMap<String, Object>();
   if (includeMetaData) {
     createEntityMetaResponse(entity.getEntityMetaData(), fetch, responseData);
   }
   createEntityValuesResponse(entity, fetch, responseData);
   return responseData;
 }
Esempio n. 2
0
  @Test
  public void checkMrefValidWrongType() {
    AttributeMetaData refIdAttr =
        when(mock(AttributeMetaData.class).getName()).thenReturn("refId").getMock();
    when(refIdAttr.getDataType()).thenReturn(STRING);

    EntityMetaData refEntityMeta = mock(EntityMetaData.class);
    when(refEntityMeta.getName()).thenReturn("refEntity");
    when(refEntityMeta.getIdAttribute()).thenReturn(refIdAttr);
    when(refEntityMeta.getAtomicAttributes()).thenReturn(asList(refIdAttr));

    String idAttrName = "id";
    String mrefAttrName = "mref";
    AttributeMetaData idAttr =
        when(mock(AttributeMetaData.class).getName()).thenReturn(idAttrName).getMock();
    when(idAttr.getDataType()).thenReturn(STRING);
    AttributeMetaData mrefAttr =
        when(mock(AttributeMetaData.class).getName()).thenReturn(mrefAttrName).getMock();
    when(mrefAttr.getDataType()).thenReturn(MREF);
    when(mrefAttr.getRefEntity()).thenReturn(refEntityMeta);

    EntityMetaData entityMeta = mock(EntityMetaData.class);
    when(entityMeta.getName()).thenReturn("entity");
    when(entityMeta.getIdAttribute()).thenReturn(idAttr);
    when(entityMeta.getAtomicAttributes()).thenReturn(asList(idAttr, mrefAttr));

    AttributeMetaData otherRefIdAttr =
        when(mock(AttributeMetaData.class).getName()).thenReturn("otherRefId").getMock();
    when(otherRefIdAttr.getDataType()).thenReturn(STRING);

    EntityMetaData otherRefEntityMeta = mock(EntityMetaData.class);
    when(otherRefEntityMeta.getName()).thenReturn("otherRefEntity");
    when(otherRefEntityMeta.getIdAttribute()).thenReturn(refIdAttr);
    when(otherRefEntityMeta.getAtomicAttributes()).thenReturn(asList(otherRefIdAttr));

    Entity refEntity0 =
        when(mock(Entity.class).getEntityMetaData()).thenReturn(otherRefEntityMeta).getMock();
    when(refEntity0.getIdValue()).thenReturn("refId0");

    Entity refEntity1 =
        when(mock(Entity.class).getEntityMetaData()).thenReturn(otherRefEntityMeta).getMock();
    when(refEntity1.getIdValue()).thenReturn("refId1");

    Entity entity0 = when(mock(Entity.class).getEntityMetaData()).thenReturn(entityMeta).getMock();
    when(entity0.getIdValue()).thenReturn("id0");
    when(entity0.getEntities(mrefAttrName)).thenReturn(asList(refEntity0, refEntity1));

    Set<ConstraintViolation> constraints =
        entityAttributesValidator.validate(entity0, entity0.getEntityMetaData());
    assertEquals(constraints.size(), 1);
  }
  private void validateEntityValueReferences(Entity entity, ValidationResource validationResource) {
    validationResource
        .getRefAttrs()
        .forEach(
            refAttr -> {
              HugeSet<Object> refEntityIds =
                  validationResource.getRefEntitiesIds().get(refAttr.getRefEntity().getName());

              Iterable<Entity> refEntities;
              if (refAttr.getDataType() instanceof XrefField) {
                Entity refEntity = entity.getEntity(refAttr.getName());
                if (refEntity != null) {
                  refEntities = singleton(refEntity);
                } else {
                  refEntities = emptyList();
                }
              } else {
                refEntities = entity.getEntities(refAttr.getName());
              }

              for (Entity refEntity : refEntities) {
                if (!refEntityIds.contains(refEntity.getIdValue())) {
                  boolean selfReference =
                      entity.getEntityMetaData().getName().equals(refAttr.getRefEntity().getName());
                  if (!(selfReference && entity.getIdValue().equals(refEntity.getIdValue()))) {
                    String message =
                        String.format(
                            "Unknown xref value '%s' for attribute '%s' of entity '%s'.",
                            DataConverter.toString(refEntity.getIdValue()),
                            refAttr.getName(),
                            getName());

                    ConstraintViolation constraintViolation =
                        new ConstraintViolation(
                            message, refAttr, Long.valueOf(validationResource.getRow()));
                    validationResource.addViolation(constraintViolation);
                  }
                }
                validationResource.addRefEntityId(getName(), refEntity.getIdValue());
              }
            });
  }
Esempio n. 4
0
  /**
   * Convert an vcfEntity to a VCF line
   *
   * @param vcfEntity
   * @return
   * @throws Exception
   */
  public static String convertToVCF(Entity vcfEntity) throws MolgenisDataException {
    StringBuilder vcfRecord = new StringBuilder();

    List<String> vcfAttributes =
        Arrays.asList(
            new String[] {
              VcfRepository.CHROM,
              VcfRepository.POS,
              VcfRepository.ID,
              VcfRepository.REF,
              VcfRepository.ALT,
              VcfRepository.QUAL,
              VcfRepository.FILTER
            });

    // fixed attributes: chrom pos id ref alt qual filter
    for (String vcfAttribute : vcfAttributes) {
      vcfRecord.append(
          ((vcfEntity.getString(vcfAttribute) != null
                      && !vcfEntity.getString(vcfAttribute).equals(""))
                  ? vcfEntity.getString(vcfAttribute)
                  : ".")
              + TAB);
      // vcfRecord.append(vcfEntity.getString(vcfAttribute) + "\t");
    }

    List<String> infoFieldsSeen = new ArrayList<String>();
    // flexible 'info' field, one column with potentially many data items
    for (AttributeMetaData attributeMetaData :
        vcfEntity.getEntityMetaData().getAttribute(VcfRepository.INFO).getAttributeParts()) {
      infoFieldsSeen.add(attributeMetaData.getName());
      if (vcfEntity.getString(attributeMetaData.getName())
          != null) // FIXME: This removes 'FLAG' fields? see
      // http://samtools.github.io/hts-specs/VCFv4.2.pdf
      {
        if (attributeMetaData.getName().startsWith(VcfRepository.getInfoPrefix())) {
          vcfRecord.append(
              attributeMetaData.getName().substring(VcfRepository.getInfoPrefix().length())
                  + "="
                  + vcfEntity.getString(attributeMetaData.getName())
                  + ";");
        } else {
          vcfRecord.append(
              attributeMetaData.getName()
                  + "="
                  + vcfEntity.getString(attributeMetaData.getName())
                  + ";");
        }
      }
    }

    for (AttributeMetaData attributeMetaData :
        vcfEntity.getEntityMetaData().getAtomicAttributes()) {
      if (!infoFieldsSeen.contains(attributeMetaData.getName())
          && attributeMetaData.getName().startsWith(VcfRepository.getInfoPrefix())
          && vcfEntity.getString(attributeMetaData.getName()) != null) {
        vcfRecord.append(
            attributeMetaData.getName().substring(VcfRepository.getInfoPrefix().length())
                + "="
                + vcfEntity.getString(attributeMetaData.getName())
                + ";");
      }
    }

    // if we have SAMPLE data, add to output VCF
    Iterable<Entity> sampleEntities = vcfEntity.getEntities(VcfRepository.SAMPLES);
    if (sampleEntities != null && !Iterables.isEmpty(sampleEntities)) {
      // add tab
      vcfRecord.append(TAB);
      boolean firstSample = true;

      for (Entity sample : sampleEntities) {
        StringBuilder formatColumn = new StringBuilder();
        StringBuilder sampleColumn = new StringBuilder();

        for (String sampleAttribute : sample.getAttributeNames()) {
          // leave out autogenerated ID and NAME columns since this will greatly bloat the output
          // file for
          // many samples
          // FIXME: chance to clash with existing ID and NAME columns in FORMAT ?? what happens
          // then?
          if (!sampleAttribute.equals(VcfRepository.ID)
              && !sampleAttribute.equals(VcfRepository.NAME)) {
            if (sample.getString(sampleAttribute) != null) {
              sampleColumn.append(sample.getString(sampleAttribute));
              sampleColumn.append(":");
            } else {
              sampleColumn.append(".:");
            }

            // get FORMAT fields, but only for the first time
            if (firstSample) {
              formatColumn.append(sampleAttribute);
              formatColumn.append(":");
            }
          }
        }

        // add FORMAT data but only first time
        if (firstSample && formatColumn.length() > 0) // FIXME: do we expect this??
        {
          formatColumn.deleteCharAt(formatColumn.length() - 1); // delete trailing ':'
          vcfRecord.append(formatColumn.toString() + TAB);
          firstSample = false;
        } else if (firstSample) {
          throw new MolgenisDataException(
              "Weird situation: we are at sample 1 and want to print FORMAT info but there seems to be none?");
        }

        // now add SAMPLE data
        sampleColumn.deleteCharAt(sampleColumn.length() - 1); // delete trailing ':'
        vcfRecord.append(sampleColumn.toString() + TAB);
      }
      // after all samples, delete trailing '\t'
      vcfRecord.deleteCharAt(vcfRecord.length() - 1); // FIXME: need a check??
    }

    return vcfRecord.toString();
  }
 @Override
 public EntityMetaData getEntityMetaData() {
   return decoratedEntity.getEntityMetaData();
 }
 private void createEntityValuesResponseRec(
     Entity entity,
     Iterable<AttributeMetaData> attrs,
     Fetch fetch,
     Map<String, Object> responseData) {
   responseData.put(
       "_href",
       Href.concatEntityHref(BASE_URI, entity.getEntityMetaData().getName(), entity.getIdValue()));
   for (AttributeMetaData attr : attrs) // TODO performance use fetch instead of attrs
   {
     String attrName = attr.getName();
     if (fetch == null || fetch.hasField(attr)) {
       FieldTypeEnum dataType = attr.getDataType().getEnumType();
       switch (dataType) {
         case BOOL:
           responseData.put(attrName, entity.getBoolean(attrName));
           break;
         case CATEGORICAL:
         case XREF:
         case FILE:
           Entity refEntity = entity.getEntity(attrName);
           Map<String, Object> refEntityResponse;
           if (refEntity != null) {
             Fetch refAttrFetch = fetch != null ? fetch.getFetch(attr) : null;
             refEntityResponse = createEntityResponse(refEntity, refAttrFetch, false);
           } else {
             refEntityResponse = null;
           }
           responseData.put(attrName, refEntityResponse);
           break;
         case CATEGORICAL_MREF:
         case MREF:
           Iterable<Entity> refEntities = entity.getEntities(attrName);
           List<Map<String, Object>> refEntityResponses;
           if (refEntities != null) {
             refEntityResponses = new ArrayList<Map<String, Object>>();
             Fetch refAttrFetch = fetch != null ? fetch.getFetch(attrName) : null;
             for (Entity refEntitiesEntity : refEntities) {
               refEntityResponses.add(
                   createEntityResponse(refEntitiesEntity, refAttrFetch, false));
             }
           } else {
             refEntityResponses = null;
           }
           responseData.put(attrName, refEntityResponses);
           break;
         case COMPOUND:
           throw new RuntimeException("Invalid data type [" + dataType + "]");
         case DATE:
           Date dateValue = entity.getDate(attrName);
           String dateValueStr = dateValue != null ? getDateFormat().format(dateValue) : null;
           responseData.put(attrName, dateValueStr);
           break;
         case DATE_TIME:
           Date dateTimeValue = entity.getDate(attrName);
           String dateTimeValueStr =
               dateTimeValue != null ? getDateTimeFormat().format(dateTimeValue) : null;
           responseData.put(attrName, dateTimeValueStr);
           break;
         case DECIMAL:
           responseData.put(attrName, entity.getDouble(attrName));
           break;
         case EMAIL:
         case ENUM:
         case HTML:
         case HYPERLINK:
         case SCRIPT:
         case STRING:
         case TEXT:
           responseData.put(attrName, entity.getString(attrName));
           break;
         case INT:
           responseData.put(attrName, entity.getInt(attrName));
           break;
         case LONG:
           responseData.put(attrName, entity.getLong(attrName));
           break;
         default:
           throw new RuntimeException("Unknown data type [" + dataType + "]");
       }
     }
   }
 }
 private void createEntityValuesResponse(
     Entity entity, Fetch fetch, Map<String, Object> responseData) {
   Iterable<AttributeMetaData> attrs = entity.getEntityMetaData().getAtomicAttributes();
   createEntityValuesResponseRec(entity, attrs, fetch, responseData);
 }