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