001/* 002 * 003 */ 004package de.isas.lipidomics.domain; 005 006import de.isas.lipidomics.palinom.exceptions.ParsingException; 007import java.util.Optional; 008import lombok.AllArgsConstructor; 009import lombok.Data; 010 011/** 012 * An adduct, generally, consists of a sum formula part, an adduct string, the 013 * charge and the charge sign. An example for a valid adduct is : [M+H]1+. 014 * 015 * @author nils.hoffmann 016 */ 017@AllArgsConstructor 018@Data 019public class Adduct { 020 021 private static final class None extends Adduct { 022 023 private None() { 024 super("", "", 0, 0); 025 } 026 } 027 028 public static final Adduct NONE = new None(); 029 030 private final String sumFormula; 031 private final String adductString; 032 private final Integer positiveElementaryCharge; 033 private final Integer chargeSign; 034 035 public Adduct() { 036 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 037 } 038 039 public String getLipidString() { 040 if (adductString == null || adductString.isEmpty()) { 041 return ""; 042 } 043 if (positiveElementaryCharge == 0) { 044 return "[M]"; 045 } 046 StringBuilder sb = new StringBuilder(); 047 sb.append("[M").append(sumFormula).append(adductString).append("]").append(positiveElementaryCharge).append((chargeSign > 0) ? "+" : ((chargeSign < 0) ? "-" : "")); 048 return sb.toString(); 049 } 050 051 public ElementTable getElements() { 052 ElementTable elements = new ElementTable(); 053 String adductName = Optional.ofNullable(adductString).map((t) -> { 054 return t.length() > 1 ? t.substring(1) : ""; 055 }).orElse(""); 056 try { 057 elements.add(new ElementTable(adductName)); 058 } catch (ParsingException ex) { 059 return elements; 060 } 061 if (adductString.length() > 0 && adductString.startsWith("-")) { 062 elements.keySet().stream().forEach((t) -> { 063 elements.negate(t); 064 }); 065 } 066 return elements; 067 } 068 069 /** 070 * Returns the positive elementary charge times the charge sign. 071 * 072 * @return the net charge. 073 */ 074 public int getCharge() { 075 return positiveElementaryCharge * chargeSign; 076 } 077 078}