4bee3f5ad73614f1912c83110d8b78e4e9592cdc
[jalview.git] / test / jalview / structure / Mapping.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 static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertTrue;
25
26 import jalview.datamodel.AlignmentAnnotation;
27 import jalview.datamodel.Annotation;
28 import jalview.datamodel.Sequence;
29 import jalview.datamodel.SequenceI;
30 import jalview.gui.AlignFrame;
31 import jalview.gui.JvOptionPane;
32 import jalview.io.DataSourceType;
33 import jalview.io.FileFormat;
34 import jalview.io.FileLoader;
35 import jalview.io.StructureFile;
36
37 import org.testng.Assert;
38 import org.testng.AssertJUnit;
39 import org.testng.annotations.BeforeClass;
40 import org.testng.annotations.Test;
41
42 public class Mapping
43 {
44
45   @BeforeClass(alwaysRun = true)
46   public void setUpJvOptionPane()
47   {
48     JvOptionPane.setInteractiveMode(false);
49     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
50   }
51
52   /*
53    * more test data
54    * 
55    * 1QCF|A/101-121 SFQKGDQMVVLEESGEWWKAR Ser 114 jumps to Gly 116 at position
56    * 115 in PDB Res Numbering secondary structure numbers in jmol seem to be in
57    * msd numbering, not pdb res numbering.
58    */
59   @Test(groups = { "Functional" }, enabled = false)
60   public void pdbEntryPositionMap() throws Exception
61   {
62     Assert.fail("This test intentionally left to fail");
63     for (int offset = 0; offset < 20; offset += 6)
64     {
65       // check we put the secondary structure in the right position
66       Sequence uprot = new Sequence("TheProtSeq",
67               "DAWEIPRESLKLEKKLGAGQFGEVWMATYNKHTKVAVKTMKPGSMSVEAFLAEANVMKTL");
68       uprot.setStart(offset + 258); // make it harder - create a fake
69                                     // relocation problem for jalview to
70                                     // deal with
71       uprot.setEnd(uprot.getStart() + uprot.getLength() - 1);
72       // original numbers taken from
73       // http://www.ebi.ac.uk/pdbe-srv/view/entry/1qcf/secondary.html
74       // these are in numbering relative to the subsequence above
75       int coils[] = { 266, 275, 278, 287, 289, 298, 302, 316 }, helices[] = new int[]
76       { 303, 315 }, sheets[] = new int[] { 267, 268, 269, 270 };
77
78       StructureSelectionManager ssm = new jalview.structure.StructureSelectionManager();
79       StructureFile pmap = ssm.setMapping(true, new SequenceI[] { uprot },
80               new String[] { "A" }, "test/jalview/ext/jmol/1QCF.pdb",
81               DataSourceType.FILE);
82       assertTrue(pmap != null);
83       SequenceI protseq = pmap.getSeqsAsArray()[0];
84       AlignmentAnnotation pstra = protseq
85               .getAnnotation("Secondary Structure")[0];
86       int pinds, pinde;
87       pstra.restrict((pinds = protseq.findIndex(258) - 1),
88               pinde = (protseq.findIndex(317) - 1));
89       int op;
90       System.out.println("PDB Annot");
91       for (char c : protseq.getSubSequence(pinds, pinde).getSequence())
92       {
93         System.out.print(c + ", ");
94       }
95       System.out.println("\n" + pstra + "\n\nsubsequence\n");
96       for (char c : uprot.getSequence())
97       {
98         System.out.print(c + ", ");
99       }
100       System.out.println("");
101       for (AlignmentAnnotation ss : uprot
102               .getAnnotation("Secondary Structure"))
103       {
104         ss.adjustForAlignment();
105         System.out.println("Uniprot Annot\n" + ss);
106         assertTrue(ss.hasIcons);
107         char expected = 'H';
108         for (int p : helices)
109         {
110           Annotation a = ss.annotations[op = (uprot.findIndex(offset + p) - 1)];
111           assertTrue(
112                   "Expected a helix at position " + p + uprot.getCharAt(op)
113                           + " but got coil", a != null);
114           assertEquals("Expected a helix at position " + p,
115                   a.secondaryStructure, expected);
116         }
117         expected = 'E';
118         for (int p : sheets)
119         {
120           Annotation a = ss.annotations[uprot.findIndex(offset + p) - 1];
121           assertTrue(
122                   "Expected a strand at position " + p + " but got coil",
123                   a != null);
124           assertEquals("Expected a strand at position " + p,
125                   a.secondaryStructure, expected);
126         }
127         expected = ' ';
128         for (int p : coils)
129         {
130           Annotation a = ss.annotations[uprot.findIndex(offset + p) - 1];
131           assertTrue("Expected coil at position " + p + " but got "
132                   + a.secondaryStructure, a == null);
133         }
134       }
135     }
136   }
137
138   @Test(groups = { "Functional" }, enabled = false)
139   public void testPDBentryMapping() throws Exception
140   {
141     Assert.fail("This test intentionally left to fail");
142     Sequence sq = new Sequence(
143             "1GAQ A subseq 126 to 219",
144             "EIVKGVCSNFLCDLQPGDNVQITGPVGKEMLMPKDPNATIIMLATGTGIAPFRSFLWKMFFEKHDDYKFNGLGWLFLGVPTSSSLLYKEEFGKM");
145     Sequence sq1 = new Sequence(sq);
146     String inFile;
147     StructureSelectionManager ssm = new jalview.structure.StructureSelectionManager();
148     // Associate the 1GAQ pdb file with the subsequence 'imported' from another
149     // source
150     StructureFile pde = ssm.setMapping(true, new SequenceI[] { sq },
151             new String[]
152     { "A" }, inFile = "examples/1gaq.txt", DataSourceType.FILE);
153     assertTrue("PDB File couldn't be found", pde != null);
154     StructureMapping[] mp = ssm.getMapping(inFile);
155     assertTrue("No mappings made.", mp != null && mp.length > 0);
156     int nsecStr = 0, nsTemp = 0;
157     // test for presence of transferred annotation on sequence
158     for (AlignmentAnnotation alan : sq.getAnnotation())
159     {
160       if (alan.hasIcons)
161       {
162         nsecStr++;
163       }
164       if (alan.graph == alan.LINE_GRAPH)
165       {
166         nsTemp++;
167       }
168     }
169     assertEquals(
170             "Only one secondary structure should be transferred to associated sequence.",
171             1, nsecStr);
172     assertEquals(
173             "Only two line graphs should be transferred to associated sequence.",
174             2, nsTemp);
175     // Now test the transfer function and compare annotated positions
176     for (StructureMapping origMap : mp)
177     {
178       if (origMap.getSequence() == sq)
179       {
180         assertEquals("Mapping was incomplete.", sq.getLength() - 1,
181                 (origMap.getPDBResNum(sq.getEnd()) - origMap
182                         .getPDBResNum(sq.getStart())));
183         // sanity check - if this fails, mapping from first position in sequence
184         // we want to transfer to is not where we expect
185         assertEquals(1, origMap.getSeqPos(126));
186         SequenceI firstChain = pde.getSeqs().get(0);
187         // Compare the annotated positions on the PDB chain sequence with the
188         // annotation on the associated sequence
189         for (AlignmentAnnotation alan : firstChain.getAnnotation())
190         {
191           AlignmentAnnotation transfer = origMap.transfer(alan);
192           System.out.println("pdb:" + firstChain.getSequenceAsString());
193           System.out.println("ann:" + alan.toString());
194           System.out.println("pdb:" + sq.getSequenceAsString());
195           System.out.println("ann:" + transfer.toString());
196
197           for (int p = 0, pSize = firstChain.getLength(); p < pSize; p++)
198           {
199             // walk along the pdb chain's jalview sequence
200             int rseqpos;
201             int fpos = origMap.getSeqPos(rseqpos = firstChain
202                     .findPosition(p));
203             // only look at positions where there is a corresponding position in
204             // mapping
205             if (fpos < 1)
206             {
207               continue;
208             }
209             // p is index into PDB residue entries
210             // rseqpos is pdb sequence position for position p
211             // fpos is sequence position for associated position for rseqpos
212             // tanpos is the column for the mapped sequence position
213             int tanpos = sq.findIndex(fpos) - 1;
214             if (tanpos < 0 || transfer.annotations.length <= tanpos)
215             {
216               // gone beyond mapping to the sequence
217               break;
218             }
219
220             Annotation a = transfer.annotations[tanpos], b = alan.annotations[p];
221             assertEquals("Non-equivalent annotation element at " + p + "("
222                     + rseqpos + ")" + " expected at " + fpos + " (alIndex "
223                     + tanpos + ")", a == null ? a : a.toString(),
224                     b == null ? b : b.toString());
225             System.out.print("(" + a + "|" + b + ")");
226           }
227
228         }
229       }
230     }
231   }
232
233   /**
234    * corner case for pdb mapping - revealed a problem with the AlignSeq->Mapping
235    * transform
236    * 
237    */
238   @Test(groups = { "Functional" })
239   public void mapFer1From3W5V() throws Exception
240   {
241     AlignFrame seqf = new FileLoader(false)
242             .LoadFileWaitTillLoaded(
243                     ">FER1_MAIZE/1-150 Ferredoxin-1, chloroplast precursor\nMATVLGSPRAPAFFFSSSSLRAAPAPTAVALPAAKVGIMGRSASSRRRLRAQATYNVKLITPEGEVELQVPD\nDVYILDQAEEDGIDLPYSCRAGSCSSCAGKVVSGSVDQSDQSYLDDGQIADGWVLTCHAYPTSDVVIETHKE\nEELTGA",
244                     DataSourceType.PASTE, FileFormat.Fasta);
245     SequenceI newseq = seqf.getViewport().getAlignment().getSequenceAt(0);
246     StructureSelectionManager ssm = new jalview.structure.StructureSelectionManager();
247     StructureFile pmap = ssm.setMapping(true, new SequenceI[] { newseq },
248             new String[] { null }, "examples/3W5V.pdb",
249             DataSourceType.FILE);
250     if (pmap == null)
251     {
252       AssertJUnit.fail("Couldn't make a mapping for 3W5V to FER1_MAIZE");
253     }
254   }
255
256   /**
257    * compare reference annotation for imported pdb sequence to identical
258    * seuqence with transferred annotation from mapped pdb file
259    */
260   @Test(groups = { "Functional" })
261   public void compareTransferredToRefPDBAnnot() throws Exception
262   {
263     StructureImportSettings.setProcessSecondaryStructure(true);
264     StructureImportSettings.setVisibleChainAnnotation(true);
265     StructureImportSettings.setShowSeqFeatures(true);
266     AlignFrame ref = new FileLoader(false)
267             .LoadFileWaitTillLoaded("test/jalview/ext/jmol/1QCF.pdb",
268                     DataSourceType.FILE);
269     SequenceI refseq = ref.getViewport().getAlignment().getSequenceAt(0);
270     SequenceI newseq = new Sequence(refseq.getName() + "Copy",
271             refseq.getSequenceAsString());
272     // make it harder by shifting the copy vs the reference
273     newseq.setStart(refseq.getStart() + 25);
274     newseq.setEnd(refseq.getLength() + 25 + refseq.getStart());
275     StructureSelectionManager ssm = new jalview.structure.StructureSelectionManager();
276     ssm.setProcessSecondaryStructure(true);
277     ssm.setAddTempFacAnnot(true);
278     StructureFile pmap = ssm.setMapping(true, new SequenceI[] { newseq },
279             new String[] { null }, "test/jalview/ext/jmol/1QCF.pdb",
280             DataSourceType.FILE);
281     assertTrue(pmap != null);
282     assertEquals("Original and copied sequence of different lengths.",
283             refseq.getLength(), newseq.getLength());
284     assertTrue(refseq.getAnnotation() != null
285             && refseq.getAnnotation().length > 0);
286     assertTrue(newseq.getAnnotation() != null
287             && newseq.getAnnotation().length > 0);
288     for (AlignmentAnnotation oannot : refseq.getAnnotation())
289     {
290       for (AlignmentAnnotation tannot : newseq.getAnnotation(oannot.label))
291       {
292         for (int p = 0, pSize = refseq.getLength(); p < pSize; p++)
293         {
294           Annotation orig = oannot.annotations[p], tran = tannot.annotations[p];
295           assertTrue("Mismatch: coil and non coil site " + p, orig == tran
296                   || orig != null && tran != null);
297           if (tran != null)
298           {
299             assertEquals("Mismatch in secondary structure at site " + p,
300                     tran.secondaryStructure, orig.secondaryStructure);
301           }
302         }
303       }
304     }
305   }
306 }