1 package fr.orsay.lri.varna.models.annotations;
4 import java.awt.geom.Point2D;
5 import java.io.Serializable;
7 import javax.xml.transform.sax.TransformerHandler;
9 import org.xml.sax.SAXException;
10 import org.xml.sax.helpers.AttributesImpl;
12 import fr.orsay.lri.varna.models.rna.ModeleBase;
13 import fr.orsay.lri.varna.utils.XMLUtils;
16 public class ChemProbAnnotation implements Serializable {
18 public static final String HEADER_TEXT = "ChemProbAnnotation";
23 private static final long serialVersionUID = 5833315460145031242L;
26 public enum ChemProbAnnotationType
34 public static double DEFAULT_INTENSITY = 1.0;
35 public static ChemProbAnnotationType DEFAULT_TYPE = ChemProbAnnotationType.ARROW;
36 public static Color DEFAULT_COLOR = Color.blue.darker();
38 private ModeleBase _mbfst;
39 private ModeleBase _mbsnd;
41 private double _intensity;
42 private ChemProbAnnotationType _type;
43 private boolean _outward;
45 public static String XML_ELEMENT_NAME = "ChemProbAnnotation";
46 public static String XML_VAR_INDEX5_NAME = "Index5";
47 public static String XML_VAR_INDEX3_NAME = "Index3";
48 public static String XML_VAR_COLOR_NAME = "Color";
49 public static String XML_VAR_INTENSITY_NAME = "Intensity";
50 public static String XML_VAR_TYPE_NAME = "Type";
51 public static String XML_VAR_OUTWARD_NAME = "Outward";
53 public void toXML(TransformerHandler hd) throws SAXException
55 AttributesImpl atts = new AttributesImpl();
56 atts.addAttribute("","",XML_VAR_INDEX5_NAME,"CDATA",""+_mbfst.getIndex());
57 atts.addAttribute("","",XML_VAR_INDEX3_NAME,"CDATA",""+_mbsnd.getIndex());
58 atts.addAttribute("","",XML_VAR_COLOR_NAME,"CDATA",XMLUtils.toHTMLNotation(_color));
59 atts.addAttribute("","",XML_VAR_INTENSITY_NAME,"CDATA",""+_intensity);
60 atts.addAttribute("","",XML_VAR_TYPE_NAME,"CDATA",""+_type);
61 atts.addAttribute("","",XML_VAR_OUTWARD_NAME,"CDATA",""+_outward);
62 hd.startElement("","",XML_ELEMENT_NAME,atts);
63 hd.endElement("","",XML_ELEMENT_NAME);
67 public ChemProbAnnotation(ModeleBase mbfst, ModeleBase mbsnd, String styleDesc) {
69 applyStyle(styleDesc);
72 public ChemProbAnnotation(ModeleBase mbfst, ModeleBase mbsnd) {
73 this(mbfst,mbsnd,ChemProbAnnotation.DEFAULT_TYPE,ChemProbAnnotation.DEFAULT_INTENSITY);
76 public ChemProbAnnotation(ModeleBase mbfst, ModeleBase mbsnd, double intensity) {
77 this(mbfst,mbsnd,ChemProbAnnotation.DEFAULT_TYPE,intensity);
80 public ChemProbAnnotation(ModeleBase mbfst, ModeleBase mbsnd, ChemProbAnnotationType type) {
81 this(mbfst,mbsnd,type,ChemProbAnnotation.DEFAULT_INTENSITY);
84 public ChemProbAnnotation(ModeleBase mbfst, ModeleBase mbsnd, ChemProbAnnotationType type, double intensity) {
85 this(mbfst,mbsnd, type, intensity, DEFAULT_COLOR, true);
88 public ChemProbAnnotation(ModeleBase mbfst, ModeleBase mbsnd, ChemProbAnnotationType type, double intensity, Color color, boolean out) {
89 if (mbfst.getIndex()>mbsnd.getIndex())
91 ModeleBase tmp = mbsnd;
98 _intensity = intensity;
103 public boolean isOut()
108 public void setOut(boolean b)
113 public Color getColor()
118 public double getIntensity()
123 public ChemProbAnnotationType getType()
128 public void setColor(Color c){
132 public void setIntensity(double d){
136 public Point2D.Double getAnchorPosition()
138 Point2D.Double result = new Point2D.Double(
139 (_mbfst.getCoords().x+_mbsnd.getCoords().x)/2.0,
140 (_mbfst.getCoords().y+_mbsnd.getCoords().y)/2.0);
144 public Point2D.Double getDirVector()
146 Point2D.Double norm = getNormalVector();
147 Point2D.Double result = new Point2D.Double(-norm.y,norm.x);
148 Point2D.Double anchor = getAnchorPosition();
149 Point2D.Double center = new Point2D.Double(
150 (_mbfst.getCenter().x+_mbsnd.getCenter().x)/2.0,
151 (_mbfst.getCenter().y+_mbsnd.getCenter().y)/2.0);
152 Point2D.Double vradius = new Point2D.Double(
153 (center.x-anchor.x)/2.0,
154 (center.y-anchor.y)/2.0);
157 if (result.x*vradius.x+result.y*vradius.y>0)
159 return new Point2D.Double(-result.x,-result.y);
164 if (result.x*vradius.x+result.y*vradius.y<0)
166 return new Point2D.Double(-result.x,-result.y);
171 public Point2D.Double getNormalVector()
176 tmp = new Point2D.Double(
177 (-(_mbsnd.getCenter().y-_mbsnd.getCoords().y)),
178 ((_mbsnd.getCenter().x-_mbsnd.getCoords().x)));
182 tmp = new Point2D.Double(
183 (_mbsnd.getCoords().x-_mbfst.getCoords().x)/2.0,
184 (_mbsnd.getCoords().y-_mbfst.getCoords().y)/2.0);
187 double norm = tmp.distance(0, 0);
188 Point2D.Double result = new Point2D.Double(tmp.x/norm,tmp.y/norm);
192 public static ChemProbAnnotationType annotTypeFromString(String value)
194 if (value.toLowerCase().equals("arrow"))
195 {return ChemProbAnnotationType.ARROW;}
196 else if (value.toLowerCase().equals("triangle"))
197 {return ChemProbAnnotationType.TRIANGLE;}
198 else if (value.toLowerCase().equals("pin"))
199 {return ChemProbAnnotationType.PIN;}
200 else if (value.toLowerCase().equals("dot"))
201 {return ChemProbAnnotationType.DOT;}
203 {return ChemProbAnnotationType.ARROW;}
207 public void applyStyle(String styleDesc)
209 String[] chemProbs = styleDesc.split(",");
210 for (int i = 0; i < chemProbs.length; i++) {
211 String thisStyle = chemProbs[i];
212 String[] data = thisStyle.split("=");
215 String name = data[0];
216 String value = data[1];
217 if (name.toLowerCase().equals("color"))
219 Color c = Color.decode(value);
224 else if (name.toLowerCase().equals("intensity"))
226 _intensity = Double.parseDouble(value);
228 else if (name.toLowerCase().equals("dir"))
230 _outward = value.toLowerCase().equals("out");
232 else if (name.toLowerCase().equals("glyph"))
234 _type= annotTypeFromString(value);
240 public void setType(ChemProbAnnotationType s)
245 public ChemProbAnnotation clone()
247 ChemProbAnnotation result = new ChemProbAnnotation(this._mbfst,this._mbsnd);
248 result._intensity = _intensity;
249 result._type = _type;
250 result._color= _color;
251 result._outward = _outward;
255 public String toString()
257 return "Chem. prob. "+this._type+" Base#"+this._mbfst.getBaseNumber()+"-"+this._mbsnd.getBaseNumber();