applied copyright 2008
[jalview.git] / src / jalview / datamodel / xdb / embl / EmblFeatureLocations.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.datamodel.xdb.embl;
20
21 import java.util.Enumeration;
22 import java.util.Iterator;
23 import java.util.Vector;
24
25 public class EmblFeatureLocations {
26     Vector locElements;
27     String locationType;
28     boolean locationComplement;
29     /**
30      * @return the locationComplement
31      */
32     public boolean isLocationComplement() {
33         return locationComplement;
34     }
35     /**
36      * @param locationComplement the locationComplement to set
37      */
38     public void setLocationComplement(boolean locationComplement) {
39         this.locationComplement = locationComplement;
40     }
41     /**
42      * @return the locationType
43      */
44     public String getLocationType() {
45         return locationType;
46     }
47     /**
48      * @param locationType the locationType to set
49      */
50     public void setLocationType(String locationType) {
51         this.locationType = locationType;
52     }
53     /**
54      * @return the locElements
55      */
56     public Vector getLocElements() {
57         return locElements;
58     }
59     /**
60      * @param locElements the locElements to set
61      */
62     public void setLocElements(Vector locElements) {
63         this.locElements = locElements;
64     }
65     /**
66      * Return all location elements as start-end pairs (without accessions)  
67      * TODO: pass back complement and 'less than or more than' range information
68      * Note: do not use this since it throws away any accessionIds associated with
69      * each location!
70      * @return int[] { start1, end1, ... }
71      */
72     public int[] getElementRanges() {
73       return getElementRanges(null);
74     }
75     /**
76      * Return all location elements concerning given accession as start-end pairs  
77      * TODO: pass back complement and 'less than or more than' range information
78      * TODO: deal with multiple accessions
79      * @param accession the accession string for which locations are requested, or null for all locations
80      * @return null or int[] { start1, end1, ... }
81      */
82     
83     public int[] getElementRanges(String accession)
84     {
85       int sepos=0;
86       int[] se = new int[locElements.size()*2];
87       if (locationType.equalsIgnoreCase("single")) {
88         for (Enumeration le=locElements.elements();le.hasMoreElements();) {
89             EmblFeatureLocElement loce = (EmblFeatureLocElement) le.nextElement();
90             if (accession==null || loce.accession!=null && accession.equals(loce.accession))
91             {
92               BasePosition bp[] = loce.getBasePositions();
93               if (bp.length==2) {
94                 se[sepos++] = Integer.parseInt(bp[0].getPos());
95                 se[sepos++] = Integer.parseInt(bp[1].getPos());
96               }
97             }
98         }
99     } else 
100     if (locationType.equalsIgnoreCase("join")) {
101         for (Enumeration le=locElements.elements();le.hasMoreElements();) {
102           EmblFeatureLocElement loce = (EmblFeatureLocElement) le.nextElement();
103           if (accession==null || loce.accession!=null && accession.equals(loce.accession))
104           {
105             BasePosition bp[] = loce.getBasePositions();
106             if (bp.length==2) {
107                 se[sepos++] = Integer.parseInt(bp[0].getPos());
108                 se[sepos++] = Integer.parseInt(bp[1].getPos());
109             }
110           }
111         }
112         return se;
113     } else
114     if (locationType!=null)
115     {
116       if (jalview.bin.Cache.log!=null)
117         jalview.bin.Cache.log.error("EmbleFeatureLocations.getElementRanges cannot deal with locationType=='"+locationType+"'");
118       else
119         System.err.println("EmbleFeatureLocations.getElementRanges cannot deal with locationType=='"+locationType+"'");
120     }
121     // trim range if necessary.
122     if (se!=null && sepos!=se.length)
123     {
124       int[] trimmed = new int[sepos];
125       System.arraycopy(se,0,trimmed, 0, sepos);
126       se = trimmed;
127     }
128     return se;
129     }
130 }