JAL-674 count number of annotation transferred in test
[jalview.git] / test / jalview / structure / Mapping.java
1 package jalview.structure;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5 import jalview.datamodel.AlignmentAnnotation;
6 import jalview.datamodel.Annotation;
7 import jalview.datamodel.Sequence;
8 import jalview.datamodel.SequenceI;
9
10 import org.junit.Test;
11
12 import MCview.PDBfile;
13
14 public class Mapping
15 {
16
17   @Test
18   public void testPDBentryMapping() throws Exception
19   {
20     Sequence sq = new Sequence(
21             "1GAQ A subseq 126 to 219",
22             "EIVKGVCSNFLCDLQPGDNVQITGPVGKEMLMPKDPNATIIMLATGTGIAPFRSFLWKMFFEKHDDYKFNGLGWLFLGVPTSSSLLYKEEFGKM");
23     Sequence sq1 = new Sequence(sq);
24     String inFile;
25     StructureSelectionManager ssm = new jalview.structure.StructureSelectionManager();
26     // Associate the 1GAQ pdb file with the subsequence 'imported' from another
27     // source
28     PDBfile pde = ssm.setMapping(true, new SequenceI[]
29     { sq }, new String[]
30     { "A" }, inFile = "examples/1gaq.txt", jalview.io.FormatAdapter.FILE);
31     assertTrue("PDB File couldn't be found", pde != null);
32     StructureMapping[] mp = ssm.getMapping(inFile);
33     assertTrue("No mappings made.", mp != null && mp.length > 0);
34     int nsecStr = 0, nsTemp = 0;
35     // test for presence of transferred annotation on sequence
36     for (AlignmentAnnotation alan : sq.getAnnotation())
37     {
38       if (alan.hasIcons)
39       {
40         nsecStr++;
41       }
42       if (alan.graph == alan.LINE_GRAPH)
43       {
44         nsTemp++;
45       }
46     }
47     assertEquals(
48             "Only one secondary structure should be transferred to associated sequence.",
49             1, nsecStr);
50     assertEquals(
51             "Only two line graphs should be transferred to associated sequence.",
52             2, nsTemp);
53     // Now test the transfer function and compare annotated positions
54     for (StructureMapping origMap:mp)
55     {
56       if (origMap.getSequence()==sq)
57       {
58         assertEquals("Mapping was incomplete.", sq.getLength() - 1,
59                 (origMap.getPDBResNum(sq.getEnd()) - origMap
60                         .getPDBResNum(sq.getStart())));
61         // sanity check - if this fails, mapping from first position in sequence
62         // we want to transfer to is not where we expect
63         assertEquals(1, origMap.getSeqPos(126));
64         SequenceI firstChain = pde.getSeqs().get(0);
65         // Compare the annotated positions on the PDB chain sequence with the
66         // annotation on the associated sequence
67         for (AlignmentAnnotation alan : firstChain.getAnnotation())
68         {
69           AlignmentAnnotation transfer = origMap.transfer(alan);
70           System.out.println("pdb:" + firstChain.getSequenceAsString());
71           System.out.println("ann:" + alan.toString());
72           System.out.println("pdb:" + sq.getSequenceAsString());
73           System.out.println("ann:" + transfer.toString());
74
75           for (int p = 0, pSize = firstChain.getLength() - 1; p < pSize; p++)
76           {
77             // walk along the pdb chain's jalview sequence
78             int rseqpos;
79             int fpos = origMap.getSeqPos(rseqpos = firstChain
80                     .findPosition(p));
81             // only look at positions where there is a corresponding position in
82             // mapping
83             if (fpos < 1)
84             {
85               continue;
86             }
87             // p is index into PDB residue entries
88             // rseqpos is pdb sequence position for position p
89             // fpos is sequence position for associated position for rseqpos
90             int tanpos = sq.findIndex(fpos);
91             if (transfer.annotations.length <= tanpos)
92             {
93               // gone beyond mapping to the sequence
94               break;
95             }
96             Annotation a = transfer.annotations[sq.findIndex(fpos)], b = alan.annotations[p];
97             assertEquals("Non-equivalent annotation element at " + p + "("
98                     + rseqpos + ")"
99                             + " expected at " + fpos + " (alIndex "
100                             + sq.findIndex(fpos) + ")",
101                     a == null ? a : a.toString(),
102                     b == null ? b : b.toString());
103             System.out.print("(" + a + "|" + b + ")");
104           }
105
106         }
107       }
108     }
109   }
110
111 }