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.Assay;
019import de.isas.mztab2.model.IndexedElement;
020import de.isas.mztab2.model.StudyVariable;
021import java.util.SortedMap;
022import java.util.TreeMap;
023
024/**
025 * If the data exporter wishes to report only final results for 'Summary' files (i.e. following averaging over replicates),
026 * then these MUST be reported as quantitative values in the columns associated with the study_variable[1-n] (e.g.
027 * abundance_study_variable[1]). mzTab allows the reporting of abundance, standard deviation, and standard error
028 * for any study_variable. The unit of values in the abundance column MUST be specified in the metadata section of the mzTab file.
029 * The reported values SHOULD represent the final result of the performed data analysis. The exact meaning of the values will
030 * thus depend on the used analysis pipeline and quantitation method and is not expected to be comparable across multiple mzTab files.
031 *
032 * @author qingwei
033 * @author nilshoffmann
034 * @since 23/05/13
035 * 
036 */
037public class AbundanceColumn extends MZTabColumn {
038    public enum Field {
039        ABUNDANCE_ASSAY          ("abundance_assay",              Double.class,    1),
040        ABUNDANCE_STUDY_VARIABLE ("abundance_study_variable",     Double.class,    2),
041        ABUNDANCE_VARIATION_STUDY_VARIABLE ("abundance_variation_study_variable",        Double.class,    3);
042
043        private String name;
044        private Class columnType;
045        private int position;
046
047        Field(String name, Class columnType, int position) {
048            this.name = name;
049            this.columnType = columnType;
050            this.position = position;
051        }
052
053        @Override
054        public String toString() {
055            return name;
056        }
057    }
058
059    /**
060     * Generate a abundance column:
061     * The column header is: {Section}_{Field#name()}_{IndexedElement[id]}
062     * The column data type: {Field#columnType()}
063     * The column position: always most right side, calculated by offset.
064     */
065    private AbundanceColumn(Section section, Field field, Object element, int offset) {
066        super(field.name, field.columnType, true, offset + field.position + "");
067        setElement(element);
068    }
069
070    /**
071     * Generate a abundance optional column as measured in the given assay.The column header like
072     * protein_abundance_assay[1-n], the position always stay the most right of the tabled section,
073     * and the data type is Double.
074     *
075     * @param section SHOULD be {@link uk.ac.ebi.pride.jmztab2.model.Section#Protein}, {@link uk.ac.ebi.pride.jmztab2.model.Section#Peptide}, {@link uk.ac.ebi.pride.jmztab2.model.Section#PSM},
076     *                or {@link uk.ac.ebi.pride.jmztab2.model.Section#Small_Molecule}.
077     * @param assay SHOULD not be null.
078     * @param offset Normally the last column's position in header, {@link uk.ac.ebi.pride.jmztab2.model.MZTabColumnFactory#getColumnMapping()},
079     * @return an abundance optional column as measured in the given assay.
080     */
081    public static MZTabColumn createOptionalColumn(Section section, Assay assay, int offset) {
082        if (section.isComment() || section.isMetadata()) {
083            throw new IllegalArgumentException("Section should be Protein, Peptide, PSM or SmallMolecule.");
084        }
085        if (assay == null) {
086            throw new NullPointerException("Assay should not be null!");
087        }
088
089        return new AbundanceColumn(Section.toDataSection(section), Field.ABUNDANCE_ASSAY, assay, offset);
090    }
091
092    /**
093     * Generate an abundance optional column as measured in the given study variable.
094     * The header can be one of abundance_study_variable[1], abundance_coeffvar_study_variable[1].
095     * The position always stay the most right of the tabled section, and the data type is Double.
096     *
097     * @param section SHOULD be {@link uk.ac.ebi.pride.jmztab2.model.Section#Protein}, {@link uk.ac.ebi.pride.jmztab2.model.Section#Peptide}, {@link uk.ac.ebi.pride.jmztab2.model.Section#PSM},
098     *                or {@link uk.ac.ebi.pride.jmztab2.model.Section#Small_Molecule}.
099     * @param studyVariable SHOULD not be null.
100     * @param order position in header for the new columns {@link uk.ac.ebi.pride.jmztab2.model.MZTabColumnFactory#getColumnMapping()}
101     * @return an abundance optional column as measured in the given study variable.
102     * @param columnHeader a processed column header with abundance_ removed.
103     * 
104     */
105    public static SortedMap<String, MZTabColumn> createOptionalColumns(Section section, StudyVariable studyVariable, String columnHeader, String order) {
106        if (section.isComment() || section.isMetadata()) {
107            throw new IllegalArgumentException("Section should be Protein, Peptide, PSM, SmallMolecule, SmallMoleculeFeature or SmallMoleculeEvidence.");
108        }
109        if (studyVariable == null) {
110            throw new NullPointerException("Study Variable should not be null!");
111        }
112
113        //In this case we know the real position in which the column need to star, so the offset is one less
114        int offset = new Integer(order)-1;
115
116        SortedMap<String, MZTabColumn> columns = new TreeMap<>();
117        Section dataSection = Section.toDataSection(section);
118
119        AbundanceColumn column;
120        if(columnHeader.startsWith("study_variable")) {
121            column = new AbundanceColumn(dataSection, Field.ABUNDANCE_STUDY_VARIABLE, studyVariable, offset);
122            columns.put(column.getLogicPosition(), column);
123        } else if (columnHeader.startsWith("variation_study_variable")) {
124            column = new AbundanceColumn(dataSection, Field.ABUNDANCE_VARIATION_STUDY_VARIABLE, studyVariable, offset);
125            columns.put(column.getLogicPosition(), column);
126        } else {
127            throw new IllegalArgumentException("column header "+columnHeader+" is not allowed for abundance definition!");
128        }
129
130        return columns;
131    }
132
133    /**
134     * <p>createOptionalColumns.</p>
135     *
136     * @param section a {@link uk.ac.ebi.pride.jmztab2.model.Section} object.
137     * @param studyVariable a {@link de.isas.mztab2.model.StudyVariable} object.
138     * @param lastOrder a {@link java.lang.Integer} object.
139     * @return a {@link java.util.SortedMap} object.
140     */
141    public static SortedMap<String, MZTabColumn> createOptionalColumns(Section section, StudyVariable studyVariable, Integer lastOrder) {
142        if (section.isComment() || section.isMetadata()) {
143            throw new IllegalArgumentException("Section should be Protein, Peptide, PSM or SmallMolecule.");
144        }
145        if (studyVariable == null) {
146            throw new NullPointerException("Study Variable should not be null!");
147        }
148
149        //In this case we know the real position in which the column need to star, so the offset is one less
150
151        SortedMap<String, MZTabColumn> columns = new TreeMap<>();
152        Section dataSection = Section.toDataSection(section);
153
154        AbundanceColumn column;
155        column = new AbundanceColumn(dataSection, Field.ABUNDANCE_STUDY_VARIABLE, studyVariable, lastOrder);
156        columns.put(column.getLogicPosition(), column);
157        column = new AbundanceColumn(dataSection, Field.ABUNDANCE_VARIATION_STUDY_VARIABLE, studyVariable, lastOrder);
158        columns.put(column.getLogicPosition(), column);
159
160        return columns;
161    }
162}