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.writeAsStringArray;
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.Metadata;
027import de.isas.mztab2.model.SmallMoleculeEvidence;
028import java.io.IOException;
029import java.util.Collections;
030import java.util.Optional;
031import java.util.stream.Collectors;
032import lombok.extern.slf4j.Slf4j;
033import uk.ac.ebi.pride.jmztab2.model.SmallMoleculeEvidenceColumn;
034import static uk.ac.ebi.pride.jmztab2.model.SmallMoleculeEvidenceColumn.Stable.columnFor;
035
036/**
037* <p>SmallMoleculeEvidenceSerializer implementation for {@link de.isas.mztab2.model.SmallMoleculeEvidence}.</p>
038 *
039 * @author nilshoffmann
040 *
041 */
042@Slf4j
043public class SmallMoleculeEvidenceSerializer extends StdSerializer<SmallMoleculeEvidence> {
044
045    /**
046     * <p>
047     * Constructor for SmallMoleculeEvidenceSerializer.</p>
048     */
049    public SmallMoleculeEvidenceSerializer() {
050        this(null);
051    }
052
053    /**
054     * <p>
055     * Constructor for SmallMoleculeEvidenceSerializer.</p>
056     *
057     * @param t a {@link java.lang.Class} object.
058     */
059    public SmallMoleculeEvidenceSerializer(Class<SmallMoleculeEvidence> t) {
060        super(t);
061    }
062
063    @Override
064    public void serializeWithType(SmallMoleculeEvidence value, JsonGenerator gen,
065        SerializerProvider serializers, TypeSerializer typeSer) throws IOException {
066        typeSer.writeTypePrefixForObject(value, gen);
067        serialize(value, gen, serializers);
068        typeSer.writeTypeSuffixForObject(value, gen);
069    }
070
071    /**
072     * {@inheritDoc}
073     */
074    @Override
075    public void serialize(SmallMoleculeEvidence smallMoleculeEvidence,
076        JsonGenerator jg,
077        SerializerProvider sp) throws IOException {
078        if (smallMoleculeEvidence != null) {
079            jg.writeStartObject();
080            writeString(SmallMoleculeEvidence.HeaderPrefixEnum.SEH.getValue(),
081                jg, SmallMoleculeEvidence.PrefixEnum.SME.
082                    getValue());
083            writeNumber(columnFor(
084                SmallMoleculeEvidenceColumn.Stable.SME_ID), jg,
085                smallMoleculeEvidence.getSmeId());
086            writeString(columnFor(
087                SmallMoleculeEvidenceColumn.Stable.EVIDENCE_INPUT_ID),
088                jg, smallMoleculeEvidence.getEvidenceInputId());
089            writeString(columnFor(
090                SmallMoleculeEvidenceColumn.Stable.DATABASE_IDENTIFIER),
091                jg, smallMoleculeEvidence.getDatabaseIdentifier());
092            writeString(columnFor(
093                SmallMoleculeEvidenceColumn.Stable.CHEMICAL_FORMULA), jg,
094                smallMoleculeEvidence.getChemicalFormula());
095            writeString(columnFor(SmallMoleculeEvidenceColumn.Stable.SMILES), jg,
096                smallMoleculeEvidence.getSmiles());
097            writeString(columnFor(SmallMoleculeEvidenceColumn.Stable.INCHI), jg,
098                smallMoleculeEvidence.getInchi());
099            writeString(columnFor(
100                SmallMoleculeEvidenceColumn.Stable.CHEMICAL_NAME), jg,
101                smallMoleculeEvidence.getChemicalName());
102            writeString(columnFor(SmallMoleculeEvidenceColumn.Stable.URI), jg,
103                smallMoleculeEvidence.getUri());
104            writeObject(columnFor(
105                SmallMoleculeEvidenceColumn.Stable.DERIVATIZED_FORM), jg,
106                sp,
107                smallMoleculeEvidence.getDerivatizedForm());
108            writeString(columnFor(SmallMoleculeEvidenceColumn.Stable.ADDUCT_ION),
109                jg,
110                smallMoleculeEvidence.getAdductIon());
111            writeNumber(columnFor(
112                SmallMoleculeEvidenceColumn.Stable.EXP_MASS_TO_CHARGE),
113                jg, smallMoleculeEvidence.
114                    getExpMassToCharge());
115            writeNumber(columnFor(SmallMoleculeEvidenceColumn.Stable.CHARGE), jg,
116                smallMoleculeEvidence.getCharge());
117            writeNumber(
118                columnFor(
119                    SmallMoleculeEvidenceColumn.Stable.THEORETICAL_MASS_TO_CHARGE),
120                jg, smallMoleculeEvidence.
121                    getTheoreticalMassToCharge());
122            writeAsStringArray(columnFor(
123                SmallMoleculeEvidenceColumn.Stable.SPECTRA_REF),
124                jg, Optional.ofNullable(smallMoleculeEvidence.
125                    getSpectraRef()).
126                    orElse(Collections.emptyList()).
127                    stream().
128                    map((spectraRef) ->
129                    {
130                        return Metadata.Properties.msRun.getPropertyName() + "[" + spectraRef.
131                            getMsRun().
132                            getId() + "]:" + spectraRef.getReference();
133                    }).
134                    collect(Collectors.toList()));
135            writeObject(columnFor(
136                SmallMoleculeEvidenceColumn.Stable.IDENTIFICATION_METHOD),
137                jg, sp, smallMoleculeEvidence.getIdentificationMethod());
138            writeObject(columnFor(SmallMoleculeEvidenceColumn.Stable.MS_LEVEL),
139                jg, sp,
140                smallMoleculeEvidence.
141                    getMsLevel());
142            Serializers.writeIndexedDoubles(
143                SmallMoleculeEvidence.Properties.idConfidenceMeasure.
144                    getPropertyName(), jg,
145                Optional.ofNullable(smallMoleculeEvidence.
146                    getIdConfidenceMeasure()).
147                    orElse(Collections.emptyList()));
148            writeNumber(columnFor(SmallMoleculeEvidenceColumn.Stable.RANK), jg,
149                smallMoleculeEvidence.getRank());
150            Serializers.writeOptColumnMappings(smallMoleculeEvidence.getOpt(),
151                jg, sp);
152            jg.writeEndObject();
153        } else {
154            log.debug(SmallMoleculeEvidence.class.getSimpleName() + " is null!");
155        }
156    }
157}