7df0698829ad9723cca0f47f9811acbccf1744d7
[jalview.git] / src / jalview / datamodel / xdb / embl / EmblFeatureLocations.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 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.datamodel.xdb.embl;
22
23 import java.util.Enumeration;
24 import java.util.Vector;
25
26 public class EmblFeatureLocations
27 {
28   Vector locElements;
29
30   String locationType;
31
32   boolean locationComplement;
33
34   /**
35    * @return the locationComplement
36    */
37   public boolean isLocationComplement()
38   {
39     return locationComplement;
40   }
41
42   /**
43    * @param locationComplement
44    *          the locationComplement to set
45    */
46   public void setLocationComplement(boolean locationComplement)
47   {
48     this.locationComplement = locationComplement;
49   }
50
51   /**
52    * @return the locationType
53    */
54   public String getLocationType()
55   {
56     return locationType;
57   }
58
59   /**
60    * @param locationType
61    *          the locationType to set
62    */
63   public void setLocationType(String locationType)
64   {
65     this.locationType = locationType;
66   }
67
68   /**
69    * @return the locElements
70    */
71   public Vector getLocElements()
72   {
73     return locElements;
74   }
75
76   /**
77    * @param locElements
78    *          the locElements to set
79    */
80   public void setLocElements(Vector locElements)
81   {
82     this.locElements = locElements;
83   }
84
85   /**
86    * Return all location elements as start-end pairs (without accessions) TODO:
87    * pass back complement and 'less than or more than' range information Note:
88    * do not use this since it throws away any accessionIds associated with each
89    * location!
90    * 
91    * @return int[] { start1, end1, ... }
92    */
93   public int[] getElementRanges()
94   {
95     return getElementRanges(null);
96   }
97
98   /**
99    * Return all location elements concerning given accession as start-end pairs
100    * TODO: pass back complement and 'less than or more than' range information
101    * TODO: deal with multiple accessions
102    * 
103    * @param accession
104    *          the accession string for which locations are requested, or null
105    *          for all locations
106    * @return null or int[] { start1, end1, ... }
107    */
108
109   public int[] getElementRanges(String accession)
110   {
111     int sepos = 0;
112     int[] se = new int[locElements.size() * 2];
113     if (locationType.equalsIgnoreCase("single"))
114     {
115       for (Enumeration le = locElements.elements(); le.hasMoreElements();)
116       {
117         EmblFeatureLocElement loce = (EmblFeatureLocElement) le
118                 .nextElement();
119         if (accession == null || loce.accession != null
120                 && accession.equals(loce.accession))
121         {
122           BasePosition bp[] = loce.getBasePositions();
123           if (bp.length == 2)
124           {
125             se[sepos++] = Integer.parseInt(bp[0].getPos());
126             se[sepos++] = Integer.parseInt(bp[1].getPos());
127           }
128         }
129       }
130     }
131     else if (locationType.equalsIgnoreCase("join"))
132     {
133       for (Enumeration le = locElements.elements(); le.hasMoreElements();)
134       {
135         EmblFeatureLocElement loce = (EmblFeatureLocElement) le
136                 .nextElement();
137         if (accession == null || loce.accession != null
138                 && accession.equals(loce.accession))
139         {
140           BasePosition bp[] = loce.getBasePositions();
141           if (bp.length == 2)
142           {
143             se[sepos++] = Integer.parseInt(bp[0].getPos());
144             se[sepos++] = Integer.parseInt(bp[1].getPos());
145           }
146         }
147       }
148       return se;
149     }
150     else if (locationType != null)
151     {
152       if (jalview.bin.Cache.log != null)
153         jalview.bin.Cache.log
154                 .error("EmbleFeatureLocations.getElementRanges cannot deal with locationType=='"
155                         + locationType + "'");
156       else
157         System.err
158                 .println("EmbleFeatureLocations.getElementRanges cannot deal with locationType=='"
159                         + locationType + "'");
160     }
161     // trim range if necessary.
162     if (se != null && sepos != se.length)
163     {
164       int[] trimmed = new int[sepos];
165       System.arraycopy(se, 0, trimmed, 0, sepos);
166       se = trimmed;
167     }
168     return se;
169   }
170 }