merge from 2_4_Release branch
[jalview.git] / src / org / biojava / dasobert / feature / AbstractSegment.java
1 /*
2  *                  BioJava development code
3  *
4  * This code may be freely distributed and modified under the
5  * terms of the GNU Lesser General Public Licence.  This should
6  * be distributed with the code.  If you do not have a copy,
7  * see:
8  *
9  *      http://www.gnu.org/copyleft/lesser.html
10  *
11  * Copyright for this code is held jointly by the individual
12  * authors.  These should be listed in @author doc comments.
13  *
14  * For more information on the BioJava project and its aims,
15  * or to join the biojava-l mailing list, visit the home page
16  * at:
17  *
18  *      http://www.biojava.org/
19  * 
20  * Created on May 22, 2007
21  * 
22  */
23
24 package org.biojava.dasobert.feature;
25
26 import java.awt.Color;
27
28 public abstract class AbstractSegment implements Segment, Cloneable
29 {
30   int start;
31
32   int end;
33
34   String name;
35
36   Color color;
37
38   FeatureTrack parent;
39
40   String txtColor;
41
42   String note;
43
44   public abstract Object clone();
45
46   /*
47    * (non-Javadoc)
48    * 
49    * @see org.biojava.spice.feature.SegmentIF#toString()
50    */
51   public String toString()
52   {
53     String str = "Segment: " + name + " " + start + " " + end;
54     if ((note != null) && (!note.equals("null")))
55       if (note.length() > 40)
56         str += note.substring(0, 39) + "...";
57       else
58         str += note;
59     return str;
60   }
61
62   /*
63    * (non-Javadoc)
64    * 
65    * @see org.biojava.spice.feature.SegmentIF#getNote()
66    */
67   public String getNote()
68   {
69     return note;
70   }
71
72   /*
73    * (non-Javadoc)
74    * 
75    * @see org.biojava.spice.feature.SegmentIF#setNote(java.lang.String)
76    */
77   public void setNote(String note)
78   {
79     this.note = note;
80   }
81
82   /*
83    * (non-Javadoc)
84    * 
85    * @see org.biojava.spice.feature.SegmentIF#setStart(int)
86    */
87   public void setStart(int strt)
88   {
89     start = strt;
90   }
91
92   /*
93    * (non-Javadoc)
94    * 
95    * @see org.biojava.spice.feature.SegmentIF#getStart()
96    */
97   public int getStart()
98   {
99     return start;
100   }
101
102   /*
103    * (non-Javadoc)
104    * 
105    * @see org.biojava.spice.feature.SegmentIF#setEnd(int)
106    */
107   public void setEnd(int ed)
108   {
109     end = ed;
110   }
111
112   /*
113    * (non-Javadoc)
114    * 
115    * @see org.biojava.spice.feature.SegmentIF#getEnd()
116    */
117   public int getEnd()
118   {
119     return end;
120   }
121
122   /*
123    * (non-Javadoc)
124    * 
125    * @see org.biojava.spice.feature.SegmentIF#setName(java.lang.String)
126    */
127   public void setName(String nam)
128   {
129     name = nam;
130   }
131
132   /*
133    * (non-Javadoc)
134    * 
135    * @see org.biojava.spice.feature.SegmentIF#getName()
136    */
137   public String getName()
138   {
139     return name;
140   }
141
142   /*
143    * (non-Javadoc)
144    * 
145    * @see org.biojava.spice.feature.SegmentIF#setColor(java.awt.Color)
146    */
147   public void setColor(Color col)
148   {
149     color = col;
150   }
151
152   /*
153    * (non-Javadoc)
154    * 
155    * @see org.biojava.spice.feature.SegmentIF#getColor()
156    */
157   public Color getColor()
158   {
159     return color;
160   }
161
162   /*
163    * (non-Javadoc)
164    * 
165    * @see org.biojava.spice.feature.SegmentIF#setParent(org.biojava.spice.feature.Feature)
166    */
167   public void setParent(FeatureTrack f)
168   {
169     parent = f;
170   }
171
172   /*
173    * (non-Javadoc)
174    * 
175    * @see org.biojava.spice.feature.SegmentIF#getParent()
176    */
177   public FeatureTrack getParent()
178   {
179     return parent;
180   }
181
182   /*
183    * (non-Javadoc)
184    * 
185    * @see org.biojava.spice.feature.SegmentIF#setTxtColor(java.lang.String)
186    */
187   public void setTxtColor(String str)
188   {
189     txtColor = str;
190   }
191
192   /*
193    * (non-Javadoc)
194    * 
195    * @see org.biojava.spice.feature.SegmentIF#getTxtColor()
196    */
197   public String getTxtColor()
198   {
199     return txtColor;
200   }
201
202   /*
203    * (non-Javadoc)
204    * 
205    * @see org.biojava.spice.feature.SegmentIF#overlaps(int)
206    */
207   public boolean overlaps(int seqPosition)
208   {
209     if ((getStart() <= seqPosition) && (getEnd() >= seqPosition))
210     {
211       return true;
212     }
213     return false;
214   }
215
216   /*
217    * (non-Javadoc)
218    * 
219    * @see org.biojava.spice.feature.SegmentIF#overlaps(org.biojava.spice.feature.Segment)
220    */
221   public boolean overlaps(Segment segment)
222   {
223     if (!(this.start <= this.end))
224       throw new IndexOutOfBoundsException("start > end for segment" + this);
225
226     if (!(segment.getStart() <= segment.getEnd()))
227       throw new IndexOutOfBoundsException("start > end for segment"
228               + segment);
229
230     // start must be in region of other
231     if (this.start >= segment.getStart())
232     {
233       if (this.start <= segment.getEnd())
234       {
235         return true;
236       }
237     }
238     // or end must be in region of other..
239     if (this.end >= segment.getStart())
240     {
241       if (this.end <= segment.getEnd())
242       {
243         return true;
244       }
245     }
246
247     if (this.start <= segment.getStart())
248     {
249       if (this.end >= segment.getEnd())
250       {
251         return true;
252       }
253     }
254     return false;
255   }
256 }