variableList.add(new Variable("family", category, Variable.VariableType.TEXT, null, true, false, Collections.<String>emptyList(), variableList.size(), null, "", null, null)); variableList.add(new Variable("id", category, Variable.VariableType.DOUBLE, null, true, false, Collections.<String>emptyList(), variableList.size(), null, "", null, null)); variableList.add(new Variable("name", category, Variable.VariableType.TEXT, null, true, false, Collections.<String>emptyList(), variableList.size(), null, "", null, null)); variableList.add(new Variable("fatherId", category, Variable.VariableType.DOUBLE, null, false, false, Collections.<String>emptyList(), variableList.size(), null, "", null, null)); variableList.add(new Variable("fatherName", category, Variable.VariableType.TEXT, null, false, false, Collections.<String>emptyList(), variableList.size(), null, "", null, null)); variableList.add(new Variable("motherId", category, Variable.VariableType.DOUBLE, null, false, false, Collections.<String>emptyList(), variableList.size(), null, "", null, null)); variableList.add(new Variable("motherName", category, Variable.VariableType.TEXT, null, false, false, Collections.<String>emptyList(), variableList.size(), null, "", null, null)); allowedSexValues.add(individual.getSex()); variableList.add(new Variable("sex", category, Variable.VariableType.CATEGORICAL, null, true, false, new LinkedList<>(allowedSexValues), variableList.size(), null, "", null, null)); variableList.add(new Variable("phenotype", category, Variable.VariableType.CATEGORICAL, null, true, false, new LinkedList<>(allowedPhenotypeValues), variableList.size(), null, "", null, null)); variableList.add(new Variable(entry.getKey(), category, type, null, false, false, new ArrayList<>(allowedValues), variableList.size(), null, "", null, null));
ParamUtils.checkParameter(variable.getId(), "variable ID"); ParamUtils.checkObj(variable.getType(), "variable Type"); variable.setAllowedValues(ParamUtils.defaultObject(variable.getAllowedValues(), Collections.emptyList())); variable.setAttributes(ParamUtils.defaultObject(variable.getAttributes(), Collections.emptyMap())); variable.setCategory(ParamUtils.defaultString(variable.getCategory(), "")); variable.setDependsOn(ParamUtils.defaultString(variable.getDependsOn(), "")); variable.setDescription(ParamUtils.defaultString(variable.getDescription(), "")); variable.setName(ParamUtils.defaultString(variable.getName(), variable.getId()));
public QueryResult<VariableSet> addFieldToVariableSet(String studyStr, String variableSetStr, Variable variable, String sessionId) throws CatalogException { if (StringUtils.isEmpty(variable.getId())) { if (StringUtils.isEmpty(variable.getName())) { throw new CatalogException("Missing variable id"); } variable.setId(variable.getName()); } MyResourceId resource = getVariableSetId(variableSetStr, studyStr, sessionId); String userId = resource.getUser(); authorizationManager.checkCanCreateUpdateDeleteVariableSets(resource.getStudyId(), userId); QueryResult<VariableSet> queryResult = studyDBAdaptor.addFieldToVariableSet(resource.getResourceId(), variable, userId); auditManager.recordDeletion(AuditRecord.Resource.variableSet, resource.getResourceId(), userId, queryResult.first(), null, null); return queryResult; }
private void removeAnnotation(Map<String, Object> annotations, String annotation, Set<Variable> variables) throws CatalogException { String[] split = StringUtils.split(annotation, ".", 2); if (split.length > 1) { if (annotations.containsKey(split[0])) { Set<Variable> nestedVariables = null; for (Variable tmpVariable : variables) { if (tmpVariable.getId().equals(split[0])) { nestedVariables = tmpVariable.getVariableSet(); break; } } if (nestedVariables == null) { throw new CatalogException("Internal error. Could not validate removal of variable."); } removeAnnotation((Map<String, Object>) annotations.get(split[0]), split[1], nestedVariables); } } else { for (Variable variable : variables) { if (variable.getId().equals(annotation)) { // We check if the annotation to be remove is mandatory if (variable.isRequired()) { throw new CatalogException("Cannot remove required variable."); } break; } } annotations.remove(annotation); } }
Object defaultValue = variable.getDefaultValue(); switch (variable.getType()) { case BOOLEAN: Boolean booleanValue = getBooleanValue(defaultValue); if (booleanValue != null) { annotation.put(variable.getId(), booleanValue); return true; Double numericValue = getNumericValue(defaultValue); if (numericValue != null) { annotation.put(variable.getId(), numericValue); return true; Integer integerValue = getIntegerValue(defaultValue); if (integerValue != null) { annotation.put(variable.getId(), integerValue); return true; String stringValue = getStringValue(defaultValue); if (stringValue != null) { annotation.put(variable.getId(), stringValue); return true; if (variable.getDefaultValue() != null) { annotation.put(variable.getId(), variable.getDefaultValue()); return true; throw new CatalogException("Unknown VariableType " + variable.getType().name());
Set<Variable> nestedVariables = null; for (Variable tmpVariable : variables) { if (tmpVariable.getId().equals(split[0])) { nestedVariables = tmpVariable.getVariableSet(); break; boolean reset = false; for (Variable variable : variables) { if (variable.getId().equals(annotation)) { if (variable.getDefaultValue() == null) { throw new CatalogException("No default value found for variable."); } else { annotations.put(annotation, variable.getDefaultValue()); reset = true;
ab1Set.add(new Variable().setId("ab1c1").setMultiValue(true).setType(Variable.VariableType.BOOLEAN)); ab1Set.add(new Variable().setId("ab1c2").setType(Variable.VariableType.TEXT)); ab2c1Set.add(new Variable().setId("ab2c1d1").setMultiValue(true).setType(Variable.VariableType.INTEGER)); ab2c1Set.add(new Variable().setId("ab2c1d2").setType(Variable.VariableType.TEXT)); Set<Variable> ab2Set = new HashSet<>(); ab2Set.add(new Variable().setId("ab2c1").setType(Variable.VariableType.OBJECT).setVariableSet(ab2c1Set)); ab3c1Set.add(new Variable().setId("ab3c1d1").setMultiValue(true).setType(Variable.VariableType.TEXT)); ab3c1Set.add(new Variable().setId("ab3c1d2").setType(Variable.VariableType.DOUBLE)); Set<Variable> ab3Set = new HashSet<>(); ab3Set.add(new Variable().setId("ab3c1").setType(Variable.VariableType.OBJECT).setMultiValue(true).setVariableSet(ab3c1Set)); aSet.add(new Variable().setId("ab1").setType(Variable.VariableType.OBJECT).setVariableSet(ab1Set)); aSet.add(new Variable().setId("ab2").setMultiValue(true).setType(Variable.VariableType.OBJECT).setVariableSet(ab2Set)); aSet.add(new Variable().setId("ab3").setMultiValue(true).setType(Variable.VariableType.OBJECT).setVariableSet(ab3Set)); rootSet.add(new Variable().setId("a").setType(Variable.VariableType.OBJECT).setVariableSet(aSet));
if (variable.getType() == Variable.VariableType.OBJECT) { if (variable.getVariableSet() != null) { for (Variable nestedVariable : variable.getVariableSet()) { List<String> keys = new ArrayList<>(variableDepthMap.getKeys()); keys.add(variable.getId()); if (variable.isMultiValue()) { nestedVariable.setMultiValue(variable.isMultiValue()); switch (variable.getType()) { case BOOLEAN: if (variable.isMultiValue()) { type = QueryParam.Type.BOOLEAN_ARRAY; } else { case CATEGORICAL: case TEXT: if (variable.isMultiValue()) { type = QueryParam.Type.TEXT_ARRAY; } else { if (variable.isMultiValue()) { type = QueryParam.Type.INTEGER_ARRAY; } else { if (variable.isMultiValue()) { type = QueryParam.Type.DECIMAL_ARRAY; } else {
Object realValue = getValue(variable.getType(), value); if (realValue == null) { if (variable.isRequired()) { throw new CatalogException(message + " value '" + value + "' is a required value for " + variable); } else { if (!variable.isMultiValue()) { throw new CatalogException(message + " value '" + value + "' does not accept multiple values for " + variable); switch (variable.getType()) { case BOOLEAN: break; for (Object object : listValues) { String stringValue = (String) object; if (variable.getAllowedValues() != null && !variable.getAllowedValues().contains(stringValue)) { throw new CatalogException(message + " value '" + value + "' is not an allowed value for " + variable); if (variable.getAllowedValues() != null && !variable.getAllowedValues().isEmpty()) { boolean valid = false; for (String range : variable.getAllowedValues()) { String[] split = range.split(":", -1); int min = split[0].isEmpty() ? Integer.MIN_VALUE : Integer.valueOf(split[0]); if (variable.getAllowedValues() != null && !variable.getAllowedValues().isEmpty()) { boolean valid = false; for (String range : variable.getAllowedValues()) { String[] split = range.split(":", -1); Double min = split[0].isEmpty() ? Double.MIN_VALUE : Double.valueOf(split[0]);
private Variable getVariable(VariableSet variableSet, String variableId) throws CatalogDBException { for (Variable variable : variableSet.getVariables()) { if (variable.getId().equals(variableId)) { return variable; } } return null; }
public static void checkVariable(Variable variable) throws CatalogException { List<String> acceptedValues = new LinkedList<>(); if (variable.getAllowedValues() != null) { acceptedValues.addAll(variable.getAllowedValues()); variable.setAllowedValues(acceptedValues); if (variable.getType() == null) { throw new CatalogException("VariableType is null"); if (variable.getVariableSet() != null && variable.getType() != Variable.VariableType.OBJECT) { throw new CatalogException("Only variables with type \"OBJECT\" can define an internal variableSet"); switch (variable.getType()) { case BOOLEAN: if (!variable.getAllowedValues().isEmpty()) { throw new CatalogException("Variable type boolean can not contain accepted values"); if (!variable.getAllowedValues().isEmpty()) { for (String range : variable.getAllowedValues()) { String[] split = range.split(":", -1); if (split.length != 2) { break; case OBJECT: if (variable.getVariableSet() != null) { for (Variable v : variable.getVariableSet()) { checkVariable(v); throw new CatalogException("Unknown VariableType " + variable.getType().name());
@Test public void annotationToDB1() throws Exception { Variable v = new Variable().setId("a").setType(Variable.VariableType.BOOLEAN); Set<Variable> setOfVariables = new HashSet<>(); setOfVariables.add(v); VariableSet variableSet = new VariableSet().setVariables(setOfVariables).setUid(1); Map<String, Object> myMap = new HashMap<>(); myMap.put("b", "nothing"); AnnotationConverter annotableConverter = new AnnotationConverter(); List<Document> document1 = annotableConverter.annotationToDB(variableSet, "annotName", myMap); assertTrue(document1.isEmpty()); myMap.put("a", "hello"); List<Document> document2 = annotableConverter.annotationToDB(variableSet, "annotName", myMap); assertEquals(1, document2.size()); assertEquals(5, document2.get(0).size()); assertEquals("a", document2.get(0).get(AnnotationConverter.ID)); assertEquals("annotName", document2.get(0).get(AnnotationConverter.ANNOTATION_SET_NAME)); assertEquals(1L, document2.get(0).get(AnnotationConverter.VARIABLE_SET)); assertEquals("hello", document2.get(0).get(AnnotationConverter.VALUE)); }
/** * Creates a new variable once and attempts to create the same one again. * @throws CatalogDBException */ @Test public void addFieldToVariableSetTest1() throws CatalogDBException, CatalogAuthorizationException { createExampleVariableSet("VARSET_1", false); createExampleVariableSet("VARSET_2", true); Variable variable = new Variable("NAM", "", Variable.VariableType.TEXT, "", true, false, Collections.emptyList(), 0, "", "", null, Collections.emptyMap()); QueryResult<VariableSet> queryResult = catalogStudyDBAdaptor.addFieldToVariableSet(18, variable, user3.getId()); // Check that the new variable has been inserted in the variableSet assertTrue(queryResult.first().getVariables().stream().filter(variable1 -> variable.getId().equals(variable1.getId())).findAny() .isPresent()); // We try to insert the same one again. thrown.expect(CatalogDBException.class); thrown.expectMessage("already exist"); catalogStudyDBAdaptor.addFieldToVariableSet(18, variable, user3.getId()); }
Map<String, Variable> variableMap = new HashMap<>(); for (Variable variable : variableSet.getVariables()) { variableMap.put(variable.getId(), variable); if (!annotatedVariables.contains(variable.getId())) { if (addDefaultValues) { if (!addDefaultAnnotation(variable, defaultAnnotations)) { if (variable.isRequired()) { throw new CatalogException("Missing required variable " + variable.getId()); annotatedVariables.add(variable.getId()); } else { if (variable.isRequired()) { throw new CatalogException("Missing required variable " + variable.getId());
annotation = ((Map<String, Object>) annotation).get(keys.get(i)); if (annotation == null) { if (variable.getDefaultValue() != null && variable.isRequired()) { return new Document(VALUE, variable.getDefaultValue()); } else { return document; // No annotation found for the variable annotation = ((Map<String, Object>) annotation).get(keys.get(i)); if (annotation == null) { if (variable.getDefaultValue() != null && variable.isRequired()) { return new Document(VALUE, variable.getDefaultValue()); } else { return document; // No annotation found for the variable
bcSet.add(new Variable().setId("c").setMultiValue(true).setType(Variable.VariableType.BOOLEAN)); Set<Variable> bSet = new HashSet<>(); bSet.add(new Variable().setId("b").setType(Variable.VariableType.OBJECT).setVariableSet(bcSet)); Set<Variable> rootSet = new HashSet<>(); rootSet.add(new Variable().setId("a").setMultiValue(true).setType(Variable.VariableType.OBJECT).setVariableSet(bSet));
if (variable.isMultiValue()) { arrayElems = Collections.singletonList(0); } else { arrayElems = Collections.emptyList(); queue.add(new VariableLevel(variable, Collections.singletonList(variable.getId()), arrayElems)); VariableLevel variableLevel = queue.remove(); switch (variableLevel.getVariable().getType()) { case BOOLEAN: case CATEGORICAL: if (variable.getVariableSet() != null) { for (Variable tmpVariable : variable.getVariableSet()) { List<Integer> arrayLevel = new ArrayList<>(variableLevel.getArrayLevel()); if (tmpVariable.isMultiValue()) { arrayLevel.add(variableLevel.getKeys().size()); keys.add(tmpVariable.getId());
public static void checkVariableSet(VariableSet variableSet) throws CatalogException { Set<String> variableIdSet = new HashSet<>(); for (Variable variable : variableSet.getVariables()) { if (variableIdSet.contains(variable.getId())) { throw new CatalogException("Duplicated variable Id"); } variableIdSet.add(variable.getId()); } for (Variable variable : variableSet.getVariables()) { checkVariable(variable); } }
@Override public QueryResult<VariableSet> addFieldToVariableSet(long variableSetId, Variable variable, String user) throws CatalogDBException, CatalogAuthorizationException { long startTime = startQuery(); QueryResult<VariableSet> variableSet = getVariableSet(variableSetId, new QueryOptions(), user); checkVariableNotInVariableSet(variableSet.first(), variable.getId()); Bson bsonQuery = Filters.eq(QueryParams.VARIABLE_SET_UID.key(), variableSetId); Bson update = Updates.push(QueryParams.VARIABLE_SET.key() + ".$." + VariableSetParams.VARIABLE.key(), getMongoDBDocument(variable, "variable")); QueryResult<UpdateResult> queryResult = studyCollection.update(bsonQuery, update, null); if (queryResult.first().getModifiedCount() == 0) { throw CatalogDBException.updateError("VariableSet", variableSetId); } if (variable.isRequired()) { dbAdaptorFactory.getCatalogSampleDBAdaptor().addVariableToAnnotations(variableSetId, variable); dbAdaptorFactory.getCatalogCohortDBAdaptor().addVariableToAnnotations(variableSetId, variable); dbAdaptorFactory.getCatalogIndividualDBAdaptor().addVariableToAnnotations(variableSetId, variable); dbAdaptorFactory.getCatalogFamilyDBAdaptor().addVariableToAnnotations(variableSetId, variable); dbAdaptorFactory.getCatalogFileDBAdaptor().addVariableToAnnotations(variableSetId, variable); } return endQuery("Add field to variable set", startTime, getVariableSet(variableSetId, null)); }
bcSet.add(new Variable().setId("c").setType(Variable.VariableType.BOOLEAN)); Set<Variable> bSet = new HashSet<>(); bSet.add(new Variable().setId("b").setMultiValue(true).setType(Variable.VariableType.OBJECT).setVariableSet(bcSet)); Set<Variable> rootSet = new HashSet<>(); rootSet.add(new Variable().setId("a").setMultiValue(true).setType(Variable.VariableType.OBJECT).setVariableSet(bSet));