1 package uk.ac.ebi.intenz.tools.sib.helper;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.Set;
8 import java.util.TreeSet;
9
10 import uk.ac.ebi.biobabel.util.StringUtil;
11 import uk.ac.ebi.intenz.domain.constants.EnzymeNameQualifierConstant;
12 import uk.ac.ebi.intenz.domain.constants.EnzymeViewConstant;
13 import uk.ac.ebi.intenz.domain.constants.XrefDatabaseConstant;
14 import uk.ac.ebi.intenz.domain.enzyme.EnzymaticReactions;
15 import uk.ac.ebi.intenz.domain.enzyme.EnzymeComment;
16 import uk.ac.ebi.intenz.domain.enzyme.EnzymeLink;
17 import uk.ac.ebi.intenz.domain.enzyme.EnzymeName;
18 import uk.ac.ebi.intenz.domain.enzyme.EnzymeCommissionNumber.Type;
19 import uk.ac.ebi.intenz.domain.history.HistoryEvent;
20 import uk.ac.ebi.intenz.domain.history.HistoryNode;
21 import uk.ac.ebi.rhea.domain.Reaction;
22 import uk.ac.ebi.intenz.tools.sib.sptr_enzyme.EnzymeCrossReference;
23 import uk.ac.ebi.intenz.tools.sib.sptr_enzyme.EnzymeEntryImpl;
24 import uk.ac.ebi.intenz.tools.sib.sptr_enzyme.EnzymeXrefFactory;
25 import uk.ac.ebi.intenz.tools.sib.translator.XCharsASCIITranslator;
26 import uk.ac.ebi.intenz.tools.sib.writer.EnzymeFlatFileWriteException;
27 import uk.ac.ebi.interfaces.sptr.SPTRCrossReference;
28 import uk.ac.ebi.interfaces.sptr.SPTRException;
29 import uk.ac.ebi.xchars.SpecialCharacters;
30 import uk.ac.ebi.xchars.domain.EncodingType;
31
32
33
34
35
36
37
38 public class SibEntryHelper {
39
40
41
42 public static EnzymeEntryImpl getSibEnzymeEntry(uk.ac.ebi.intenz.domain.enzyme.EnzymeEntry enzymeEntry,
43 SpecialCharacters encoding, EncodingType encodingType) throws SPTRException {
44 final XCharsASCIITranslator translator = XCharsASCIITranslator.getInstance();
45 EnzymeEntryImpl sibEnzymeEntry = new EnzymeEntryImpl();
46 try {
47
48
49 if (enzymeEntry.getHistory().isDeletedRootNode() || enzymeEntry.getHistory().isTransferredRootNode())
50 return getDeletedTransferredSibEntry(enzymeEntry);
51
52
53 sibEnzymeEntry.setEC(enzymeEntry.getEc().toString());
54
55
56 List commonNames = enzymeEntry.getCommonNames();
57 for (int iii = 0; iii < commonNames.size(); iii++) {
58 EnzymeName commonName = (EnzymeName) commonNames.get(iii);
59 if (EnzymeViewConstant.isInSIBView(commonName.getView().toString())) {
60 sibEnzymeEntry.setCommonName(encoding.xml2Display(translator.toASCII(commonName.getName().trim(), false, true), encodingType));
61 break;
62 }
63 }
64
65
66
67 List synonyms = new ArrayList();
68 synonyms.addAll(enzymeEntry.getSynonyms());
69
70 synonyms.add(enzymeEntry.getSystematicName());
71
72 List synonymStringsTranslated = new ArrayList();
73 for (int iii = 0; iii < synonyms.size(); iii++) {
74 EnzymeName synonym = (EnzymeName) synonyms.get(iii);
75 if (EnzymeViewConstant.isInSIBView(synonym.getView().toString())) {
76 synonymStringsTranslated.add(encoding.xml2Display(translator.toASCII(synonym.getName().trim(), false, false), encodingType));
77 }
78 }
79
80 Collections.sort(synonymStringsTranslated, StringUtil.getStringReverseCasingComparator());
81 sibEnzymeEntry.setSynonyms((String[]) synonymStringsTranslated.toArray(sibEnzymeEntry.getSynonyms()));
82
83
84 EnzymaticReactions reactions = enzymeEntry.getEnzymaticReactions();
85 for (int iii = 0; iii < reactions.size(); iii++) {
86 Reaction reaction = reactions.getReaction(iii);
87
88 if (reaction.getId() > Reaction.NO_ID_ASSIGNED
89 && !enzymeEntry.getEc().getType().equals(Type.PRELIMINARY)
90
91 continue;
92 if (EnzymeViewConstant.isInSIBView(reactions.getReactionView(iii).toString())) {
93 String rtr = reaction.getTextualRepresentation().trim()
94 .replace(" <?> "," = ").replace(" => "," = ")
95 .replace(" <= "," = ").replace(" <=> "," = ");
96 sibEnzymeEntry.addReaction(encoding.xml2Display(translator.toASCII(rtr, true, false), encodingType));
97 }
98 }
99
100
101 Set<Object> cofactors = enzymeEntry.getCofactors();
102 List cofactorNames = new ArrayList();
103 StringBuilder cofactorString = new StringBuilder();
104 for (Object cofactor : cofactors) {
105 if (EnzymeViewConstant.isInView(EnzymeViewConstant.SIB, cofactor)){
106
107
108
109
110
111
112
113
114
115
116
117
118
119 String cfText = cofactor.toString()
120 .replaceAll(" OR\\d? ", " or ")
121 .replaceAll(" AND ", " and ")
122 .trim();
123 cofactorNames.add(encoding.xml2Display(translator.toASCII(cfText, false, false), encodingType));
124 }
125 }
126 Collections.sort(cofactorNames, StringUtil.getStringReverseCasingComparator());
127 for (int i = 0; i < cofactorNames.size(); i++){
128 cofactorString.append(cofactorNames.get(i));
129 if (i < cofactorNames.size()-1) cofactorString.append(";");
130 cofactorString.append(" ");
131 }
132 sibEnzymeEntry.setCofactors(cofactorString.toString().trim());
133
134
135 List comments = enzymeEntry.getComments();
136 StringBuilder commentString = new StringBuilder();
137 for (int iii = 0; iii < comments.size(); iii++) {
138 EnzymeComment comment = (EnzymeComment) comments.get(iii);
139 if (EnzymeViewConstant.isInSIBView(comment.getView().toString())) {
140 commentString.append(encoding.xml2Display(translator.toASCII(comment.getCommentText().trim(), false, false ), encodingType));
141 commentString.append(" ");
142 }
143 }
144 sibEnzymeEntry.setComment(commentString.toString().trim());
145
146
147 Set xrefs = enzymeEntry.getLinks();
148 for (Iterator it = xrefs.iterator(); it.hasNext();) {
149 EnzymeLink enzymeLink = (EnzymeLink) it.next();
150 if (enzymeLink.getXrefDatabaseConstant() == XrefDatabaseConstant.MIM &&
151 EnzymeViewConstant.isInSIBView(enzymeLink.getView().toString())) {
152 SPTRCrossReference DICrossReference = getDICrossReference(enzymeLink);
153 sibEnzymeEntry.addCrossReference(DICrossReference);
154 }
155 if (enzymeLink.getXrefDatabaseConstant() == XrefDatabaseConstant.PROSITE &&
156 EnzymeViewConstant.isInSIBView(enzymeLink.getView().toString())) {
157 SPTRCrossReference PRCrossReference = getPRCrossReference(enzymeLink);
158 sibEnzymeEntry.addCrossReference(PRCrossReference);
159 }
160 if (enzymeLink.getXrefDatabaseConstant() == XrefDatabaseConstant.SWISSPROT &&
161 EnzymeViewConstant.isInSIBView(enzymeLink.getView().toString())) {
162 SPTRCrossReference DRCrossReference = getDRCrossReference(enzymeLink);
163 sibEnzymeEntry.addCrossReference(DRCrossReference);
164 }
165 }
166 } catch (SPTRException e) {
167 throw new EnzymeFlatFileWriteException(e);
168 }
169
170 return sibEnzymeEntry;
171 }
172
173
174
175
176
177
178 private static String getSynonymQualifier(EnzymeNameQualifierConstant constant){
179 StringBuilder buffer = new StringBuilder("");
180 if(constant!=null){
181 String constantString = constant.toASCII(constant);
182 if( !StringUtil.isNullOrEmpty(constantString) ){
183 buffer.append(" ");
184 buffer.append(constantString);
185 }
186 }
187
188 return buffer.toString();
189 }
190
191 private static EnzymeEntryImpl getDeletedTransferredSibEntry(uk.ac.ebi.intenz.domain.enzyme.EnzymeEntry enzymeEntry) throws SPTRException {
192 assert enzymeEntry != null : "Parameter 'enzymeEntry' must not be null.";
193 EnzymeEntryImpl sibEnzymeEntry = new EnzymeEntryImpl();
194 sibEnzymeEntry.setTransferredOrDeleted(true);
195 sibEnzymeEntry.setEC(enzymeEntry.getEc().toString());
196 if (enzymeEntry.getHistory().isDeletedRootNode()) {
197 sibEnzymeEntry.setCommonName("Deleted entry");
198 } else {
199 HistoryNode rootNode = enzymeEntry.getHistory().getRootNode();
200 HistoryNode lastRootNode = ((HistoryEvent) new TreeSet(rootNode.getEdges()).last()).getAfterNode();
201 HistoryNode lastNode = null;
202 try {
203 lastNode = ((HistoryEvent) new TreeSet(lastRootNode.getEdges()).last()).getAfterNode();
204 if (lastNode != null){
205 lastRootNode = lastNode;
206 } else {
207
208 }
209 } catch (NullPointerException e){}
210 String commonName = "Transferred entry: " + lastRootNode.getEnzymeEntry().getEc().toString();
211 sibEnzymeEntry.setCommonName(XCharsASCIITranslator.getInstance().toASCII(commonName, false, true));
212
213
214 }
215 return sibEnzymeEntry;
216 }
217
218 private static SPTRCrossReference getDICrossReference(EnzymeLink enzymeLink) throws SPTRException {
219 SPTRCrossReference enzymeXref = null;
220 try {
221 enzymeXref = new EnzymeXrefFactory().newEnzymeCrossReference(EnzymeCrossReference.MIM);
222 enzymeXref.setAccessionNumber(enzymeLink.getAccession());
223 enzymeXref.setPropertyValue(EnzymeCrossReference.PROPERTY_DESCRIPTION, enzymeLink.getName().trim());
224 } catch (SPTRException e) {
225 throw new EnzymeFlatFileWriteException(e);
226 }
227 return enzymeXref;
228 }
229
230 private static SPTRCrossReference getPRCrossReference(EnzymeLink enzymeLink) throws SPTRException {
231 SPTRCrossReference enzymeXref = null;
232 try {
233 enzymeXref = new EnzymeXrefFactory().newEnzymeCrossReference(EnzymeCrossReference.PROSITE);
234 enzymeXref.setAccessionNumber(enzymeLink.getAccession().trim());
235 } catch (SPTRException e) {
236 throw new EnzymeFlatFileWriteException(e);
237 }
238 return enzymeXref;
239 }
240
241 private static SPTRCrossReference getDRCrossReference(EnzymeLink enzymeLink) throws SPTRException {
242 SPTRCrossReference enzymeXref = null;
243 try {
244 enzymeXref = new EnzymeXrefFactory().newEnzymeCrossReference(EnzymeCrossReference.SWISSPROT);
245 enzymeXref.setAccessionNumber(enzymeLink.getAccession().trim());
246 enzymeXref.setPropertyValue(EnzymeCrossReference.PROPERTY_DESCRIPTION, enzymeLink.getName().trim());
247 } catch (SPTRException e) {
248 throw new EnzymeFlatFileWriteException(e);
249 }
250 return enzymeXref;
251 }
252 }