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