001/*
002 * Copyright 2019 Leibniz-Institut für Analytische Wissenschaften – ISAS – e.V..
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package de.isas.mztab2.io.validators;
017
018import de.isas.mztab2.model.Assay;
019import de.isas.mztab2.model.Metadata;
020import de.isas.mztab2.model.StudyVariable;
021import java.util.LinkedList;
022import java.util.List;
023import java.util.SortedMap;
024import uk.ac.ebi.pride.jmztab2.utils.errors.LogicalErrorType;
025import uk.ac.ebi.pride.jmztab2.utils.errors.MZTabError;
026import uk.ac.ebi.pride.jmztab2.utils.parser.MZTabParserContext;
027
028/**
029 * Validates that the study variable section is present in metadata and is
030 * linked to all assays and ms runs.
031 *
032 * @author nilshoffmann
033 */
034public class StudyVariableValidator implements RefiningValidator<Metadata> {
035
036    @Override
037    public List<MZTabError> validateRefine(Metadata metadata, MZTabParserContext parserContext) {
038        SortedMap<Integer, Assay> assayMap = parserContext.getAssayMap();
039        SortedMap<Integer, StudyVariable> svMap = parserContext.getStudyVariableMap();
040        List<MZTabError> errorList = new LinkedList<>();
041        if (svMap.isEmpty()) {
042            errorList.add(new MZTabError(
043                    LogicalErrorType.SingleStudyVariableName, -1));
044        } else {
045            if (svMap.size() == 1 && assayMap.size() > 0) {
046                StudyVariable sv = svMap.get(Integer.valueOf(1));
047                if (sv.getName() == null || sv.getName().isEmpty()) {
048                    errorList.add(new MZTabError(
049                            LogicalErrorType.SingleStudyVariableName, -1));
050                }
051            } else {
052                boolean undefinedDefined = false;
053                for (Integer id : svMap.keySet()) {
054                    StudyVariable sv = svMap.get(id);
055                    if (sv == null) {
056                        errorList.add(new MZTabError(
057                                LogicalErrorType.NotDefineInMetadata, -1,
058                                Metadata.Properties.studyVariable + "[" + id + "]" + "\t" + "<NAME>"));
059                    } else {
060                        if (sv.getName() == null) {
061                            errorList.add(new MZTabError(
062                                    LogicalErrorType.NotDefineInMetadata, -1,
063                                    Metadata.Properties.studyVariable + "[" + id + "]" + "\t" + "<NAME>"));
064                        } else {
065                            if (sv.getName().equals("undefined")) {
066                                if (undefinedDefined) {
067                                    errorList.add(new MZTabError(
068                                            LogicalErrorType.UndefinedStudyVariableNameOnceOnly, -1,
069                                            Metadata.Properties.studyVariable + "[" + id + "]" + "\t" + "<NAME>"));
070                                } else {
071                                    undefinedDefined = true;
072                                }
073                            }
074                        }
075                        if (sv.
076                                getDescription() == null) {
077                            errorList.add(new MZTabError(
078                                    LogicalErrorType.NotDefineInMetadata, -1,
079                                    Metadata.Properties.studyVariable + "[" + id + "]-" + StudyVariable.Properties.description + "\t" + "<DESCRIPTION>"));
080                        }
081                        if (sv.
082                                getAssayRefs() == null || sv.getAssayRefs().
083                                        isEmpty()) {
084                            errorList.add(new MZTabError(
085                                    LogicalErrorType.AssayRefs, -1,
086                                    Metadata.Properties.studyVariable + "[" + id + "]-" + StudyVariable.Properties.assayRefs));
087                        }
088                    }
089                }
090            }
091        }
092
093        return errorList;
094    }
095
096}