debugged mappings and translated sequence recovery
[jalview.git] / src / jalview / datamodel / xdb / embl / EmblFeatureLocations.java
1 package jalview.datamodel.xdb.embl;
2
3 import java.util.Enumeration;
4 import java.util.Iterator;
5 import java.util.Vector;
6
7 public class EmblFeatureLocations {
8     Vector locElements;
9     String locationType;
10     boolean locationComplement;
11     /**
12      * @return the locationComplement
13      */
14     public boolean isLocationComplement() {
15         return locationComplement;
16     }
17     /**
18      * @param locationComplement the locationComplement to set
19      */
20     public void setLocationComplement(boolean locationComplement) {
21         this.locationComplement = locationComplement;
22     }
23     /**
24      * @return the locationType
25      */
26     public String getLocationType() {
27         return locationType;
28     }
29     /**
30      * @param locationType the locationType to set
31      */
32     public void setLocationType(String locationType) {
33         this.locationType = locationType;
34     }
35     /**
36      * @return the locElements
37      */
38     public Vector getLocElements() {
39         return locElements;
40     }
41     /**
42      * @param locElements the locElements to set
43      */
44     public void setLocElements(Vector locElements) {
45         this.locElements = locElements;
46     }
47     /**
48      * Return all location elements as start-end pairs (without accessions)  
49      * TODO: pass back complement and 'less than or more than' range information
50      * Note: do not use this since it throws away any accessionIds associated with
51      * each location!
52      * @return int[] { start1, end1, ... }
53      */
54     public int[] getElementRanges() {
55       return getElementRanges(null);
56     }
57     /**
58      * Return all location elements concerning given accession as start-end pairs  
59      * TODO: pass back complement and 'less than or more than' range information
60      * TODO: deal with multiple accessions
61      * @param accession the accession string for which locations are requested, or null for all locations
62      * @return null or int[] { start1, end1, ... }
63      */
64     
65     public int[] getElementRanges(String accession)
66     {
67       int sepos=0;
68       int[] se = new int[locElements.size()*2];
69       if (locationType.equalsIgnoreCase("single")) {
70         for (Enumeration le=locElements.elements();le.hasMoreElements();) {
71             EmblFeatureLocElement loce = (EmblFeatureLocElement) le.nextElement();
72             if (accession==null || loce.accession!=null && accession.equals(loce.accession))
73             {
74               BasePosition bp[] = loce.getBasePositions();
75               if (bp.length==2) {
76                 se[sepos++] = Integer.parseInt(bp[0].getPos());
77                 se[sepos++] = Integer.parseInt(bp[1].getPos());
78               }
79             }
80         }
81     } else 
82     if (locationType.equalsIgnoreCase("join")) {
83         for (Enumeration le=locElements.elements();le.hasMoreElements();) {
84           EmblFeatureLocElement loce = (EmblFeatureLocElement) le.nextElement();
85           if (accession==null || loce.accession!=null && accession.equals(loce.accession))
86           {
87             BasePosition bp[] = loce.getBasePositions();
88             if (bp.length==2) {
89                 se[sepos++] = Integer.parseInt(bp[0].getPos());
90                 se[sepos++] = Integer.parseInt(bp[1].getPos());
91             }
92           }
93         }
94         return se;
95     } else
96     if (locationType!=null)
97     {
98       if (jalview.bin.Cache.log!=null)
99         jalview.bin.Cache.log.error("EmbleFeatureLocations.getElementRanges cannot deal with locationType=='"+locationType+"'");
100       else
101         System.err.println("EmbleFeatureLocations.getElementRanges cannot deal with locationType=='"+locationType+"'");
102     }
103     // trim range if necessary.
104     if (se!=null && sepos!=se.length)
105     {
106       int[] trimmed = new int[sepos];
107       System.arraycopy(se,0,trimmed, 0, sepos);
108       se = trimmed;
109     }
110     return se;
111     }
112 }