Debian Codegraph @

jna-rinchi-core/src/main/java/io/github/dan2097/jnarinchi/RinchiInputComponent.java

raw
1 /**
2 * JNA-RInChI - Library for calling RInChI from Java
3 * Copyright © 2022 Nikolay Kochev
4 * <p>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 * <p>
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 * <p>
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 package io.github.dan2097.jnarinchi;
19
20 import io.github.dan2097.jnainchi.*;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 /**
26 * This class models a particular component of a reaction.
27 * <p>
28 * It inherits from {@link InchiInput} which stores the input for InChI generation
29 * and uses {@link InchiAtom atom}, @link InchiBond bond} and {@link InchiStereo}
30 * objects to represent molecular structures.
31 * <br>
32 * RinchiInputComponent adds a {@link ReactionComponentRole component role} and the
33 * ability to convert an instance to a String representation.
34 * </p>
35 *
36 * @author Nikolay Kochev
37 */
38 public class RinchiInputComponent extends InchiInput {
39 private ReactionComponentRole role = ReactionComponentRole.REAGENT;
40
41 public ReactionComponentRole getRole() {
42 return role;
43 }
44
45 public void setRole(ReactionComponentRole role) {
46 this.role = role;
47 }
48
49 /**
50 * Returns a list of atoms that are connected to <code>atom</code>.
51 *
52 * @param atom adjacent atoms to this atom are returned in a list
53 * @return list of atoms connected to <code>atom</code> or null if <code>atom</code> equals <code>null</code>
54 */
55 public List<InchiAtom> getConnectedAtomList(InchiAtom atom) {
56 if (atom == null) {
57 return null;
58 }
59 List<InchiAtom> list = new ArrayList<>();
60
61 for (int i = 0; i < getBonds().size(); i++) {
62 InchiBond bond = getBond(i);
63 if (bond.getStart() == atom)
64 list.add(bond.getEnd());
65 else if (bond.getEnd() == atom)
66 list.add(bond.getStart());
67 }
68
69 return list;
70 }
71
72 /**
73 * Returns the bond that connects <code>atom1</code> and <code>atom2</code>.
74 *
75 * @param atom1 the first atom participating in the bond to be returned
76 * @param atom2 the second atom participating in the bond to be returned
77 * @return the bond that connects <code>atom1</code> and <code>atom2</code> or <code>null</code> if no such bond exists
78 */
79 public InchiBond getBond(InchiAtom atom1, InchiAtom atom2) {
80 for (int i = 0; i < getBonds().size(); i++) {
81 InchiBond bond = getBond(i);
82 if ((bond.getStart() == atom1 && bond.getEnd() == atom2) || (bond.getStart() == atom2 && bond.getEnd() == atom1)) {
83 return bond;
84 }
85 }
86
87 return null;
88 }
89
90 @Override
91 public String toString() {
92 StringBuilder sb = new StringBuilder();
93 sb.append(" Atoms:").append("\n");
94 for (int i = 0; i < getAtoms().size(); i++)
95 sb.append(" ").append(atomToString(i)).append("\n");
96
97 if (!getBonds().isEmpty())
98 sb.append(" Bonds:").append("\n");
99 for (int i = 0; i < getBonds().size(); i++)
100 sb.append(" ").append(bondToString(i)).append("\n");
101
102 if (!getStereos().isEmpty())
103 sb.append(" Stereos:").append("\n");
104 for (int i = 0; i < getStereos().size(); i++)
105 sb.append(" ").append(stereoToString(i)).append("\n");
106
107 return sb.toString();
108 }
109
110 private String atomToString(int i) {
111 InchiAtom atom = getAtom(i);
112 StringBuilder stringBuilder = new StringBuilder();
113 stringBuilder.append(i + 1).append(" ").append(atom.getElName());
114 if (atom.getImplicitHydrogen() != 0)
115 stringBuilder.append(" H").append(atom.getImplicitHydrogen());
116 else
117 stringBuilder.append(" ");
118
119 stringBuilder.append(" ").append(atom.getX()).append(" ").append(atom.getY()).append(" ").append(atom.getZ());
120
121 if (atom.getCharge() != 0)
122 stringBuilder.append(" ").append(atom.getCharge());
123 if (atom.getIsotopicMass() != 0)
124 stringBuilder.append(" iso ").append(atom.getIsotopicMass());
125 if (atom.getRadical() != InchiRadical.NONE)
126 stringBuilder.append(" radical ").append(atom.getRadical());
127
128 return stringBuilder.toString();
129 }
130
131 private String bondToString(int i) {
132 InchiBond bond = getBond(i);
133 int atomIndex1 = getAtoms().indexOf(bond.getStart()) + 1;
134 int atomIndex2 = getAtoms().indexOf(bond.getEnd()) + 1;
135
136 StringBuilder stringBuilder = new StringBuilder();
137 stringBuilder.append(atomIndex1).append(" ").append(atomIndex2).append(" ").append(bond.getType());
138 if (bond.getStereo() != InchiBondStereo.NONE)
139 stringBuilder.append(" stereo ").append(bond.getStereo());
140
141 return stringBuilder.toString();
142 }
143
144 private String stereoToString(int atIndex) {
145 InchiStereo stereo = getStereos().get(atIndex);
146 StringBuilder stringBuilder = new StringBuilder();
147
148 stringBuilder.append(stereo.getType().toString()).append(": ");
149
150 switch (stereo.getType()) {
151 case Tetrahedral:
152 stringBuilder.append("center ").append(getAtoms().indexOf(stereo.getCentralAtom()) + 1);
153 stringBuilder.append(" ligands");
154 for (int i = 0; i < 4; i++)
155 stringBuilder.append(" ").append(getAtoms().indexOf(stereo.getAtoms()[i]) + 1);
156 break;
157 case DoubleBond:
158 stringBuilder.append(" ");
159 stringBuilder.append(getAtoms().indexOf(stereo.getAtoms()[0]) + 1).append(" - ");
160 stringBuilder.append(getAtoms().indexOf(stereo.getAtoms()[1]) + 1).append(" = ");
161 stringBuilder.append(getAtoms().indexOf(stereo.getAtoms()[2]) + 1).append(" - ");
162 stringBuilder.append(getAtoms().indexOf(stereo.getAtoms()[3]) + 1);
163 break;
164 case Allene:
165 stringBuilder.append(" ");
166 stringBuilder.append(getAtoms().indexOf(stereo.getAtoms()[0]) + 1).append(" - ");
167 stringBuilder.append(getAtoms().indexOf(stereo.getAtoms()[1]) + 1).append(" = ");
168 stringBuilder.append(getAtoms().indexOf(stereo.getCentralAtom()) + 1).append(" = ");
169 stringBuilder.append(getAtoms().indexOf(stereo.getAtoms()[2]) + 1).append(" - ");
170 stringBuilder.append(getAtoms().indexOf(stereo.getAtoms()[3]) + 1);
171 break;
172 case None:
173 break;
174 }
175
176 stringBuilder.append(" ").append(stereo.getParity());
177
178 return stringBuilder.toString();
179 }
180 }