Merge branch 'releases/Release_2_11_3_Branch'
[jalview.git] / groovy / sdppred_testing.groovy
1
2 import jalview.bin.Jalview
3 import jalview.io.gff.GffConstants;
4 import jalview.workers.AnnotationProviderI
5 import jalview.workers.AlignmentAnnotationFactory
6 import jalview.datamodel.*;
7   
8 /*
9  * Define something that counts each visible feature type
10  */
11 def sdppredCounter =
12     [
13      getNames: { visibleFeatures as String[] }, 
14      getDescriptions:  { visibleFeatures as String[] },
15      getMinColour: { [0, 255, 255] as int[] }, // cyan
16      getMaxColour: { [0, 0, 255] as int[] }, // blue
17      count: 
18          { res, feats -> 
19              getCounts.call(feats) 
20          },
21      calculateAnnotation: 
22          { al, feat_rend ->         
23          ProfilesI a_prof=al.getSequenceConsensusHash();
24          al_width = a_prof.getEndColumn();
25 //         jalview.datamodel.Annotation ai[] = new jalview.datamodel.Annotation[al_width];
26          def sg_profs = [];
27          al.getGroups().each({group -> sg_profs << group.getSequenceConsensusHash() });
28          if (sg_profs==null) {
29            return null;
30          }
31          annot = new Annotation[al_width];
32          for (int c=0;c<al_width;c++) {
33            def al_p=a_prof.get(c);
34            if (al_p!=null) {
35            def al_ng = al_p.getNonGapped()
36            if (al_p.getCounts()!=null) {
37            al_p = al_p.getCounts().getSymbolCounts();
38            def tot_count=[foo:0];
39            for (int a=al_p.values.length-1;a>-1;a--) {
40              tot_count << [(al_p.symbols[a]):(al_p.values[a])]
41            }
42            Ip=0.0;
43            if (al_ng>0) {
44              sg_profs.each({sg_prof -> 
45                if (sg_prof!=null) {count = sg_prof.get(c);
46                if ((count!=null) && (count.getCounts()!=null)) {
47                  g_size = count.getHeight();
48                  count = count.getCounts().getSymbolCounts(); 
49                  for (int a=count.values.length-1;a>-1; a--) {
50                    Ip+= (count.values[a]/al_ng) * Math.log((count.values[a]*al_ng)/(g_size*tot_count[(count.symbols[a])]));
51                  }
52                }
53                }
54              });
55            annot[c]=new Annotation((float) Ip);
56
57            }
58          }
59          }
60          }
61          AlignmentAnnotation annotrow = new AlignmentAnnotation("SDPpred","SDP calculation results",annot);
62          annotrow.setCalcId("SDPPRED");
63          return [annotrow];
64      }
65      ] as AnnotationProviderI
66
67 /*
68  * and register the counter
69  */
70 AlignmentAnnotationFactory.newCalculator(sdppredCounter)