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