001/*
002 * Copyright 2019 nils.hoffmann.
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.lipidomics.domain;
017
018import de.isas.lipidomics.palinom.exceptions.ConstraintViolationException;
019import java.util.Optional;
020import lombok.Builder;
021import lombok.Data;
022import lombok.EqualsAndHashCode;
023
024/**
025 * A structural subspecies. Child of LipidMolecularSubspecies. Individual FAs
026 * and their sn positions are known, but not the double bond positions.
027 *
028 * Example: Phosphatidylinositol (8:0/8:0) or PI(8:0/8:0)
029 *
030 * @author nils.hoffmann
031 * @see LipidMolecularSubspecies
032 */
033@Data
034@EqualsAndHashCode(callSuper = true)
035public class LipidStructuralSubspecies extends LipidMolecularSubspecies {
036
037    @Builder(builderMethodName = "lipidStructuralSubspeciesBuilder")
038    public LipidStructuralSubspecies(HeadGroup headGroup, FattyAcid... fa) {
039        super(headGroup);
040        int nCarbon = 0;
041        int nHydroxyl = 0;
042        int nDoubleBonds = 0;
043        ModificationsList mods = new ModificationsList();
044        for (FattyAcid fas : fa) {
045            if (super.fa.containsKey(fas.getName())) {
046                throw new ConstraintViolationException(
047                        "FA names must be unique! FA with name " + fas.getName() + " was already added!");
048            } else {
049                super.fa.put(fas.getName(), fas);
050                nCarbon += fas.getNCarbon();
051                nHydroxyl += fas.getNHydroxy();
052                nDoubleBonds += fas.getNDoubleBonds();
053                mods.addAll(fas.getModifications());
054            }
055        }
056        super.info = Optional.of(LipidSpeciesInfo.lipidSpeciesInfoBuilder().
057                level(LipidLevel.STRUCTURAL_SUBSPECIES).
058                name(headGroup.getName()).
059                position(-1).
060                nCarbon(nCarbon).
061                nHydroxy(nHydroxyl).
062                nDoubleBonds(nDoubleBonds).
063                lipidFaBondType(LipidFaBondType.getLipidFaBondType(headGroup, fa)).
064                modifications(mods).
065                build()
066        );
067    }
068
069    @Override
070    public String getLipidString(LipidLevel level) {
071        return this.getLipidString(level, false);
072    }
073
074    @Override
075    public String getLipidString(LipidLevel level, boolean normalizeHeadGroup) {
076        String headGroup = normalizeHeadGroup ? getNormalizedHeadGroup() : getHeadGroup().getName();
077        switch (level) {
078            case STRUCTURAL_SUBSPECIES:
079                return super.buildLipidSubspeciesName(level, "/", headGroup, normalizeHeadGroup);
080            case MOLECULAR_SUBSPECIES:
081            case CATEGORY:
082            case CLASS:
083            case SPECIES:
084                return super.getLipidString(level, normalizeHeadGroup);
085            default:
086                LipidLevel thisLevel = getInfo().orElse(LipidSpeciesInfo.NONE).getLevel();
087                throw new ConstraintViolationException(getClass().getSimpleName() + " can not create a normalized string for lipid with level " + thisLevel + " for level " + level + ": target level is more specific than this lipid's level!");
088        }
089    }
090
091    @Override
092    public String getNormalizedLipidString() {
093        return getLipidString(getInfo().orElse(LipidSpeciesInfo.NONE).getLevel(), true);
094    }
095
096    @Override
097    public String toString() {
098        return getLipidString();
099    }
100}