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