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.model;
017
018import de.isas.mztab2.model.IndexedElement;
019import de.isas.mztab2.model.Parameter;
020import de.isas.mztab2.model.SmallMoleculeEvidence;
021import static de.isas.mztab2.model.SmallMoleculeEvidence.Properties.*;
022import de.isas.mztab2.model.StringList;
023import java.util.Arrays;
024import java.util.List;
025import java.util.stream.Collectors;
026
027/**
028 * Define the stable columns which have stable order in the small molecule
029 * evidence header line. Refactored to contain an enum for stable columns.
030 *
031 * @author nilshoffmann
032 * @since 11/09/17
033 *
034 */
035public class SmallMoleculeEvidenceColumn implements ISmallMoleculeEvidenceColumn {
036
037    private final IMZTabColumn column;
038
039    SmallMoleculeEvidenceColumn(String name, Class dataType, boolean optional,
040        String order) {
041        this.column = new MZTabColumn(name, dataType, optional, order);
042    }
043
044    SmallMoleculeEvidenceColumn(String name, Class dataType, boolean optional,
045        String order, Integer id) {
046        this.column = new MZTabColumn(name, dataType, optional, order, id);
047    }
048
049    /**
050     * Stable {@link SmallMoleculeEvidenceColumn} definition templates.
051     */
052    public static enum Stable {
053        SME_ID(smeId.toUpper(), Integer.class, false, "01"),
054        EVIDENCE_INPUT_ID(evidenceInputId, Integer.class, false, "02"),
055        DATABASE_IDENTIFIER(databaseIdentifier, String.class, false, "03"),
056        CHEMICAL_FORMULA(
057            chemicalFormula, String.class, true, "04"),
058        SMILES(smiles,
059            String.class, true, "05"),
060        INCHI(inchi,
061            String.class, true, "06"),
062        CHEMICAL_NAME(
063            chemicalName, String.class, true, "07"),
064        URI(uri,
065            java.net.URI.class, true, "08"),
066        DERIVATIZED_FORM(derivatizedForm, String.class, true, "09"),
067        ADDUCT_ION(
068            adductIon, String.class, false, "10"),
069        EXP_MASS_TO_CHARGE(
070            expMassToCharge, Double.class, false, "11"),
071        CHARGE(charge, Integer.class, false, "12"),
072        THEORETICAL_MASS_TO_CHARGE(theoreticalMassToCharge, Double.class,
073            false, "13"),
074        SPECTRA_REF(spectraRef, StringList.class, false, "14"),
075        IDENTIFICATION_METHOD(identificationMethod, Parameter.class, false,
076            "15"),
077        MS_LEVEL(msLevel, Parameter.class, false, "16"),
078        RANK(rank, Integer.class, false, "17");
079
080        private final ISmallMoleculeEvidenceColumn column;
081
082        private Stable(SmallMoleculeEvidence.Properties property,
083            Class columnType, boolean optional,
084            String order) {
085            this.column = new SmallMoleculeEvidenceColumn(property.
086                getPropertyName(), columnType, optional,
087                order);
088        }
089
090        private Stable(String name, Class columnType, boolean optional,
091            String order) {
092            this.column = new SmallMoleculeEvidenceColumn(name, columnType,
093                optional,
094                order);
095        }
096
097        private Stable(String name, Class columnType, boolean optional,
098            String order, Integer id) {
099            this.column = new SmallMoleculeEvidenceColumn(name, columnType,
100                optional,
101                order, id);
102        }
103
104        /**
105         * Returns a stable column instance template.
106         *
107         * @param name the column name (lower case).
108         * @return the stable column instance template.
109         * @throws IllegalArgumentException for unknown column names.
110         */
111        public static SmallMoleculeEvidenceColumn.Stable forName(String name) throws IllegalArgumentException {
112            SmallMoleculeEvidenceColumn.Stable s = Arrays.stream(
113                SmallMoleculeEvidenceColumn.Stable.values()).
114                filter((v) ->
115                    v.column.
116                        getName().
117                        equals(name)).
118                findFirst().
119                orElseThrow(() ->
120                    new IllegalArgumentException("Unknown key:" + name));
121            return s;
122        }
123
124        /**
125         * Returns a new {@link ISmallMoleculeEvidenceColumn} instance for the
126         * given stable column template.
127         *
128         * @param s the small molecule evidence stable column template.
129         * @return a new small molecule column instance
130         * {@link SmallMoleculeEvidenceColumn}.
131         */
132        public static ISmallMoleculeEvidenceColumn columnFor(
133            SmallMoleculeEvidenceColumn.Stable s) {
134            return new SmallMoleculeEvidenceColumn(s.column.getName(), s.column.
135                getDataType(), s.column.isOptional(), s.column.getOrder());
136        }
137
138       /**
139         * Returns a new {@link ISmallMoleculeEvidenceColumn} instance for the
140         * given stable column name.
141         *
142         * @param name the small molecule stable column template name (lower
143         * case).
144         * @return a new small molecule column instance
145         * {@link SmallMoleculeEvidenceColumn}.
146         * @throws IllegalArgumentException for unknown column names.
147         */
148        public static ISmallMoleculeEvidenceColumn columnFor(String name) throws IllegalArgumentException {
149            return columnFor(forName(name));
150        }
151
152        /**
153         * Returns all stable {@link SmallMoleculeEvidenceColumn} templates.
154         *
155         * @return the stable small molecule columns templates.
156         */
157        public static List<ISmallMoleculeEvidenceColumn> columns() {
158            return Arrays.stream(SmallMoleculeEvidenceColumn.Stable.values()).
159                map((s) ->
160                {
161                    return new SmallMoleculeEvidenceColumn(s.column.getName(),
162                        s.column.getDataType(), s.column.isOptional(), s.column.
163                        getOrder());
164                }).
165                collect(Collectors.toList());
166        }
167    };
168
169    /**
170     * {@inheritDoc}
171     */
172    @Override
173    public Class<?> getDataType() {
174        return this.column.getDataType();
175    }
176
177    /**
178     * {@inheritDoc}
179     */
180    @Override
181    public Object getElement() {
182        return this.column.getElement();
183    }
184
185    /**
186     * {@inheritDoc}
187     */
188    @Override
189    public String getHeader() {
190        return this.column.getHeader();
191    }
192
193    /**
194     * {@inheritDoc}
195     */
196    @Override
197    public String getLogicPosition() {
198        return this.column.getLogicPosition();
199    }
200
201    /**
202     * {@inheritDoc}
203     */
204    @Override
205    public String getName() {
206        return this.column.getName();
207    }
208
209    /**
210     * {@inheritDoc}
211     */
212    @Override
213    public String getOrder() {
214        return this.column.getOrder();
215    }
216
217    /**
218     * {@inheritDoc}
219     */
220    @Override
221    public boolean isOptional() {
222        return this.column.isOptional();
223    }
224
225    /**
226     * {@inheritDoc}
227     */
228    @Override
229    public void setHeader(String header) {
230        this.column.setHeader(header);
231    }
232
233    /**
234     * {@inheritDoc}
235     */
236    @Override
237    public void setLogicPosition(String logicPosition) {
238        this.column.setLogicPosition(logicPosition);
239    }
240
241    /**
242     * {@inheritDoc}
243     */
244    @Override
245    public void setOrder(String order) {
246        this.column.setOrder(order);
247    }
248
249    /**
250     * {@inheritDoc}
251     */
252    @Override
253    public void setElement(Object element) {
254        this.column.setElement(element);
255    }
256}