JAL-2780 JAL-2781 fix javadoc in line with behaviour for getPDBResNum
[jalview.git] / src / jalview / structure / StructureMapping.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.structure;
22
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.SequenceI;
25
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29
30 public class StructureMapping
31 {
32   String mappingDetails;
33
34   SequenceI sequence;
35
36   String pdbfile;
37
38   String pdbid;
39
40   String pdbchain;
41
42   public static final int UNASSIGNED_VALUE = -1;
43
44   private static final int PDB_RES_NUM_INDEX = 0;
45
46   private static final int PDB_ATOM_NUM_INDEX = 1;
47
48   // Mapping key is residue index while value is an array containing PDB resNum,
49   // and atomNo
50   HashMap<Integer, int[]> mapping;
51
52   /**
53    * Constructor
54    * 
55    * @param seq
56    * @param pdbfile
57    * @param pdbid
58    * @param chain
59    * @param mapping
60    *          a map from sequence to two values, { resNo, atomNo } in the
61    *          structure
62    * @param mappingDetails
63    */
64   public StructureMapping(SequenceI seq, String pdbfile, String pdbid,
65           String chain, HashMap<Integer, int[]> mapping,
66           String mappingDetails)
67   {
68     sequence = seq;
69     this.pdbfile = pdbfile;
70     this.pdbid = pdbid;
71     this.pdbchain = chain;
72     this.mapping = mapping;
73     this.mappingDetails = mappingDetails;
74   }
75
76   public SequenceI getSequence()
77   {
78     return sequence;
79   }
80
81   public String getChain()
82   {
83     return pdbchain;
84   }
85
86   public String getPdbId()
87   {
88     return pdbid;
89   }
90
91   /**
92    * 
93    * @param seqpos
94    * @return 0 or corresponding atom number for the sequence position
95    */
96   public int getAtomNum(int seqpos)
97   {
98     int[] resNumAtomMap = mapping.get(seqpos);
99     if (resNumAtomMap != null)
100     {
101       return resNumAtomMap[PDB_ATOM_NUM_INDEX];
102     }
103     else
104     {
105       return UNASSIGNED_VALUE;
106     }
107   }
108
109   /**
110    * 
111    * @param seqpos
112    * @return UNASSIGNED_VALUE or the corresponding residue number for the
113    *         sequence position
114    */
115   public int getPDBResNum(int seqpos)
116   {
117     int[] resNumAtomMap = mapping.get(seqpos);
118     if (resNumAtomMap != null)
119     {
120       return resNumAtomMap[PDB_RES_NUM_INDEX];
121     }
122     else
123     {
124       return UNASSIGNED_VALUE;
125     }
126   }
127
128   /**
129    * Returns a (possibly empty) list of [start, end] residue positions in the
130    * mapped structure, corresponding to the given range of sequence positions
131    * 
132    * @param fromSeqPos
133    * @param toSeqPos
134    * @return
135    */
136   public List<int[]> getPDBResNumRanges(int fromSeqPos, int toSeqPos)
137   {
138     List<int[]> result = new ArrayList<int[]>();
139     int startRes = -1;
140     int endRes = -1;
141
142     for (int i = fromSeqPos; i <= toSeqPos; i++)
143     {
144       int resNo = getPDBResNum(i);
145       if (resNo == UNASSIGNED_VALUE)
146       {
147         continue; // no mapping from this sequence position
148       }
149       if (startRes == -1)
150       {
151         startRes = resNo;
152         endRes = resNo;
153       }
154       if (resNo >= startRes && resNo <= endRes)
155       {
156         // within the current range - no change
157         continue;
158       }
159       if (resNo == startRes - 1)
160       {
161         // extend beginning of current range
162         startRes--;
163         continue;
164       }
165       if (resNo == endRes + 1)
166       {
167         // extend end of current range
168         endRes++;
169         continue;
170       }
171
172       /*
173        * resNo is not within or contiguous with last range,
174        * so write out the last range
175        */
176       result.add(new int[] { startRes, endRes });
177       startRes = resNo;
178       endRes = resNo;
179     }
180
181     /*
182      * and add the last range
183      */
184     if (startRes != -1)
185     {
186       result.add(new int[] { startRes, endRes });
187     }
188
189     return result;
190   }
191
192   /**
193    * 
194    * @param pdbResNum
195    * @return -1 or the corresponding sequence position for a pdb residue number
196    */
197   public int getSeqPos(int pdbResNum)
198   {
199     for (Integer seqPos : mapping.keySet())
200     {
201       if (pdbResNum == getPDBResNum(seqPos))
202       {
203         return seqPos;
204       }
205     }
206     return UNASSIGNED_VALUE;
207   }
208
209   /**
210    * transfer a copy of an alignment annotation row in the PDB chain coordinate
211    * system onto the mapped sequence
212    * 
213    * @param ana
214    * @return the copy that was remapped to the mapped sequence
215    * @note this method will create a copy and add it to the dataset sequence for
216    *       the mapped sequence as well as the mapped sequence (if it is not a
217    *       dataset sequence).
218    */
219   public AlignmentAnnotation transfer(AlignmentAnnotation ana)
220   {
221     AlignmentAnnotation ala_copy = new AlignmentAnnotation(ana);
222     SequenceI ds = sequence;
223     while (ds.getDatasetSequence() != null)
224     {
225       ds = ds.getDatasetSequence();
226     }
227     // need to relocate annotation from pdb coordinates to local sequence
228     // -1,-1 doesn't look at pdbresnum but fails to remap sequence positions...
229
230     ala_copy.remap(ds, mapping, -1, -1, 0);
231     ds.addAlignmentAnnotation(ala_copy);
232     if (ds != sequence)
233     {
234       // mapping wasn't to an original dataset sequence, so we make a copy on
235       // the mapped sequence too
236       ala_copy = new AlignmentAnnotation(ala_copy);
237       sequence.addAlignmentAnnotation(ala_copy);
238     }
239     return ala_copy;
240   }
241
242   public String getMappingDetailsOutput()
243   {
244     return mappingDetails;
245   }
246
247   public HashMap<Integer, int[]> getMapping()
248   {
249     return mapping;
250   }
251 }