JAL-2089 patch broken merge to master for Release 2.10.0b1
[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.HashMap;
27
28 public class StructureMapping
29 {
30   String mappingDetails;
31
32   SequenceI sequence;
33
34   String pdbfile;
35
36   String pdbid;
37
38   String pdbchain;
39
40   public static final int UNASSIGNED_VALUE = -1;
41
42   private static final int PDB_RES_NUM_INDEX = 0;
43
44   private static final int PDB_ATOM_NUM_INDEX = 1;
45
46   // Mapping key is residue index while value is an array containing PDB resNum,
47   // and atomNo
48   HashMap<Integer, int[]> mapping;
49
50   public StructureMapping(SequenceI seq, String pdbfile, String pdbid,
51           String chain, HashMap<Integer, int[]> mapping,
52           String mappingDetails)
53   {
54     sequence = seq;
55     this.pdbfile = pdbfile;
56     this.pdbid = pdbid;
57     this.pdbchain = chain;
58     this.mapping = mapping;
59     this.mappingDetails = mappingDetails;
60   }
61
62   public SequenceI getSequence()
63   {
64     return sequence;
65   }
66
67   public String getChain()
68   {
69     return pdbchain;
70   }
71
72   public String getPdbId()
73   {
74     return pdbid;
75   }
76
77   /**
78    * 
79    * @param seqpos
80    * @return 0 or corresponding atom number for the sequence position
81    */
82   public int getAtomNum(int seqpos)
83   {
84     int[] resNumAtomMap = mapping.get(seqpos);
85     if (resNumAtomMap != null)
86     {
87       return resNumAtomMap[PDB_ATOM_NUM_INDEX];
88     }
89     else
90     {
91       return UNASSIGNED_VALUE;
92     }
93   }
94
95   /**
96    * 
97    * @param seqpos
98    * @return 0 or the corresponding residue number for the sequence position
99    */
100   public int getPDBResNum(int seqpos)
101   {
102     int[] resNumAtomMap = mapping.get(seqpos);
103     if (resNumAtomMap != null)
104     {
105       return resNumAtomMap[PDB_RES_NUM_INDEX];
106     }
107     else
108     {
109       return UNASSIGNED_VALUE;
110     }
111   }
112
113   /**
114    * 
115    * @param pdbResNum
116    * @return -1 or the corresponding sequence position for a pdb residue number
117    */
118   public int getSeqPos(int pdbResNum)
119   {
120     for (Integer seqPos : mapping.keySet())
121     {
122       if (pdbResNum == getPDBResNum(seqPos))
123       {
124         return seqPos;
125       }
126     }
127     return UNASSIGNED_VALUE;
128   }
129
130   /**
131    * transfer a copy of an alignment annotation row in the PDB chain coordinate
132    * system onto the mapped sequence
133    * 
134    * @param ana
135    * @return the copy that was remapped to the mapped sequence
136    * @note this method will create a copy and add it to the dataset sequence for
137    *       the mapped sequence as well as the mapped sequence (if it is not a
138    *       dataset sequence).
139    */
140   public AlignmentAnnotation transfer(AlignmentAnnotation ana)
141   {
142     AlignmentAnnotation ala_copy = new AlignmentAnnotation(ana);
143     SequenceI ds = sequence;
144     while (ds.getDatasetSequence() != null)
145     {
146       ds = ds.getDatasetSequence();
147     }
148     // need to relocate annotation from pdb coordinates to local sequence
149     // -1,-1 doesn't look at pdbresnum but fails to remap sequence positions...
150
151     ala_copy.remap(ds, mapping, -1, -1, 0);
152     ds.addAlignmentAnnotation(ala_copy);
153     if (ds != sequence)
154     {
155       // mapping wasn't to an original dataset sequence, so we make a copy on
156       // the mapped sequence too
157       ala_copy = new AlignmentAnnotation(ala_copy);
158       sequence.addAlignmentAnnotation(ala_copy);
159     }
160     return ala_copy;
161   }
162
163   public String getMappingDetailsOutput()
164   {
165     return mappingDetails;
166   }
167
168   public HashMap<Integer, int[]> getMapping()
169   {
170     return mapping;
171   }
172 }