1 package uk.ac.ebi.intenz.tools.sib.helper;
2
3 import java.util.Iterator;
4 import java.util.List;
5 import java.util.Set;
6
7 import org.apache.log4j.Logger;
8
9 import uk.ac.ebi.biobabel.util.collections.OperatorSet;
10 import uk.ac.ebi.intenz.domain.constants.EnzymeViewConstant;
11 import uk.ac.ebi.intenz.domain.constants.XrefDatabaseConstant;
12 import uk.ac.ebi.intenz.domain.enzyme.EnzymaticReactions;
13 import uk.ac.ebi.intenz.domain.enzyme.EnzymeComment;
14 import uk.ac.ebi.intenz.domain.enzyme.EnzymeEntry;
15 import uk.ac.ebi.intenz.domain.enzyme.EnzymeLink;
16 import uk.ac.ebi.intenz.domain.enzyme.EnzymeName;
17 import uk.ac.ebi.rhea.domain.Reaction;
18 import uk.ac.ebi.intenz.tools.sib.sptr_enzyme.EnzymeCrossReference;
19 import uk.ac.ebi.intenz.tools.sib.sptr_enzyme.EnzymeEntryImpl;
20 import uk.ac.ebi.intenz.tools.sib.sptr_enzyme.EnzymeXrefFactory;
21 import uk.ac.ebi.intenz.tools.sib.translator.XCharsASCIITranslator;
22 import uk.ac.ebi.intenz.tools.sib.writer.EnzymeFlatFileWriteException;
23 import uk.ac.ebi.interfaces.sptr.SPTRCrossReference;
24 import uk.ac.ebi.interfaces.sptr.SPTRException;
25 import uk.ac.ebi.xchars.SpecialCharacters;
26 import uk.ac.ebi.xchars.domain.EncodingType;
27
28
29
30
31
32
33
34
35
36 public class EnzymeHelper {
37
38 private static final Logger LOGGER =
39 Logger.getLogger(EnzymeHelper.class.getName());
40
41
42
43
44
45
46
47
48
49
50 public static EnzymeEntryImpl getSibEnzymeEntry(EnzymeEntry enzymeEntry,
51 SpecialCharacters encoding, EncodingType encodingType,
52 boolean translate) throws SPTRException {
53 final XCharsASCIITranslator translator = XCharsASCIITranslator.getInstance();
54
55 EnzymeEntryImpl sibEnzymeEntry = new EnzymeEntryImpl();
56 try {
57
58 if (enzymeEntry.getHistory().isDeletedRootNode() || enzymeEntry.getHistory().isTransferredRootNode())
59 return getDeletedTransferredSibEntry(enzymeEntry);
60
61
62 sibEnzymeEntry.setEC(enzymeEntry.getEc().toString());
63
64
65 List commonNames = enzymeEntry.getCommonNames();
66 boolean hasSibCommonName = false;
67 for (int iii = 0; iii < commonNames.size(); iii++) {
68 EnzymeName commonName = (EnzymeName) commonNames.get(iii);
69 if (EnzymeViewConstant.isInSIBView(commonName.getView().toString())) {
70 hasSibCommonName = true;
71 if (translate)
72 sibEnzymeEntry.setCommonName(encoding.xml2Display(translator.toASCII(commonName.getName().trim(), false, true), encodingType));
73 else
74 sibEnzymeEntry.setCommonName(encoding.xml2Display(commonName.getName().toString().trim(), encodingType));
75 break;
76 }
77 }
78 if(!hasSibCommonName) {
79 LOGGER.error("EC "+enzymeEntry.getEc().toString()+" does not have a SIB common name.");
80 sibEnzymeEntry.setCommonName("--error--");
81 }
82
83
84 List synonyms = enzymeEntry.getSynonyms();
85 for (int iii = 0; iii < synonyms.size(); iii++) {
86 EnzymeName synonym = (EnzymeName) synonyms.get(iii);
87 if (EnzymeViewConstant.isInSIBView(synonym.getView().toString())) {
88 if (translate)
89 sibEnzymeEntry.addSynonym(encoding.xml2Display(translator.toASCII(synonym.getName().trim(), false, false), encodingType));
90 else
91 sibEnzymeEntry.addSynonym(encoding.xml2Display(synonym.getName().trim(), encodingType));
92 }
93 }
94
95
96 EnzymaticReactions reactions = enzymeEntry.getEnzymaticReactions();
97 for (int iii = 0; iii < reactions.size(); iii++) {
98 Reaction reaction = reactions.getReaction(iii);
99 if (EnzymeViewConstant.isInSIBView(reactions.getReactionView(iii).toString())) {
100 if (translate)
101 sibEnzymeEntry.addReaction(encoding.xml2Display(translator.toASCII(reaction.getTextualRepresentation().trim(), true, false), encodingType));
102 else
103 sibEnzymeEntry.addReaction(encoding.xml2Display(reaction.getTextualRepresentation().trim(), encodingType));
104 }
105 }
106
107
108 Set<Object> cofactors = enzymeEntry.getCofactors();
109 StringBuffer cofactorString = new StringBuffer();
110 for (Iterator it = cofactors.iterator(); it.hasNext();) {
111 Object cofactor = it.next();
112 if (EnzymeViewConstant.isInView(EnzymeViewConstant.SIB, cofactor)) {
113 String s = (cofactor instanceof OperatorSet)?
114 ((OperatorSet) cofactor).toString(true, true) :
115 cofactor.toString();
116 cofactorString.append(encoding.xml2Display(translator.toASCII(s, false, false), encodingType));
117 if (cofactorString.length() > 0 && it.hasNext())
118 cofactorString.append(";");
119 cofactorString.append(" ");
120 }
121 }
122
123
124 List comments = enzymeEntry.getComments();
125 StringBuffer commentString = new StringBuffer();
126 for (int iii = 0; iii < comments.size(); iii++) {
127 EnzymeComment comment = (EnzymeComment) comments.get(iii);
128 if (EnzymeViewConstant.isInSIBView(comment.getView().toString())) {
129 commentString.append(encoding.xml2Display(translator.toASCII(comment.getCommentText().trim(), false, false), encodingType));
130 commentString.append(" ");
131 }
132 }
133 sibEnzymeEntry.setComment(commentString.toString().trim());
134
135
136 Set xrefs = enzymeEntry.getLinks();
137 for (Iterator it = xrefs.iterator(); it.hasNext();) {
138 EnzymeLink enzymeLink = (EnzymeLink) it.next();
139 if (enzymeLink.getXrefDatabaseConstant() == XrefDatabaseConstant.MIM &&
140 EnzymeViewConstant.isInSIBView(enzymeLink.getView().toString())) {
141 SPTRCrossReference DICrossReference = getDICrossReference(enzymeLink);
142 sibEnzymeEntry.addCrossReference(DICrossReference);
143 }
144 if (enzymeLink.getXrefDatabaseConstant() == XrefDatabaseConstant.PROSITE &&
145 EnzymeViewConstant.isInSIBView(enzymeLink.getView().toString())) {
146 SPTRCrossReference PRCrossReference = getPRCrossReference(enzymeLink);
147 sibEnzymeEntry.addCrossReference(PRCrossReference);
148 }
149 if (enzymeLink.getXrefDatabaseConstant() == XrefDatabaseConstant.SWISSPROT &&
150 EnzymeViewConstant.isInSIBView(enzymeLink.getView().toString())) {
151 SPTRCrossReference DRCrossReference = getDRCrossReference(enzymeLink);
152 sibEnzymeEntry.addCrossReference(DRCrossReference);
153 }
154 }
155 } catch (SPTRException e) {
156 throw new EnzymeFlatFileWriteException(e);
157 }
158
159 return sibEnzymeEntry;
160 }
161
162
163
164 private static EnzymeEntryImpl getDeletedTransferredSibEntry(EnzymeEntry enzymeEntry) throws SPTRException {
165 assert enzymeEntry != null : "Parameter 'enzymeEntry' must not be null.";
166 EnzymeEntryImpl sibEnzymeEntry = new EnzymeEntryImpl();
167 sibEnzymeEntry.setTransferredOrDeleted(true);
168 sibEnzymeEntry.setEC(enzymeEntry.getEc().toString());
169 if (enzymeEntry.getHistory().isDeletedRootNode()) {
170 sibEnzymeEntry.setCommonName("Deleted entry");
171 } else {
172 sibEnzymeEntry.setCommonName("Transferred entry: " +
173 enzymeEntry.getHistory().getLatestHistoryEventOfRoot().getAfterNode().getEnzymeEntry().getEc().toString());
174 }
175 return sibEnzymeEntry;
176 }
177
178 private static SPTRCrossReference getDICrossReference(EnzymeLink enzymeLink) throws SPTRException {
179 SPTRCrossReference enzymeXref = null;
180 try {
181 enzymeXref = new EnzymeXrefFactory().newEnzymeCrossReference(EnzymeCrossReference.MIM);
182 enzymeXref.setAccessionNumber(enzymeLink.getAccession());
183 enzymeXref.setPropertyValue(EnzymeCrossReference.PROPERTY_DESCRIPTION, enzymeLink.getName());
184 } catch (SPTRException e) {
185 throw new EnzymeFlatFileWriteException(e);
186 }
187 return enzymeXref;
188 }
189
190 private static SPTRCrossReference getPRCrossReference(EnzymeLink enzymeLink) throws SPTRException {
191 SPTRCrossReference enzymeXref = null;
192 try {
193 enzymeXref = new EnzymeXrefFactory().newEnzymeCrossReference(EnzymeCrossReference.PROSITE);
194 enzymeXref.setAccessionNumber(enzymeLink.getAccession());
195 } catch (SPTRException e) {
196 throw new EnzymeFlatFileWriteException(e);
197 }
198 return enzymeXref;
199 }
200
201 private static SPTRCrossReference getDRCrossReference(EnzymeLink enzymeLink) throws SPTRException {
202 SPTRCrossReference enzymeXref = null;
203 try {
204 enzymeXref = new EnzymeXrefFactory().newEnzymeCrossReference(EnzymeCrossReference.SWISSPROT);
205 enzymeXref.setAccessionNumber(enzymeLink.getAccession());
206 enzymeXref.setPropertyValue(EnzymeCrossReference.PROPERTY_DESCRIPTION, enzymeLink.getName());
207 } catch (SPTRException e) {
208 throw new EnzymeFlatFileWriteException(e);
209 }
210 return enzymeXref;
211 }
212
213 }