001/* 
002 * Copyright 2018 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 uk.ac.ebi.pride.jmztab2.utils.parser;
017
018import de.isas.mztab2.model.Metadata;
019import de.isas.mztab2.model.OptColumnMapping;
020import de.isas.mztab2.model.SmallMoleculeEvidence;
021import uk.ac.ebi.pride.jmztab2.model.IMZTabColumn;
022import uk.ac.ebi.pride.jmztab2.model.ISmallMoleculeEvidenceColumn;
023import uk.ac.ebi.pride.jmztab2.model.MZBoolean;
024import uk.ac.ebi.pride.jmztab2.model.MZTabColumnFactory;
025import uk.ac.ebi.pride.jmztab2.model.MZTabConstants;
026import uk.ac.ebi.pride.jmztab2.model.OptionColumn;
027import uk.ac.ebi.pride.jmztab2.model.SmallMoleculeEvidenceColumn;
028import uk.ac.ebi.pride.jmztab2.model.SmallMoleculeEvidenceColumn.Stable;
029import uk.ac.ebi.pride.jmztab2.utils.errors.MZTabErrorList;
030
031
032/**
033 * <p>SMELineParser class.</p>
034 *
035 * @author nilshoffmann
036 * @since 11/09/17
037 * 
038 */
039public class SMELineParser extends MZTabDataLineParser<SmallMoleculeEvidence> {
040
041    private SmallMoleculeEvidence smallMoleculeEvidence;
042
043    /**
044     * <p>Constructor for SMELineParser.</p>
045     *
046     * @param context a {@link uk.ac.ebi.pride.jmztab2.utils.parser.MZTabParserContext} object.
047     * @param factory a {@link uk.ac.ebi.pride.jmztab2.model.MZTabColumnFactory} object.
048     * @param positionMapping a {@link uk.ac.ebi.pride.jmztab2.utils.parser.PositionMapping} object.
049     * @param metadata a {@link de.isas.mztab2.model.Metadata} object.
050     * @param errorList a {@link uk.ac.ebi.pride.jmztab2.utils.errors.MZTabErrorList} object.
051     */
052    public SMELineParser(MZTabParserContext context, MZTabColumnFactory factory, PositionMapping positionMapping,
053                         Metadata metadata, MZTabErrorList errorList) {
054        super(context, factory, positionMapping, metadata, errorList);
055    }
056
057    /** {@inheritDoc} */
058    @Override
059    protected int checkData() {
060
061        IMZTabColumn column;
062        String columnName;
063        String target;
064        int physicalPosition;
065        String logicalPosition;
066        smallMoleculeEvidence = new SmallMoleculeEvidence();
067
068        for (physicalPosition = 1; physicalPosition < items.length; physicalPosition++) {
069            logicalPosition = positionMapping.get(physicalPosition);
070            column = factory.getColumnMapping().get(logicalPosition);
071
072            if (column != null) {
073                columnName = column.getName();
074                target = items[physicalPosition];
075                if (column instanceof ISmallMoleculeEvidenceColumn) {
076                    Stable stableColumn = SmallMoleculeEvidenceColumn.Stable.forName(columnName);
077                    switch(stableColumn) {
078                        case ADDUCT_ION:
079                            smallMoleculeEvidence.adductIon(checkString(column,
080                                target));
081                            break;
082                        case CHARGE:
083                            smallMoleculeEvidence.charge(checkInteger(column,
084                                checkData(column, target, false)));
085                            break;
086                        case CHEMICAL_FORMULA:
087                            smallMoleculeEvidence.chemicalFormula(
088                                checkString(column, target));
089                            break;
090                        case CHEMICAL_NAME:
091                            smallMoleculeEvidence.chemicalName(checkString(column, target));
092                            break;
093                        case DATABASE_IDENTIFIER:
094                            smallMoleculeEvidence.databaseIdentifier(checkString(column, target, true));
095                            break;
096                        case DERIVATIZED_FORM:
097                            smallMoleculeEvidence.derivatizedForm(checkParameter(column, target, true));
098                            break;
099                        case EVIDENCE_INPUT_ID:
100                            smallMoleculeEvidence.evidenceInputId(checkString(column, target, false));
101                            break;
102                        case EXP_MASS_TO_CHARGE:
103                            smallMoleculeEvidence.expMassToCharge(checkDouble(column, target, false));
104                            break;
105                        case IDENTIFICATION_METHOD:
106                            smallMoleculeEvidence.identificationMethod(checkParameter(column, target, false));
107                            break;
108                        case INCHI:
109                            smallMoleculeEvidence.inchi(checkString(column, target));
110                            break;
111                        case MS_LEVEL:
112                            smallMoleculeEvidence.msLevel(checkParameter(column, target, false));
113                            break;
114                        case RANK:
115                            smallMoleculeEvidence.rank(checkInteger(column, target, false));
116                            break;
117                        case SME_ID:
118                            smallMoleculeEvidence.smeId(checkInteger(column, target, false));
119                            break;
120                        case SMILES:
121                            smallMoleculeEvidence.smiles(checkString(column, target));
122                            break;
123                        case SPECTRA_REF:
124                            smallMoleculeEvidence.spectraRef(checkSpectraRef(context, column, target, false));
125                            break;
126                        case THEORETICAL_MASS_TO_CHARGE:
127                            smallMoleculeEvidence.theoreticalMassToCharge(checkDouble(column, target, false));
128                            break;
129                        case URI:
130                            smallMoleculeEvidence.uri(checkURI(column, target));
131                            break;
132                    }
133
134                } else if (column instanceof OptionColumn) {
135                   if (columnName.startsWith(MZTabConstants.OPT_PREFIX)) {
136                        Class dataType = column.getDataType();
137                        OptColumnMapping optColMapping = new OptColumnMapping();
138                        optColMapping.identifier(columnName.substring(MZTabConstants.OPT_PREFIX.length()));
139                        if (dataType.equals(String.class)) {
140                            optColMapping.value(checkString(column, target));
141                        } else if (dataType.equals(Double.class)) {
142                            optColMapping.value(Double.toString(checkDouble(column, target)));
143                        } else if (dataType.equals(MZBoolean.class)) {
144                            optColMapping.value(Boolean.toString(checkMZBoolean(column, target).toBoolean()));
145                        }
146                        smallMoleculeEvidence.addOptItem(optColMapping);
147                   }
148                } else if (column.getName().equals(SmallMoleculeEvidence.Properties.idConfidenceMeasure.getPropertyName())) {
149                    smallMoleculeEvidence.addIdConfidenceMeasureItem(checkDouble(column, target));
150                }
151            }
152        }
153
154        return physicalPosition;
155    }
156
157    /** {@inheritDoc} */
158    @Override
159    public SmallMoleculeEvidence getRecord() {
160
161        if(smallMoleculeEvidence == null){
162            smallMoleculeEvidence = new SmallMoleculeEvidence();
163        }
164        return smallMoleculeEvidence;
165    }
166}