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