8a86ce058abfc7ff48f770a468cea8f24fda6f92
[jalview.git] / src / jalview / workers / AlignmentAnnotationFactory.java
1 package jalview.workers;
2
3 import jalview.api.AlignViewportI;
4 import jalview.api.AlignmentViewPanel;
5 import jalview.bin.Jalview;
6 import jalview.datamodel.AlignmentAnnotation;
7 import jalview.datamodel.Annotation;
8 import jalview.gui.AlignFrame;
9
10 import java.awt.Color;
11 import java.lang.reflect.Method;
12
13 /**
14  * Factory class with methods which allow clients (including external scripts
15  * such as Groovy) to 'register and forget' an alignment annotation calculator. <br>
16  * Currently supports two flavours of calculator:
17  * <ul>
18  * <li>a simple 'feature counter' which counts any desired score derivable from
19  * residue value and any sequence features at each position of the alignment</li>
20  * <li>a 'general purpose' calculator which computes one or more complete
21  * AlignmentAnnotation objects</li>
22  * </ul>
23  */
24 public class AlignmentAnnotationFactory
25 {
26   /**
27    * Constructs and registers a new alignment annotation worker
28    * 
29    * @param counter
30    *          provider of feature counts per alignment position
31    */
32   public static void newCalculator(ColumnCounterI counter)
33   {
34     // TODO need an interface for AlignFrame by which to access
35     // its AlignViewportI and AlignmentViewPanel
36     AlignmentViewPanel currentAlignFrame = Jalview.getCurrentAlignFrame().alignPanel;
37     if (currentAlignFrame != null)
38     {
39       Method newCalcMethod = null;
40       try
41       {
42         for (Method m : AlignmentAnnotationFactory.class.getMethods())
43         {
44           if (m.getName().equals("newCalculator"))
45           {
46             if (m.getParameterCount() == 3
47                     && m.getParameterTypes()[2].isInstance(counter))
48             {
49               newCalcMethod = m;
50               break;
51             }
52           }
53         }
54       } catch (Exception q)
55       {
56       }
57       if (newCalcMethod == null)
58       {
59
60         System.err
61                 .println("Couldn't find a newCalculator method for ColumnCounterI type "
62                         + counter.getClass().getName());
63       }
64       try
65       {
66         newCalcMethod.invoke(null, currentAlignFrame.getAlignViewport(),
67                 currentAlignFrame, counter);
68       } catch (Exception ie)
69       {
70         System.err
71                 .println("Exception when reporting newCalculator method for ColumnCounterI type "
72                         + counter.getClass().getName());
73       }
74     }
75     else
76     {
77       System.err
78               .println("Can't register calculator as no alignment window has focus");
79     }
80   }
81
82   /**
83    * Constructs and registers a new alignment annotation worker
84    * 
85    * @param viewport
86    * @param panel
87    * @param counter
88    *          provider of feature counts per alignment position
89    */
90   public static void newCalculator(AlignViewportI viewport,
91           AlignmentViewPanel panel, FeatureCounterI counter)
92   {
93     new ColumnCounterWorker(viewport, panel, counter);
94   }
95
96   /**
97    * Constructs and registers a new alignment annotation worker for a set of
98    * column counters
99    * 
100    * @param viewport
101    * @param panel
102    * @param counter
103    *          provider of feature counts per alignment position
104    */
105   public static void newCalculator(AlignViewportI viewport,
106           AlignmentViewPanel panel, FeatureSetCounterI counter)
107   {
108     new ColumnCounterSetWorker(viewport, panel, counter);
109   }
110
111   /**
112    * Constructs and registers a new alignment annotation worker
113    * 
114    * @param calculator
115    *          provider of AlignmentAnnotation for the alignment
116    */
117   public static void newCalculator(AnnotationProviderI calculator)
118   {
119     // TODO need an interface for AlignFrame by which to access
120     // its AlignViewportI and AlignmentViewPanel
121     AlignFrame currentAlignFrame = Jalview.getCurrentAlignFrame();
122     if (currentAlignFrame != null)
123     {
124       newCalculator(currentAlignFrame.getViewport(), currentAlignFrame
125               .getAlignPanels().get(0), calculator);
126     }
127     else
128     {
129       System.err
130               .println("Can't register calculator as no alignment window has focus");
131     }
132   }
133
134   /**
135    * Constructs and registers a new alignment annotation worker
136    * 
137    * @param viewport
138    * @param panel
139    * @param calculator
140    *          provider of AlignmentAnnotation for the alignment
141    */
142   public static void newCalculator(AlignViewportI viewport,
143           AlignmentViewPanel panel, AnnotationProviderI calculator)
144   {
145     new AnnotationWorker(viewport, panel, calculator);
146   }
147
148   /**
149    * Factory method to construct an Annotation object
150    * 
151    * @param displayChar
152    * @param desc
153    * @param secondaryStructure
154    * @param val
155    * @param color
156    * @return
157    */
158   public static Annotation newAnnotation(String displayChar, String desc,
159           char secondaryStructure, float val, Color color)
160   {
161     return new Annotation(displayChar, desc, secondaryStructure, val, color);
162   }
163
164   /**
165    * Factory method to construct an AlignmentAnnotation object
166    * 
167    * @param name
168    * @param desc
169    * @param anns
170    * @return
171    */
172   public static AlignmentAnnotation newAlignmentAnnotation(String name,
173           String desc, Annotation[] anns)
174   {
175     return new AlignmentAnnotation(name, desc, anns);
176   }
177 }