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