959214241209c0870cb94d6ddb878fb75d65b984
[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.schemes.NucleotideColourScheme;
8 import jalview.ws.jws2.jabaws2.Jws2Instance;
9 import jalview.ws.params.WsParamSetI;
10
11 import java.util.ArrayList;
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.TreeMap;
16 import java.util.TreeSet;
17
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;
24
25 public class RNAalifoldClient extends JabawsAlignCalcWorker implements
26                 AlignCalcWorkerI 
27 {
28         
29         // test
30         
31         String methodName;
32         
33         AlignFrame af;
34         
35         public RNAalifoldClient(Jws2Instance sh, AlignFrame alignFrame,
36                         WsParamSetI preset, List<Argument> paramset)
37         {
38                 super(sh, alignFrame, preset, paramset);
39                 
40                 if (arguments == null) arguments = new ArrayList<Argument>();
41                 arguments.add(sh.getRunnerConfig().getArgumentByOptionName("-p"));
42
43                 
44                 af = alignFrame;
45                 methodName = sh.serviceType;
46                 
47                 // defult false. Which one here?
48                 // submitGaps = true;
49                 nucleotidesAllowed = true;
50                 proteinAllowed = false;
51                 
52                 arguments.add(sh.getRunnerConfig().getArgumentByOptionName("-p"));
53         }
54         
55          @Override
56           public String getServiceActionText()
57           {
58             return "Submitting RNA alignment for Secondary Structure prediction using "
59                         + "RNAalifold Service";
60           }
61         
62         @Override
63         public void updateResultAnnotation(boolean immediate)
64         {
65                 
66
67                 if (immediate || !calcMan.isWorking(this) && scoremanager != null) 
68                 {
69                         
70                         List<AlignmentAnnotation> ourAnnot = new ArrayList<AlignmentAnnotation>();
71                         //              ourAnnots = new ArrayList<AlignmentAnnotation>();
72
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();
76                         
77                         
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);
81                         
82                         // construct Annotation from scoremanager
83                         
84                         // Deal with the consensus structure and (?)BasePair Probabilities
85                         Annotation[] anns = new Annotation[structs.get(1).length()];
86
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);
91
92                         TreeMap<Range, Float> basePairs = null;
93                         if (BPScores) {
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)));
100                                 }
101                         }
102
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++) {
106
107                                 if (BPScores) {
108                                         // Return all the contacts associated with position i
109                                         List<Range> contacts = isContact(basePairs, i+1);
110
111                                         if (contacts.size() == 0) {
112                                                 anns[i] = new Annotation(struct.substring(i, i+1), "", struct.charAt(i), 0f);
113                                         }
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);
118                                         }
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);
125                                         }
126                                 }
127                                 else {
128                                         // Same as the first if from the previous block
129                                         anns[i] = new Annotation(struct.substring(i, i+1), "", struct.charAt(i), 0f);
130                                 }
131                         }
132
133                         System.out.println("size of anns: " + anns.length);
134                         
135                         // Set the annotation to the AlignmentAnnotation object
136                         annotation.annotations = anns;
137                         
138                         // Set the probability
139                         annotation.setScore(data.get(1).first().getScores().get(0));
140                         
141                         
142
143                         // old
144 //                      AlignmentAnnotation annot = new AlignmentAnnotation("Consensus Structure", "Free Energy", anns);
145                         
146                         
147                         
148                         
149                         System.out.println("RNAalifoldClient - annotation:\n");
150                         for (Annotation ann : annotation.annotations) {
151                                 System.out.print(ann.toString()+"|");
152                         }
153                         System.out.println();
154
155                         // Instead of this look at existing methods for creating annotations
156                         ourAnnot.add(annotation);
157                         
158                         
159                         if (ourAnnot.size() > 0) {
160                                 updateOurAnnots(ourAnnot);
161                         }
162                         
163                 }
164         }
165         
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) {
169                 
170                 List<Range> contacts = new ArrayList<Range>();
171                 
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);
176                 }
177                 
178                 return contacts;
179         }
180         
181         public String getCalcId()
182   {
183     return SequenceAnnotationWSClient.AAConCalcId;
184   }
185         
186 }