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.Assay;
020import de.isas.mztab2.model.IndexedElement;
021import de.isas.mztab2.model.MsRun;
022import de.isas.mztab2.model.Parameter;
023import de.isas.mztab2.model.StudyVariable;
024
025/**
026 * An kind of {@link uk.ac.ebi.pride.jmztab2.model.OptionColumn} which use CV parameter accessions in following the format:
027 * opt_{OBJECT_ID}_cv_{accession}_{parameter name}. Spaces within the parameter' s name MUST be replaced by '_'.
028 *
029 * @author qingwei
030 * @since 30/05/13
031 * 
032 */
033public class ParameterOptionColumn extends OptionColumn {
034    /** Constant <code>CV="cv_"</code> */
035    public static final String CV = "cv_";
036
037    private final Parameter param;
038
039    /**
040     * Define a {@link uk.ac.ebi.pride.jmztab2.model.OptionColumn} which use CV parameter accessions in following the format:
041     * opt_{OBJECT_ID}_cv_{accession}_{parameter name}. Spaces within the parameter' s name MUST be replaced by '_'.
042     *
043     * @param element SHOULD not be null.
044     * @param param SHOULD not be null.
045     * @param columnType SHOULD not be null.
046     * @param offset SHOULD be non-negative integer.
047     */
048    public ParameterOptionColumn(Object element, Parameter param, Class columnType, int offset) {
049        super(element, CV + param.getCvAccession() + "_" + param.getName().replaceAll(" ", "_"), columnType, offset);
050        this.param = param;
051    }
052
053    /**
054     * get column header like: opt_{OBJECT_ID}_cv_{accession}_{parameter name}
055     * Spaces within the parameter's name MUST be replaced by '_'.
056     *
057     * @param element {@link Assay}, {@link StudyVariable}, {@link MsRun} or "global" (if the value relates to all replicates).
058     * @param param SHOULD NOT be null.
059     * @return the string representation of this column's header.
060     */
061    public static String getHeader(Object element, Parameter param) {
062        StringBuilder sb = new StringBuilder();
063
064        sb.append(OPT).append("_").append(element == null ? GLOBAL : Serializers.getReference(element, IndexedElement.of(element).getId()));
065        sb.append("_").append(CV).append(param.getCvAccession()).append("_").append(param.getName().replaceAll(" ", "_"));
066
067        return sb.toString();
068    }
069
070}