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 de.isas.mztab2.io.serialization;
017
018import com.fasterxml.jackson.core.JsonGenerator;
019import com.fasterxml.jackson.databind.SerializerProvider;
020import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
021import com.fasterxml.jackson.databind.ser.std.StdSerializer;
022import static de.isas.mztab2.io.serialization.Serializers.writeAsNumberArray;
023import static de.isas.mztab2.io.serialization.Serializers.writeAsStringArray;
024import static de.isas.mztab2.io.serialization.Serializers.writeIndexedDoubles;
025import static de.isas.mztab2.io.serialization.Serializers.writeNumber;
026import static de.isas.mztab2.io.serialization.Serializers.writeObject;
027import static de.isas.mztab2.io.serialization.Serializers.writeOptColumnMappings;
028import static de.isas.mztab2.io.serialization.Serializers.writeString;
029import de.isas.mztab2.model.SmallMoleculeSummary;
030import java.io.IOException;
031import java.util.Collections;
032import java.util.Optional;
033import lombok.extern.slf4j.Slf4j;
034import uk.ac.ebi.pride.jmztab2.model.AbundanceColumn;
035import uk.ac.ebi.pride.jmztab2.model.SmallMoleculeColumn;
036import static uk.ac.ebi.pride.jmztab2.model.SmallMoleculeColumn.Stable.columnFor;
037
038/**
039* <p>SmallMoleculeSummarySerializer implementation for {@link de.isas.mztab2.model.SmallMoleculeSummary}.</p>
040 *
041 * @author nilshoffmann
042 *
043 */
044@Slf4j
045public class SmallMoleculeSummarySerializer extends StdSerializer<SmallMoleculeSummary> {
046
047    /**
048     * <p>
049     * Constructor for SmallMoleculeSummarySerializer.</p>
050     */
051    public SmallMoleculeSummarySerializer() {
052        this(null);
053    }
054
055    /**
056     * <p>
057     * Constructor for SmallMoleculeSummarySerializer.</p>
058     *
059     * @param t a {@link java.lang.Class} object.
060     */
061    public SmallMoleculeSummarySerializer(Class<SmallMoleculeSummary> t) {
062        super(t);
063    }
064
065    @Override
066    public void serializeWithType(SmallMoleculeSummary value, JsonGenerator gen,
067        SerializerProvider serializers, TypeSerializer typeSer) throws IOException {
068        typeSer.writeTypePrefixForObject(value, gen);
069        serialize(value, gen, serializers);
070        typeSer.writeTypeSuffixForObject(value, gen);
071    }
072
073    /**
074     * {@inheritDoc}
075     */
076    @Override
077    public void serialize(SmallMoleculeSummary smallMoleculeSummary,
078        JsonGenerator jg,
079        SerializerProvider sp) throws IOException {
080        if (smallMoleculeSummary != null) {
081            jg.writeStartObject();
082            writeString(SmallMoleculeSummary.HeaderPrefixEnum.SMH.getValue(), jg,
083                SmallMoleculeSummary.PrefixEnum.SML.
084                    getValue());
085            writeNumber(columnFor(SmallMoleculeColumn.Stable.SML_ID), jg,
086                smallMoleculeSummary.getSmlId());
087            writeAsNumberArray(columnFor(SmallMoleculeColumn.Stable.SMF_ID_REFS),
088                jg,
089                Optional.ofNullable(smallMoleculeSummary.
090                    getSmfIdRefs()).
091                    orElse(Collections.emptyList()));
092            writeAsStringArray(columnFor(
093                SmallMoleculeColumn.Stable.DATABASE_IDENTIFIER),
094                jg, smallMoleculeSummary.getDatabaseIdentifier());
095            writeAsStringArray(columnFor(
096                SmallMoleculeColumn.Stable.CHEMICAL_FORMULA), jg,
097                smallMoleculeSummary.getChemicalFormula());
098            writeAsStringArray(columnFor(SmallMoleculeColumn.Stable.SMILES), jg,
099                smallMoleculeSummary.getSmiles());
100            writeAsStringArray(columnFor(SmallMoleculeColumn.Stable.INCHI), jg,
101                smallMoleculeSummary.getInchi());
102            writeAsStringArray(columnFor(
103                SmallMoleculeColumn.Stable.CHEMICAL_NAME), jg,
104                smallMoleculeSummary.getChemicalName());
105            writeAsStringArray(columnFor(SmallMoleculeColumn.Stable.URI), jg,
106                smallMoleculeSummary.getUri());
107            writeAsNumberArray(columnFor(
108                SmallMoleculeColumn.Stable.THEOR_NEUTRAL_MASS), jg,
109                smallMoleculeSummary.
110                    getTheoreticalNeutralMass());
111            writeAsStringArray(columnFor(SmallMoleculeColumn.Stable.ADDUCT_IONS),
112                jg,
113                smallMoleculeSummary.getAdductIons());
114            writeString(columnFor(SmallMoleculeColumn.Stable.RELIABILITY), jg,
115                smallMoleculeSummary.getReliability());
116            writeObject(
117                columnFor(SmallMoleculeColumn.Stable.BEST_ID_CONFIDENCE_MEASURE),
118                jg, sp,
119                smallMoleculeSummary.
120                    getBestIdConfidenceMeasure());
121            writeNumber(
122                columnFor(SmallMoleculeColumn.Stable.BEST_ID_CONFIDENCE_VALUE),
123                jg,
124                smallMoleculeSummary.
125                    getBestIdConfidenceValue());
126            writeIndexedDoubles(AbundanceColumn.Field.ABUNDANCE_ASSAY.toString(),
127                jg, Optional.ofNullable(smallMoleculeSummary.
128                    getAbundanceAssay()).
129                    orElse(Collections.emptyList()));
130            writeIndexedDoubles(AbundanceColumn.Field.ABUNDANCE_STUDY_VARIABLE.
131                toString(), jg, Optional.ofNullable(smallMoleculeSummary.
132                    getAbundanceStudyVariable()).
133                    orElse(Collections.emptyList()));
134            writeIndexedDoubles(
135                AbundanceColumn.Field.ABUNDANCE_VARIATION_STUDY_VARIABLE.
136                    toString(), jg,
137                Optional.ofNullable(smallMoleculeSummary.
138                    getAbundanceVariationStudyVariable()).
139                    orElse(Collections.emptyList()));
140
141            writeOptColumnMappings(smallMoleculeSummary.getOpt(), jg, sp);
142            jg.writeEndObject();
143        } else {
144            log.debug(SmallMoleculeSummary.class.getSimpleName() + " is null!");
145        }
146    }
147
148}