70ddb7e23ce9a99d5ee2787486b02fae5b986559
[jalview.git] / src / jalview / ws / jws2 / RNAalifoldClient.java
1 package jalview.ws.jws2;
2
3 import jalview.api.AlignCalcWorkerI;
4 import jalview.datamodel.AlignmentAnnotation;
5 import jalview.datamodel.Annotation;
6 import jalview.gui.AlignFrame;
7 import jalview.ws.jws2.jabaws2.Jws2Instance;
8 import jalview.ws.params.WsParamSetI;
9
10 import java.util.ArrayList;
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.TreeMap;
15 import java.util.TreeSet;
16
17 import compbio.data.msa.jaxws.Align;
18 import compbio.data.sequence.Range;
19 import compbio.data.sequence.Score;
20 import compbio.data.sequence.RNAStructScoreManager;
21 import compbio.data.sequence.ScoreManager;
22 import compbio.metadata.Argument;
23
24 public class RNAalifoldClient extends JabawsAlignCalcWorker implements
25                 AlignCalcWorkerI 
26 {
27         
28         String methodName;
29         
30         AlignFrame af;
31         
32         public RNAalifoldClient(Jws2Instance sh, AlignFrame alignFrame,
33                         WsParamSetI preset, List<Argument> paramset)
34         {
35                 super(sh, alignFrame, preset, paramset);
36
37                 
38                 af = alignFrame;
39                                 
40                 methodName = sh.serviceType;
41
42         }
43         
44          @Override
45           public String getServiceActionText()
46           {
47             return "Submitting RNA alignment for Secondary Structure prediction using"
48                         + "RNAalifold Service";
49           }
50         
51         @Override
52         public void updateResultAnnotation(boolean immediate)
53         {
54
55                 if (immediate || !calcMan.isWorking(this) && scoremanager != null) 
56                 {
57                         
58
59                         
60                         List<AlignmentAnnotation> ourAnnot = new ArrayList<AlignmentAnnotation>();
61 //                      ourAnnots = new ArrayList<AlignmentAnnotation>();
62
63                         // So I don't have to do any more casting
64                         List<String> structs = ((RNAStructScoreManager) scoremanager).getStructs();
65                         List<TreeSet<Score>> data = ((RNAStructScoreManager) scoremanager).getData();
66                         
67                         // Deal with the consensus structure and (?)BasePair Probabilities
68                         Annotation[] anns = new Annotation[structs.get(1).length()];
69
70                         // check if the first Score object is populated with base pair probabilities
71                         Score fscore = data.get(0).first();
72                         boolean BPScores = (fscore.getScores().size() > 0 
73                                         && fscore.getRanges() != null);
74
75                         TreeMap<Range, Float> basePairs = null;
76                         if (BPScores) {
77                                 // The base pair probabilities are stored in a set in scoremanager... we want a map
78                                 basePairs = new TreeMap<Range, Float>();
79                                 for (Score score : data.get(0)) {
80                                         // The Score objects contain a set of size one containing the range and
81                                         //  an ArrayList<float> of size one containing the probabilty
82                                         basePairs.put(score.getRanges().first(), new Float(score.getScores().get(0)));
83                                 }
84                         }
85
86                         // ignoring the Consensus alignemnt for now, get the Structure and make an AlignmentAnnotation
87                         String struct = structs.get(1);
88                         for (int i = 0; i < struct.length(); i++) {
89
90                                 if (BPScores) {
91                                         // Return all the contacts associated with position i
92                                         List<Range> contacts = isContact(basePairs, i+1);
93
94                                         if (contacts.size() == 0) {
95                                                 anns[i] = new Annotation(struct.substring(i, i+1), "", struct.charAt(i), 0f);
96                                         }
97                                         else if (contacts.size() == 1) {
98                                                 // There is only one contact associated with this base
99                                                 float prob = basePairs.get(contacts.get(i));
100                                                 anns[i] = new Annotation(struct.substring(i, i+1), "", struct.charAt(i), prob);
101                                         }
102                                         else if (contacts.size() > 1) {
103                                                 // For now we will simply deal with alternate contact information by mentioning its
104                                                 //  existance in the description
105                                                 float prob = basePairs.get(contacts.get(i));
106                                                 anns[i] = new Annotation(struct.substring(i, i+1), "This base has alternate contact(s)",
107                                                                 struct.charAt(i), prob);
108                                         }
109                                 }
110                                 else {
111                                         // Same as the first if from the previous block
112                                         anns[i] = new Annotation(struct.substring(i, i+1), "", struct.charAt(i), 0f);
113                                 }
114                         }
115                         
116                         AlignmentAnnotation annot = new AlignmentAnnotation("Consensus Structure", "Free Energy", anns);
117                         annot.setScore(data.get(1).first().getScores().get(0));
118
119                         if (ourAnnot.size() > 0) {
120                                 updateOurAnnots(ourAnnot);
121                         }
122
123                 }
124         }
125         
126         // Check whether, at position i there is a base contact and return all the
127         //  contacts at this position. Should be in order of descending probability.
128         private List<Range> isContact(TreeMap<Range, Float> basePairs, int i) {
129                 
130                 List<Range> contacts = new ArrayList<Range>();
131                 
132                 for (Range contact : basePairs.keySet()) {
133                         // finds the contacts associtated with position i ordered by the natural
134                         //  ordering of the Scores TreeSet in ScoreManager which is, descending probability
135                         if (contact.from == i || contact.to == i) contacts.add(contact);
136                 }
137                 
138                 return contacts;
139         }
140 }