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.io.serialization.Serializers;
019import de.isas.mztab2.model.IndexedElement;
020
021/**
022 * Additional columns can be added to the end of the protein table. These column
023 * headers MUST start with the prefix "opt_". Column names MUST only contain the
024 * following characters: 'A'-'Z', 'a'-'z', '0'-'9', '_', '-', '[', ']', and ':'.
025 *
026 * @author qingwei
027 * @since 28/05/13
028 *
029 */
030public class OptionColumn extends MZTabColumn {
031
032    /**
033     * Constant <code>OPT="opt"</code>
034     */
035    public static final String OPT = "opt";
036    /**
037     * Constant <code>GLOBAL="global"</code>
038     */
039    public static final String GLOBAL = "global";
040
041    /**
042     * Get the optional column header, which start with the prefix "opt_". the
043     * format: opt_{indexedElement[id]}_{name}. Spaces within the parameter's
044     * name MUST be replaced by '_'.
045     *
046     * @param element if the name relates to all replicates, we use "global" in
047     * header. Here, if user set element to null, the definition applies for all
048     * replicates.
049     * @param name SHOULD NOT be empty.
050     * @return a {@link java.lang.String} object.
051     */
052    public static String getHeader(Object element, String name) {
053        if (MZTabStringUtils.isEmpty(name)) {
054            throw new IllegalArgumentException(
055                    "Optional column's name should not be empty.");
056        }
057
058        return OPT + "_" + (element == null ? GLOBAL : Serializers.
059                getElementName(element).
060                orElseThrow(()
061                        -> {
062                    return new IllegalArgumentException(
063                            "Could not retrieve element name for " + element.toString());
064                }))
065                + "_" + name.replaceAll(" ", "_");
066    }
067
068    /**
069     * Create a optional column. Which header start with the prefix "opt_",
070     * logical position always stay the end of table.
071     *
072     * @see #getHeader() generate optional column header.
073     * @param element if the value relates to all replicates, we use "global" in
074     * header. Here, if user set element is null for define for all replicates.
075     * @param value SHOULD NOT be empty.
076     * @param columnType SHOULD NOT be empty.
077     * @param offset SHOULD be positive integer.
078     */
079    public OptionColumn(Object element, String value, Class columnType,
080            int offset) {
081        super(getHeader(element, value), columnType, true, offset + 1 + "");
082    }
083}