JAL-674 patch test to verify presence of annotation on the sequence associated with...
[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(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     boolean hasSecStr=false,hasTemp=false;
35     for (StructureMapping origMap:mp)
36     {
37       if (origMap.getSequence()==sq)
38       {
39         assertEquals("Mapping was incomplete.", sq.getLength() - 1,
40                 (origMap.getPDBResNum(sq.getEnd()) - origMap
41                         .getPDBResNum(sq.getStart())));
42         // sanity check - if this fails, mapping from first position in sequence
43         // we want to transfer to is not where we expect
44         assertEquals(1, origMap.getSeqPos(126));
45         SequenceI firstChain = pde.getSeqs().get(0);
46         // Compare the annotated positions on the PDB chain sequence with the
47         // annotation on the associated sequence
48         for (AlignmentAnnotation alan : firstChain.getAnnotation())
49         {
50           AlignmentAnnotation transfer = origMap.transfer(alan);
51           System.out.println("pdb:" + firstChain.getSequenceAsString());
52           System.out.println("ann:" + alan.toString());
53           System.out.println("pdb:" + sq.getSequenceAsString());
54           System.out.println("ann:" + transfer.toString());
55
56           for (int p = 0, pSize = firstChain.getLength() - 1; p < pSize; p++)
57           {
58             // walk along the pdb chain's jalview sequence
59             int rseqpos;
60             int fpos = origMap.getSeqPos(rseqpos = firstChain
61                     .findPosition(p));
62             // only look at positions where there is a corresponding position in
63             // mapping
64             if (fpos < 1)
65             {
66               continue;
67             }
68             // p is index into PDB residue entries
69             // rseqpos is pdb sequence position for position p
70             // fpos is sequence position for associated position for rseqpos
71             int tanpos = sq.findIndex(fpos);
72             if (transfer.annotations.length <= tanpos)
73             {
74               // gone beyond mapping to the sequence
75               break;
76             }
77             Annotation a = transfer.annotations[sq.findIndex(fpos)], b = alan.annotations[p];
78             assertEquals("Non-equivalent annotation element at " + p + "("
79                     + rseqpos + ")"
80                             + " expected at " + fpos + " (alIndex "
81                             + sq.findIndex(fpos) + ")",
82                     a == null ? a : a.toString(),
83                     b == null ? b : b.toString());
84             System.out.print("(" + a + "|" + b + ")");
85           }
86
87         }
88       }
89     }
90     hasSecStr = false;
91     hasTemp = false;
92     // test for presence of transferred annotation on sequence
93     for (AlignmentAnnotation alan : sq.getAnnotation())
94     {
95       if (alan.hasIcons)
96       {
97         hasSecStr = true;
98       }
99       if (alan.graph == alan.LINE_GRAPH)
100       {
101         hasTemp = true;
102       }
103     }
104     assertTrue("No secondary structure transferred to associated sequence.",hasSecStr);
105     assertTrue("No temperature factor transferred to associated sequence.",hasTemp);
106   }
107
108 }