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 Optional<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.map((lipidClass) -> {
047            return lipidClass.getCategory();
048        }).orElse(LipidCategory.UNDEFINED);
049    }
050
051    /**
052     * Creates a new head group from the given head group name and optionally a
053     * lipid class. The lipid class also determines the category.
054     *
055     * @param rawName the lipid head gruop string.
056     * @param lipidClass the lipid class.
057     */
058    public HeadGroup(String rawName, Optional<LipidClass> lipidClass) {
059        this.rawName = rawName;
060        this.name = rawName.trim().replaceAll(" O", "");
061        this.lipidClass = lipidClass;
062        this.lipidCategory = this.lipidClass.map((myLipidClass) -> {
063            return myLipidClass.getCategory();
064        }).orElse(LipidCategory.UNDEFINED);
065    }
066
067    /**
068     * Returns a lipid string representation for the head group of this lipid.
069     * This method normalizes the original head group name to the class specific
070     * primary alias, if the level and class are known. E.g. TG is normalized to
071     * TAG.
072     *
073     * @return the normalized lipid head group.
074     */
075    public String getNormalizedName() {
076        if (lipidClass.isPresent()) {
077            return lipidClass.get().getSynonyms().get(0);
078        }
079        return name;
080    }
081
082    @Override
083    public String toString() {
084        return this.name;
085    }
086}