2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.ws.jws2.jabaws2;
23 import jalview.api.FeatureColourI;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.Annotation;
27 import jalview.datamodel.features.FeatureMatcherSetI;
28 import jalview.util.MessageManager;
29 import jalview.ws.uimodel.AlignAnalysisUIText;
31 import java.text.MessageFormat;
32 import java.util.ArrayList;
33 import java.util.LinkedHashMap;
34 import java.util.List;
36 import java.util.TreeSet;
37 import java.util.regex.Pattern;
39 import compbio.data.sequence.RNAStructReader.AlifoldResult;
40 import compbio.data.sequence.RNAStructScoreManager;
41 import compbio.data.sequence.Range;
42 import compbio.data.sequence.Score;
45 * Client for the JABA RNA Alifold Service
47 * @author daluke - Daniel Barton
51 public class RNAalifoldClient extends JabawsAnnotationInstance
57 public String getCalcId()
62 private static String CALC_ID = "jalview.ws.jws2.RNAalifoldClient";
64 public static AlignAnalysisUIText getAlignAnalysisUIText()
66 return new AlignAnalysisUIText(
67 compbio.ws.client.Services.RNAalifoldWS.toString(),
68 jalview.ws.jws2.jabaws2.RNAalifoldClient.class, CALC_ID, true,
69 false, true, true, false, 2,
70 MessageManager.getString("label.rnalifold_calculations"),
71 MessageManager.getString("tooltip.rnalifold_calculations"),
72 MessageManager.getString("label.rnalifold_settings"),
73 MessageManager.getString("tooltip.rnalifold_settings"));
76 public static String getServiceActionText()
78 return "Submitting RNA alignment for Secondary Structure prediction using "
79 + "RNAalifold Service";
84 public RNAalifoldClient(Jws2Instance handle)
90 List<AlignmentAnnotation> annotationFromScoreManager(AlignmentI seqs,
91 Map<String, FeatureColourI> featureColours,
92 Map<String, FeatureMatcherSetI> featureFilters)
94 List<AlignmentAnnotation> ourAnnot = new ArrayList<>();
96 // Unpack the ScoreManager
97 List<String> structs = ((RNAStructScoreManager) scoremanager)
99 List<TreeSet<Score>> data = ((RNAStructScoreManager) scoremanager)
102 // test to see if this data object contains base pair contacts
103 Score fscore = data.get(0).first();
104 boolean bpScores = (fscore.getMethod()
105 .equals(AlifoldResult.contactProbabilities.toString()));
107 // add annotation for the consensus sequence alignment
108 createAnnotationRowforScoreHolder(seqs, null,
110 getCalcId(), structs.get(0), null, null);
112 // Add annotations for the mfe Structure
113 createAnnotationRowforScoreHolder(seqs, null,
115 getCalcId(), structs.get(1), data.get(1), null);
117 // decide whether to add base pair contact probability histogram
121 createAnnotationRowforScoreHolder(seqs, null,
123 getCalcId(), structs.get(2), data.get(0), data.get(2));
127 // Now loop for the rest of the Annotations (if there it isn't stochastic
129 // only the centroid and MEA structures remain anyway)
130 for (int i = count; i < structs.size(); i++)
132 // The ensemble values should be displayed in the description of the
133 // first (or all?) Stochastic Backtrack Structures.
134 if (!data.get(i).first().getMethod()
135 .equals(AlifoldResult.ensembleValues.toString()))
138 createAnnotationRowforScoreHolder(seqs, null, ourAnnot,
139 getCalcId(), structs.get(i), data.get(i), null);
145 private static void createAnnotationRowforScoreHolder(
146 AlignmentI alignment, boolean[] gapMap,
147 List<AlignmentAnnotation> ourAnnot, String calcId, String struct,
148 TreeSet<Score> data, TreeSet<Score> descriptionData)
151 * If contactProbability information is returned from RNAalifold it is
152 * stored in the first TreeSet<Score> object corresponding to the String Id
153 * which holds the consensus alignment. The method enumeration is then
154 * updated to AlifoldResult.contactProbabilties. This line recreates the
155 * same data object as was overwritten with the contact probabilites data.
159 data = compbio.data.sequence.RNAStructReader
160 .newEmptyScore(AlifoldResult.consensusAlignment);
163 if (descriptionData == null)
165 descriptionData = data;
168 String[] typenameAndDescription = constructTypenameAndDescription(
169 descriptionData.first());
170 String typename = typenameAndDescription[0];
171 String description = typenameAndDescription[1];
173 AlignmentAnnotation annotation = alignment
174 .findOrCreateAnnotation(typename, calcId, false, null, null);
176 constructAnnotationFromScoreHolder(gapMap, annotation, struct, data);
179 * update annotation description with the free Energy, frequency in ensemble
180 * or other data where appropriate.
182 * Doesnt deal with AlifoldResult.ensembleValues, the free energy of
183 * ensemble and frequency of mfe structure in ensemble. How to deal with
186 annotation.description = description;
188 annotation.belowAlignment = false;
189 // annotation.showAllColLabels = true;
190 annotation.validateRangeAndDisplay();
191 ourAnnot.add(annotation);
194 private static AlignmentAnnotation constructAnnotationFromScoreHolder(
195 boolean[] gapMap, AlignmentAnnotation annotation, String struct,
198 Annotation[] anns = new Annotation[gapMap != null ? gapMap.length + 1
201 if (data != null && data.size() > 1 && data.first().getMethod()
202 .equals(AlifoldResult.contactProbabilities.toString()))
205 // The base pair probabilities are stored in a set in scoreholder. we want
207 LinkedHashMap<Range, Float> basePairs = new LinkedHashMap<>();
208 for (Score score : data)
210 // The Score objects contain a set of size one containing the range and
211 // an ArrayList<float> of size one containing the probabilty
212 basePairs.put(score.getRanges().first(),
213 Float.valueOf(score.getScores().get(0)));
216 for (int i = 0, ri = 0, iEnd = struct.length(); i < iEnd; i++, ri++)
220 // skip any gapped columns in the input data
226 // Return all the contacts associated with position i
227 LinkedHashMap<Range, Float> contacts = isContact(basePairs, i + 1);
229 String description = "";
232 if (contacts.size() == 0)
234 description = "No Data";
238 for (Range contact : contacts.keySet())
240 float t = contacts.get(contact);
245 description += Integer.toString(contact.from) + "->"
246 + Integer.toString(contact.to) + ": "
247 + Float.toString(t) + "% | ";
251 anns[ri] = new Annotation(struct.substring(i, i + 1), description,
252 isSS(struct.charAt(i)), prob);
255 else if (data == null || data.size() == 1)
257 for (int i = 0, ri = 0, iEnd = struct.length(); i < iEnd; i++, ri++)
261 // skip any gapped columns in the input data
262 while (!gapMap[ri] && ri < gapMap.length)
266 if (ri == gapMap.length)
271 anns[ri] = new Annotation(struct.substring(i, i + 1), "",
272 isSS(struct.charAt(i)), Float.NaN);
275 annotation.graph = 0; // No graph
278 annotation.annotations = anns;
283 private static String[] constructTypenameAndDescription(Score score)
285 String description = "";
286 String typename = "";
287 String datatype = score.getMethod();
289 // Look up java switch syntax and use one here
290 if (datatype.equals(AlifoldResult.mfeStructure.toString()))
293 description = MessageFormat.format(
294 "Minimum Free Energy Structure. Energy: {0} = {1} + {2}",
295 score.getScores().get(0), score.getScores().get(1),
296 score.getScores().get(2));
297 typename = "MFE Structure";
300 .equals(AlifoldResult.contactProbabilityStructure.toString()))
302 description = MessageFormat.format("Base Pair Contact Probabilities. "
303 + "Energy of Ensemble: {0} Frequency of Ensemble: {1}",
304 score.getScores().get(0), score.getScores().get(1));
305 typename = "Contact Probabilities";
307 else if (datatype.equals(AlifoldResult.centroidStructure.toString()))
309 description = MessageFormat.format(
310 "Centroid Structure. Energy: {0} = {1} + {2}",
311 score.getScores().get(0), score.getScores().get(1),
312 score.getScores().get(2));
313 typename = "Centroid Structure";
315 else if (datatype.equals(AlifoldResult.stochBTStructure.toString()))
317 if (score.getScores().size() > 0)
319 description = MessageFormat.format("Probability: {0} Energy: {1}",
320 score.getScores().get(0), score.getScores().get(1));
324 description = "Stochastic Backtrack Structure";
327 else if (datatype.equals(AlifoldResult.MEAStucture.toString()))
329 description = MessageFormat.format(
330 "Maximum Expected Accuracy Values: '{' {0} MEA={1} '}",
331 score.getScores().get(0), score.getScores().get(1));
332 typename = "MEA Structure";
334 else if (datatype.equals(AlifoldResult.consensusAlignment.toString()))
336 typename = "RNAalifold Consensus";
337 description = "Consensus Alignment Produced by RNAalifold";
342 description = typename;
345 return new String[] { typename, description };
348 // Check whether, at position i there is a base contact and return all the
349 // contacts at this position. Should be in order of descending probability.
350 private static LinkedHashMap<Range, Float> isContact(
351 LinkedHashMap<Range, Float> basePairs, int i)
353 LinkedHashMap<Range, Float> contacts = new LinkedHashMap<>();
355 for (Range contact : basePairs.keySet())
357 // finds the contacts associtated with position i ordered by the natural
358 // ordering of the Scores TreeSet in ScoreManager which is, descending
360 if (contact.from == i || contact.to == i)
362 contacts.put(contact, basePairs.get(contact));
369 private static char isSS(char chr)
371 String regex = "\\(|\\)|\\{|\\}|\\[|\\]";
372 char ss = (Pattern.matches(regex, Character.toString(chr))) ? 'S' : ' ';