1 package jalview.ws.jws2;
3 import jalview.api.AlignCalcWorkerI;
4 import jalview.datamodel.AlignmentAnnotation;
5 import jalview.datamodel.Annotation;
6 import jalview.gui.AlignFrame;
7 import jalview.schemes.NucleotideColourScheme;
8 import jalview.ws.jws2.jabaws2.Jws2Instance;
9 import jalview.ws.params.WsParamSetI;
11 import java.util.ArrayList;
12 import java.util.Iterator;
13 import java.util.List;
15 import java.util.TreeMap;
16 import java.util.TreeSet;
18 import compbio.data.msa.jaxws.Align;
19 import compbio.data.sequence.Range;
20 import compbio.data.sequence.Score;
21 import compbio.data.sequence.RNAStructScoreManager;
22 import compbio.data.sequence.ScoreManager;
23 import compbio.metadata.Argument;
25 public class RNAalifoldClient extends JabawsAlignCalcWorker implements
35 public RNAalifoldClient(Jws2Instance sh, AlignFrame alignFrame,
36 WsParamSetI preset, List<Argument> paramset)
38 super(sh, alignFrame, preset, paramset);
40 if (arguments == null) arguments = new ArrayList<Argument>();
41 arguments.add(sh.getRunnerConfig().getArgumentByOptionName("-p"));
45 methodName = sh.serviceType;
47 // defult false. Which one here?
49 nucleotidesAllowed = true;
50 proteinAllowed = false;
52 arguments.add(sh.getRunnerConfig().getArgumentByOptionName("-p"));
56 public String getServiceActionText()
58 return "Submitting RNA alignment for Secondary Structure prediction using "
59 + "RNAalifold Service";
63 public void updateResultAnnotation(boolean immediate)
67 if (immediate || !calcMan.isWorking(this) && scoremanager != null)
70 List<AlignmentAnnotation> ourAnnot = new ArrayList<AlignmentAnnotation>();
71 // ourAnnots = new ArrayList<AlignmentAnnotation>();
73 // So I don't have to do any more casting
74 List<String> structs = ((RNAStructScoreManager) scoremanager).getStructs();
75 List<TreeSet<Score>> data = ((RNAStructScoreManager) scoremanager).getData();
78 // I think this will never find an annotation at the moment. It will always create...
79 AlignmentAnnotation annotation = alignViewport.getAlignment()
80 .findOrCreateAnnotation("Consensus Structure", getCalcId(), false, null , null);
82 // construct Annotation from scoremanager
84 // Deal with the consensus structure and (?)BasePair Probabilities
85 Annotation[] anns = new Annotation[structs.get(1).length()];
87 // check if the first Score object is populated with base pair probabilities
88 Score fscore = data.get(0).first();
89 boolean BPScores = (fscore.getScores().size() > 0
90 && fscore.getRanges() != null);
92 TreeMap<Range, Float> basePairs = null;
94 // The base pair probabilities are stored in a set in scoremanager... we want a map
95 basePairs = new TreeMap<Range, Float>();
96 for (Score score : data.get(0)) {
97 // The Score objects contain a set of size one containing the range and
98 // an ArrayList<float> of size one containing the probabilty
99 basePairs.put(score.getRanges().first(), new Float(score.getScores().get(0)));
103 // ignoring the Consensus alignemnt for now, get the Structure and make an AlignmentAnnotation
104 String struct = structs.get(1); // get(1)
105 for (int i = 0; i < struct.length(); i++) {
108 // Return all the contacts associated with position i
109 List<Range> contacts = isContact(basePairs, i+1);
111 if (contacts.size() == 0) {
112 anns[i] = new Annotation(struct.substring(i, i+1), "", struct.charAt(i), 0f);
114 else if (contacts.size() == 1) {
115 // There is only one contact associated with this base
116 float prob = basePairs.get(contacts.get(0));
117 anns[i] = new Annotation(struct.substring(i, i+1), "", struct.charAt(i), prob);
119 else if (contacts.size() > 1) {
120 // For now we will simply deal with alternate contact information by mentioning its
121 // existance in the description
122 float prob = basePairs.get(contacts.get(0));
123 anns[i] = new Annotation(struct.substring(i, i+1), "This base has alternate contacts",
124 struct.charAt(i), prob);
128 // Same as the first if from the previous block
129 anns[i] = new Annotation(struct.substring(i, i+1), "", struct.charAt(i), 0f);
133 System.out.println("size of anns: " + anns.length);
135 // Set the annotation to the AlignmentAnnotation object
136 annotation.annotations = anns;
138 // Set the probability
139 annotation.setScore(data.get(1).first().getScores().get(0));
144 // AlignmentAnnotation annot = new AlignmentAnnotation("Consensus Structure", "Free Energy", anns);
149 System.out.println("RNAalifoldClient - annotation:\n");
150 for (Annotation ann : annotation.annotations) {
151 System.out.print(ann.toString()+"|");
153 System.out.println();
155 // Instead of this look at existing methods for creating annotations
156 ourAnnot.add(annotation);
159 if (ourAnnot.size() > 0) {
160 updateOurAnnots(ourAnnot);
166 // Check whether, at position i there is a base contact and return all the
167 // contacts at this position. Should be in order of descending probability.
168 private List<Range> isContact(TreeMap<Range, Float> basePairs, int i) {
170 List<Range> contacts = new ArrayList<Range>();
172 for (Range contact : basePairs.keySet()) {
173 // finds the contacts associtated with position i ordered by the natural
174 // ordering of the Scores TreeSet in ScoreManager which is, descending probability
175 if (contact.from == i || contact.to == i) contacts.add(contact);
181 public String getCalcId()
183 return SequenceAnnotationWSClient.AAConCalcId;