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