LipidMolecularSpecies.java

  1. /*
  2.  * Copyright 2021 Dominik Kopczynski, Nils Hoffmann.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package org.lifstools.jgoslin.domain;

  17. import com.fasterxml.jackson.annotation.JsonIgnore;
  18. import java.util.Collection;
  19. import java.util.Collections;

  20. /**
  21.  * Molecular species level according to the 2020 update of the Liebisch shorthand
  22.  * nomenclature.
  23.  * @author Dominik Kopczynski
  24.  * @author Nils Hoffmann
  25.  * @see LipidLevel
  26.  */
  27. public class LipidMolecularSpecies extends LipidSpecies {

  28.     public LipidMolecularSpecies(Headgroup _headgroup, KnownFunctionalGroups knownFunctionalGroups) {
  29.         this(_headgroup, Collections.emptyList(), knownFunctionalGroups);
  30.     }

  31.     public LipidMolecularSpecies(Headgroup _headgroup, Collection<FattyAcid> _fa, KnownFunctionalGroups knownFunctionalGroups) {
  32.         super(_headgroup, _fa, knownFunctionalGroups);
  33.         info.setLevel(LipidLevel.MOLECULAR_SPECIES);
  34.         for (FattyAcid fatty_acid : _fa){
  35.             if (fa.containsKey(fatty_acid.getName())) {
  36.                 throw new ConstraintViolationException("FA names must be unique! FA with name " + fatty_acid.getName() + " was already added!");
  37.             }
  38.             fa.put(fatty_acid.getName(), fatty_acid);
  39.             faList.add(fatty_acid);
  40.         }
  41.        

  42.         // add 0:0 dummys
  43.         for (int i = _fa.size(); i < info.totalFa; ++i) {
  44.             FattyAcid fatty_acid = new FattyAcid("FA" + Integer.toString(i + 1), knownFunctionalGroups);
  45.             fatty_acid.position = -1;
  46.             info.add(fatty_acid);
  47.             fa.put(fatty_acid.getName(), fatty_acid);
  48.             faList.add(fatty_acid);
  49.         }
  50.     }

  51.     public String buildLipidSubspeciesName() {
  52.         return buildLipidSubspeciesName(LipidLevel.NO_LEVEL);
  53.     }

  54.     public String buildLipidSubspeciesName(LipidLevel level) {
  55.         if (level == LipidLevel.NO_LEVEL) {
  56.             level = LipidLevel.MOLECULAR_SPECIES;
  57.         }

  58.         String fa_separator = (level != LipidLevel.MOLECULAR_SPECIES || headGroup.getLipidCategory() == LipidCategory.SP) ? "/" : "_";
  59.         StringBuilder lipid_name = new StringBuilder();
  60.         lipid_name.append(headGroup.getLipidString(level));

  61.         String fa_headgroup_separator = (headGroup.getLipidCategory() != LipidCategory.ST) ? " " : "/";

  62.         switch (level) {
  63.             case COMPLETE_STRUCTURE, FULL_STRUCTURE, STRUCTURE_DEFINED, SN_POSITION -> {
  64.                 if (faList.size() > 0) {
  65.                     lipid_name.append(fa_headgroup_separator);
  66.                     int i = 0;

  67.                     for (FattyAcid fatty_acid : faList) {
  68.                         if (i++ > 0) {
  69.                             lipid_name.append(fa_separator);
  70.                         }
  71.                         lipid_name.append(fatty_acid.toString(level));
  72.                     }
  73.                 }
  74.             }
  75.             default -> {
  76.                 boolean go_on = false;
  77.                 for (FattyAcid fatty_acid : faList) {
  78.                     if (fatty_acid.numCarbon > 0) {
  79.                         go_on = true;
  80.                         break;
  81.                     }
  82.                 }

  83.                 if (go_on) {
  84.                     lipid_name.append(fa_headgroup_separator);
  85.                     int i = 0;
  86.                     for (FattyAcid fatty_acid : faList) {
  87.                         if (fatty_acid.numCarbon > 0) {
  88.                             if (i++ > 0) {
  89.                                 lipid_name.append(fa_separator);
  90.                             }
  91.                             lipid_name.append(fatty_acid.toString(level));
  92.                         }
  93.                     }
  94.                 }
  95.             }
  96.         }
  97.         return lipid_name.toString();
  98.     }

  99.     @Override
  100.     public LipidLevel getLipidLevel() {
  101.         return LipidLevel.MOLECULAR_SPECIES;
  102.     }

  103.     @JsonIgnore
  104.     @Override
  105.     public ElementTable getElements() {
  106.         ElementTable elements = headGroup.getElements();

  107.         // add elements from all fatty acyl chains
  108.         faList.forEach(fatty_acid -> {
  109.             elements.add(fatty_acid.computeAndCopyElements());
  110.         });

  111.         return elements;
  112.     }

  113.     @Override
  114.     public String getLipidString() {
  115.         return getLipidString(LipidLevel.NO_LEVEL);

  116.     }

  117.     @Override
  118.     public String getLipidString(LipidLevel level) {
  119.         switch (level) {
  120.             case NO_LEVEL, MOLECULAR_SPECIES -> {
  121.                 return buildLipidSubspeciesName(LipidLevel.MOLECULAR_SPECIES);
  122.             }
  123.             case CATEGORY, CLASS, SPECIES -> {
  124.                 return super.getLipidString(level);
  125.             }

  126.             default -> throw new IllegalArgumentException("LipidMolecularSpecies does not know how to create a lipid string for level " + level.toString());
  127.         }
  128.     }
  129. }