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.writeNumber;
024import static de.isas.mztab2.io.serialization.Serializers.writeObject;
025import static de.isas.mztab2.io.serialization.Serializers.writeString;
026import de.isas.mztab2.model.SmallMoleculeFeature;
027import java.io.IOException;
028import java.util.Collections;
029import java.util.Optional;
030import lombok.extern.slf4j.Slf4j;
031import uk.ac.ebi.pride.jmztab2.model.AbundanceColumn;
032import uk.ac.ebi.pride.jmztab2.model.SmallMoleculeFeatureColumn;
033import static uk.ac.ebi.pride.jmztab2.model.SmallMoleculeFeatureColumn.Stable.columnFor;
034
035/**
036 * <p>SmallMoleculeFeatureSerializer implementation for {@link de.isas.mztab2.model.SmallMoleculeFeature}.</p>
037 *
038 * @author nilshoffmann
039 *
040 */
041@Slf4j
042public class SmallMoleculeFeatureSerializer extends StdSerializer<SmallMoleculeFeature> {
043
044    /**
045     * <p>
046     * Constructor for SmallMoleculeFeatureSerializer.</p>
047     */
048    public SmallMoleculeFeatureSerializer() {
049        this(null);
050    }
051
052    /**
053     * <p>
054     * Constructor for SmallMoleculeFeatureSerializer.</p>
055     *
056     * @param t a {@link java.lang.Class} object.
057     */
058    public SmallMoleculeFeatureSerializer(Class<SmallMoleculeFeature> t) {
059        super(t);
060    }
061
062    @Override
063    public void serializeWithType(SmallMoleculeFeature value, JsonGenerator gen,
064        SerializerProvider serializers, TypeSerializer typeSer) throws IOException {
065        typeSer.writeTypePrefixForObject(value, gen);
066        serialize(value, gen, serializers);
067        typeSer.writeTypeSuffixForObject(value, gen);
068    }
069
070    /**
071     * {@inheritDoc}
072     */
073    @Override
074    public void serialize(SmallMoleculeFeature smallMoleculeFeature,
075        JsonGenerator jg,
076        SerializerProvider sp) throws IOException {
077        if (smallMoleculeFeature != null) {
078            jg.writeStartObject();
079            writeString(SmallMoleculeFeature.HeaderPrefixEnum.SFH.getValue(), jg,
080                SmallMoleculeFeature.PrefixEnum.SMF.
081                    getValue());
082            writeNumber(columnFor(SmallMoleculeFeatureColumn.Stable.SMF_ID), jg,
083                smallMoleculeFeature.getSmfId());
084            writeAsNumberArray(columnFor(
085                SmallMoleculeFeatureColumn.Stable.SME_ID_REFS), jg,
086                Optional.ofNullable(smallMoleculeFeature.getSmeIdRefs()).
087                    orElse(Collections.emptyList()));
088            writeNumber(
089                columnFor(
090                    SmallMoleculeFeatureColumn.Stable.SME_ID_REF_AMBIGUITY_CODE),
091                jg,
092                smallMoleculeFeature.
093                    getSmeIdRefAmbiguityCode());
094            writeString(columnFor(SmallMoleculeFeatureColumn.Stable.ADDUCT_ION),
095                jg,
096                smallMoleculeFeature.getAdductIon());
097            writeObject(columnFor(SmallMoleculeFeatureColumn.Stable.ISOTOPOMER),
098                jg, sp,
099                smallMoleculeFeature.
100                    getIsotopomer());
101            writeNumber(columnFor(
102                SmallMoleculeFeatureColumn.Stable.EXP_MASS_TO_CHARGE), jg,
103                smallMoleculeFeature.
104                    getExpMassToCharge());
105            writeNumber(columnFor(SmallMoleculeFeatureColumn.Stable.CHARGE), jg,
106                smallMoleculeFeature.getCharge());
107            writeNumber(
108                columnFor(
109                    SmallMoleculeFeatureColumn.Stable.RETENTION_TIME_IN_SECONDS),
110                jg,
111                smallMoleculeFeature.
112                    getRetentionTimeInSeconds());
113            writeNumber(
114                columnFor(
115                    SmallMoleculeFeatureColumn.Stable.RETENTION_TIME_IN_SECONDS_START),
116                jg, smallMoleculeFeature.
117                    getRetentionTimeInSecondsStart());
118            writeNumber(
119                columnFor(
120                    SmallMoleculeFeatureColumn.Stable.RETENTION_TIME_IN_SECONDS_END),
121                jg, smallMoleculeFeature.
122                    getRetentionTimeInSecondsEnd());
123            Serializers.writeIndexedDoubles(
124                AbundanceColumn.Field.ABUNDANCE_ASSAY.toString(), jg,
125                Optional.ofNullable(smallMoleculeFeature.getAbundanceAssay()).
126                    orElse(Collections.emptyList()));
127            Serializers.
128                writeOptColumnMappings(smallMoleculeFeature.getOpt(), jg, sp);
129            jg.writeEndObject();
130        } else {
131            log.debug(SmallMoleculeFeature.class.getSimpleName(), " is null!");
132        }
133    }
134}