JAL-2446 merged to spike branch
[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(FeatureSetCounterI counter)
52   {
53     AlignmentViewPanel currentAlignFrame = Jalview.getCurrentAlignFrame().alignPanel;
54     if (currentAlignFrame == null)
55     {
56       System.err
57               .println("Can't register calculator as no alignment window has focus");
58       return;
59     }
60     new ColumnCounterSetWorker(currentAlignFrame.getAlignViewport(),
61             currentAlignFrame, counter);
62   }
63
64   /**
65    * Constructs and registers a new alignment annotation worker
66    * 
67    * @param calculator
68    *          provider of AlignmentAnnotation for the alignment
69    */
70   public static void newCalculator(AnnotationProviderI calculator)
71   {
72     // TODO need an interface for AlignFrame by which to access
73     // its AlignViewportI and AlignmentViewPanel
74     AlignFrame currentAlignFrame = Jalview.getCurrentAlignFrame();
75     if (currentAlignFrame != null)
76     {
77       new AnnotationWorker(currentAlignFrame.getViewport(),
78               currentAlignFrame.getAlignPanels().get(0), calculator);
79     }
80     else
81     {
82       System.err
83               .println("Can't register calculator as no alignment window has focus");
84     }
85   }
86
87   /**
88    * Constructs and registers a new alignment annotation worker
89    * 
90    * @param viewport
91    * @param panel
92    * @param calculator
93    *          provider of AlignmentAnnotation for the alignment
94    */
95   public static void newCalculator(AlignViewportI viewport,
96           AlignmentViewPanel panel, AnnotationProviderI calculator)
97   {
98     new AnnotationWorker(viewport, panel, calculator);
99   }
100
101   /**
102    * Factory method to construct an Annotation object
103    * 
104    * @param displayChar
105    * @param desc
106    * @param secondaryStructure
107    * @param val
108    * @param color
109    * @return
110    */
111   public static Annotation newAnnotation(String displayChar, String desc,
112           char secondaryStructure, float val, Color color)
113   {
114     return new Annotation(displayChar, desc, secondaryStructure, val, color);
115   }
116
117   /**
118    * Factory method to construct an AlignmentAnnotation object
119    * 
120    * @param name
121    * @param desc
122    * @param anns
123    * @return
124    */
125   public static AlignmentAnnotation newAlignmentAnnotation(String name,
126           String desc, Annotation[] anns)
127   {
128     return new AlignmentAnnotation(name, desc, anns);
129   }
130 }