001/*
002 * Copyright 2020  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 java.util.Optional;
019import lombok.Data;
020
021/**
022 * This class represents functional head groups of lipids. This is where the
023 * association to {@link LipidClass} and {@link LipidCategory} is maintained.
024 *
025 * @author nils.hoffmann
026 */
027@Data
028public class HeadGroup {
029
030    private final String name;
031    private final String rawName;
032    private final LipidClass lipidClass;
033    private final LipidCategory lipidCategory;
034
035    /**
036     * Creates a new head group from the given head group name. Lipid class and
037     * category will be looked up from {@link LipidClass#forHeadGroup(java.lang.String)
038     * }.
039     *
040     * @param rawName the lipid head group string.
041     */
042    public HeadGroup(String rawName) {
043        this.rawName = rawName;
044        this.name = rawName.trim().replaceAll(" O", "");
045        this.lipidClass = LipidClass.forHeadGroup(this.name);
046        this.lipidCategory = this.lipidClass.getCategory();
047    }
048
049    /**
050     * Creates a new head group from the given head group name and optionally a
051     * lipid class. The lipid class also determines the category.
052     *
053     * @param rawName the lipid head gruop string.
054     * @param lipidClass the lipid class.
055     */
056    public HeadGroup(String rawName, Optional<LipidClass> lipidClass) {
057        this.rawName = rawName;
058        this.name = rawName.trim().replaceAll(" O", "");
059        this.lipidClass = lipidClass.orElse(LipidClass.UNDEFINED);
060        this.lipidCategory = this.lipidClass.getCategory();
061    }
062
063    /**
064     * Returns a lipid string representation for the head group of this lipid.
065     * This method normalizes the original head group name to the class specific
066     * primary alias, if the level and class are known. E.g. TG is normalized to
067     * TAG.
068     *
069     * @return the normalized lipid head group.
070     */
071    public String getNormalizedName() {
072        if (lipidClass == LipidClass.UNDEFINED) {
073            return name;
074        }
075        return lipidClass.getSynonyms().get(0);
076    }
077
078    @Override
079    public String toString() {
080        return this.name;
081    }
082}