JAL-2189 apply license
[jalview.git] / src / jalview / workers / AlignmentAnnotationFactory.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.workers;
22
23 import jalview.api.AlignViewportI;
24 import jalview.api.AlignmentViewPanel;
25 import jalview.bin.Jalview;
26 import jalview.datamodel.AlignmentAnnotation;
27 import jalview.datamodel.Annotation;
28 import jalview.gui.AlignFrame;
29
30 import java.awt.Color;
31
32 /**
33  * Factory class with methods which allow clients (including external scripts
34  * such as Groovy) to 'register and forget' an alignment annotation calculator. <br>
35  * Currently supports two flavours of calculator:
36  * <ul>
37  * <li>a simple 'feature counter' which counts any desired score derivable from
38  * residue value and any sequence features at each position of the alignment</li>
39  * <li>a 'general purpose' calculator which computes one or more complete
40  * AlignmentAnnotation objects</li>
41  * </ul>
42  */
43 public class AlignmentAnnotationFactory
44 {
45   /**
46    * Constructs and registers a new alignment annotation worker
47    * 
48    * @param counter
49    *          provider of feature counts per alignment position
50    */
51   public static void newCalculator(FeatureCounterI counter)
52   {
53     // TODO need an interface for AlignFrame by which to access
54     // its AlignViewportI and AlignmentViewPanel
55     AlignmentViewPanel currentAlignFrame = Jalview.getCurrentAlignFrame().alignPanel;
56     if (currentAlignFrame != null)
57     {
58       newCalculator(currentAlignFrame.getAlignViewport(),
59               currentAlignFrame, counter);
60     }
61     else
62     {
63       System.err
64               .println("Can't register calculator as no alignment window has focus");
65     }
66   }
67
68   /**
69    * Constructs and registers a new alignment annotation worker
70    * 
71    * @param viewport
72    * @param panel
73    * @param counter
74    *          provider of feature counts per alignment position
75    */
76   public static void newCalculator(AlignViewportI viewport,
77           AlignmentViewPanel panel, FeatureCounterI counter)
78   {
79     new ColumnCounterWorker(viewport, panel, counter);
80   }
81
82   /**
83    * Constructs and registers a new alignment annotation worker
84    * 
85    * @param calculator
86    *          provider of AlignmentAnnotation for the alignment
87    */
88   public static void newCalculator(AnnotationProviderI calculator)
89   {
90     // TODO need an interface for AlignFrame by which to access
91     // its AlignViewportI and AlignmentViewPanel
92     AlignFrame currentAlignFrame = Jalview.getCurrentAlignFrame();
93     if (currentAlignFrame != null)
94     {
95       newCalculator(currentAlignFrame.getViewport(), currentAlignFrame
96               .getAlignPanels().get(0), calculator);
97     }
98     else
99     {
100       System.err
101               .println("Can't register calculator as no alignment window has focus");
102     }
103   }
104
105   /**
106    * Constructs and registers a new alignment annotation worker
107    * 
108    * @param viewport
109    * @param panel
110    * @param calculator
111    *          provider of AlignmentAnnotation for the alignment
112    */
113   public static void newCalculator(AlignViewportI viewport,
114           AlignmentViewPanel panel, AnnotationProviderI calculator)
115   {
116     new AnnotationWorker(viewport, panel, calculator);
117   }
118
119   /**
120    * Factory method to construct an Annotation object
121    * 
122    * @param displayChar
123    * @param desc
124    * @param secondaryStructure
125    * @param val
126    * @param color
127    * @return
128    */
129   public static Annotation newAnnotation(String displayChar, String desc,
130           char secondaryStructure, float val, Color color)
131   {
132     return new Annotation(displayChar, desc, secondaryStructure, val, color);
133   }
134
135   /**
136    * Factory method to construct an AlignmentAnnotation object
137    * 
138    * @param name
139    * @param desc
140    * @param anns
141    * @return
142    */
143   public static AlignmentAnnotation newAlignmentAnnotation(String name,
144           String desc, Annotation[] anns)
145   {
146     return new AlignmentAnnotation(name, desc, anns);
147   }
148 }