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