2444592253ef54560c0b9a4a975872b7186d0eb6
[jalview.git] / src / jalview / datamodel / xdb / embl / EmblFeatureLocations.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.datamodel.xdb.embl;
20
21 import java.util.Enumeration;
22 import java.util.Iterator;
23 import java.util.Vector;
24
25 public class EmblFeatureLocations
26 {
27   Vector locElements;
28
29   String locationType;
30
31   boolean locationComplement;
32
33   /**
34    * @return the locationComplement
35    */
36   public boolean isLocationComplement()
37   {
38     return locationComplement;
39   }
40
41   /**
42    * @param locationComplement
43    *                the locationComplement to set
44    */
45   public void setLocationComplement(boolean locationComplement)
46   {
47     this.locationComplement = locationComplement;
48   }
49
50   /**
51    * @return the locationType
52    */
53   public String getLocationType()
54   {
55     return locationType;
56   }
57
58   /**
59    * @param locationType
60    *                the locationType to set
61    */
62   public void setLocationType(String locationType)
63   {
64     this.locationType = locationType;
65   }
66
67   /**
68    * @return the locElements
69    */
70   public Vector getLocElements()
71   {
72     return locElements;
73   }
74
75   /**
76    * @param locElements
77    *                the locElements to set
78    */
79   public void setLocElements(Vector locElements)
80   {
81     this.locElements = locElements;
82   }
83
84   /**
85    * Return all location elements as start-end pairs (without accessions) TODO:
86    * pass back complement and 'less than or more than' range information Note:
87    * do not use this since it throws away any accessionIds associated with each
88    * location!
89    * 
90    * @return int[] { start1, end1, ... }
91    */
92   public int[] getElementRanges()
93   {
94     return getElementRanges(null);
95   }
96
97   /**
98    * Return all location elements concerning given accession as start-end pairs
99    * TODO: pass back complement and 'less than or more than' range information
100    * TODO: deal with multiple accessions
101    * 
102    * @param accession
103    *                the accession string for which locations are requested, or
104    *                null for all locations
105    * @return null or int[] { start1, end1, ... }
106    */
107
108   public int[] getElementRanges(String accession)
109   {
110     int sepos = 0;
111     int[] se = new int[locElements.size() * 2];
112     if (locationType.equalsIgnoreCase("single"))
113     {
114       for (Enumeration le = locElements.elements(); le.hasMoreElements();)
115       {
116         EmblFeatureLocElement loce = (EmblFeatureLocElement) le
117                 .nextElement();
118         if (accession == null || loce.accession != null
119                 && accession.equals(loce.accession))
120         {
121           BasePosition bp[] = loce.getBasePositions();
122           if (bp.length == 2)
123           {
124             se[sepos++] = Integer.parseInt(bp[0].getPos());
125             se[sepos++] = Integer.parseInt(bp[1].getPos());
126           }
127         }
128       }
129     }
130     else if (locationType.equalsIgnoreCase("join"))
131     {
132       for (Enumeration le = locElements.elements(); le.hasMoreElements();)
133       {
134         EmblFeatureLocElement loce = (EmblFeatureLocElement) le
135                 .nextElement();
136         if (accession == null || loce.accession != null
137                 && accession.equals(loce.accession))
138         {
139           BasePosition bp[] = loce.getBasePositions();
140           if (bp.length == 2)
141           {
142             se[sepos++] = Integer.parseInt(bp[0].getPos());
143             se[sepos++] = Integer.parseInt(bp[1].getPos());
144           }
145         }
146       }
147       return se;
148     }
149     else if (locationType != null)
150     {
151       if (jalview.bin.Cache.log != null)
152         jalview.bin.Cache.log
153                 .error("EmbleFeatureLocations.getElementRanges cannot deal with locationType=='"
154                         + locationType + "'");
155       else
156         System.err
157                 .println("EmbleFeatureLocations.getElementRanges cannot deal with locationType=='"
158                         + locationType + "'");
159     }
160     // trim range if necessary.
161     if (se != null && sepos != se.length)
162     {
163       int[] trimmed = new int[sepos];
164       System.arraycopy(se, 0, trimmed, 0, sepos);
165       se = trimmed;
166     }
167     return se;
168   }
169 }