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