JAL-674 methods and test for PDB derived annotation stored and recovered in project
[jalview.git] / test / jalview / io / AnnotatedPDBFileInputTest.java
1 package jalview.io;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotEquals;
5 import static org.junit.Assert.assertNotNull;
6 import static org.junit.Assert.assertTrue;
7 import jalview.datamodel.AlignmentAnnotation;
8 import jalview.datamodel.AlignmentI;
9 import jalview.datamodel.PDBEntry;
10 import jalview.datamodel.SequenceI;
11 import jalview.gui.AlignFrame;
12
13 import java.io.File;
14 import java.util.Vector;
15
16 import org.junit.AfterClass;
17 import org.junit.Before;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20
21 public class AnnotatedPDBFileInputTest
22 {
23
24   AlignmentI al;
25
26   String pdbStr = "examples/2GIS.pdb";
27
28   String pdbId;
29
30   @Before
31   public void setup() throws Exception
32   {
33     FileLoader loader = new FileLoader(false);
34     AlignFrame af = loader.LoadFileWaitTillLoaded(pdbStr,
35             FormatAdapter.FILE);
36     al = af.getViewport().getAlignment();
37     pdbId = ((PDBEntry) al.getSequenceAt(0).getDatasetSequence().getPDBId()
38             .get(0)).getId();
39   }
40
41   @Test
42   public void checkNoDuplicates()
43   {
44     // not strictly a requirement, but strange things may happen if multiple
45     // instances of the same annotation are placed in the alignment annotation
46     // vector
47     assertNotNull(al.getAlignmentAnnotation());
48     // verify that all sequence annotation is doubly referenced
49     AlignmentAnnotation[] avec = al.getAlignmentAnnotation();
50     for (int p = 0; p < avec.length; p++)
51     {
52       for (int q = p + 1; q < avec.length; q++)
53       {
54         assertNotEquals(
55                 "Found a duplicate annotation row " + avec[p].label,
56                 avec[p], avec[q]);
57       }
58     }
59   }
60
61   @Test
62   public void checkPDBannotationSource()
63   {
64
65     for (SequenceI asq : al.getSequences())
66     {
67       for (AlignmentAnnotation aa : asq.getAnnotation())
68       {
69
70         System.out.println("CalcId: " + aa.getCalcId());
71         assertTrue(MCview.PDBfile.isCalcIdForFile(aa.getCalcId(), pdbId));
72       }
73     }
74   }
75
76   @Test
77   public void checkAnnotationWiring()
78   {
79     assertTrue(al.getAlignmentAnnotation() != null);
80     // verify that all sequence annotation is doubly referenced
81     for (AlignmentAnnotation aa : al.getAlignmentAnnotation())
82     {
83       if (aa.sequenceRef != null)
84       {
85         assertTrue(al.getSequences().contains(aa.sequenceRef));
86         assertNotNull(aa.sequenceRef.getAnnotation());
87         boolean found = false;
88         for (AlignmentAnnotation sqan : aa.sequenceRef.getAnnotation())
89         {
90           if (sqan == aa)
91           {
92             found = true;
93             break;
94           }
95         }
96         assertTrue(
97                 "Couldn't find sequence associated annotation "
98                         + aa.label
99                         + " on the sequence it is associated with.\nSequence associated editing will fail.",
100                 found);
101       }
102     }
103   }
104
105   /**
106    * @throws java.lang.Exception
107    */
108   @BeforeClass
109   public static void setUpBeforeClass() throws Exception
110   {
111     jalview.bin.Jalview.main(new String[]
112     { "-props", "test/src/jalview/io/testProps.jvprops" });
113   }
114
115   /**
116    * @throws java.lang.Exception
117    */
118   @AfterClass
119   public static void tearDownAfterClass() throws Exception
120   {
121     jalview.gui.Desktop.instance.closeAll_actionPerformed(null);
122
123   }
124
125   @Test
126   public void testJalviewProjectRelocationAnnotation() throws Exception
127   {
128
129     String inFile = "examples/2GIS.pdb";
130     String tfile = File.createTempFile("JalviewTest", ".jvp")
131             .getAbsolutePath();
132     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
133             inFile, FormatAdapter.FILE);
134     assertTrue("Didn't read input file " + inFile, af != null);
135     assertTrue("Failed to store as a project.",
136             af.saveAlignment(tfile, "Jalview"));
137     af.closeMenuItem_actionPerformed(true);
138     af = null;
139     af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(tfile,
140             FormatAdapter.FILE);
141     assertTrue("Failed to import new project", af != null);
142     for (SequenceI asq : af.getViewport().getAlignment().getSequences())
143     {
144       SequenceI sq = asq;
145       while (sq.getDatasetSequence() != null)
146       {
147         sq = sq.getDatasetSequence();
148       }
149       assertNotNull(sq.getPDBId());
150       assertEquals("Expected only one PDB ID", sq.getPDBId().size(), 1);
151       for (PDBEntry pdbentry : (Vector<PDBEntry>) sq.getPDBId())
152       {
153         System.err.println("PDB Entry " + pdbentry.getId() + " "
154                 + pdbentry.getFile());
155         boolean exists = false, found = false;
156         for (AlignmentAnnotation ana : sq.getAnnotation())
157         {
158           System.err.println("CalcId " + ana.getCalcId());
159           if (ana.getCalcId() != null
160                   && MCview.PDBfile.isCalcIdHandled(ana.getCalcId()))
161           {
162             exists = true;
163             if (MCview.PDBfile.isCalcIdForFile(ana.getCalcId(),
164                     pdbentry.getId()))
165             {
166               found = true;
167             }
168           }
169         }
170         if (exists)
171         {
172           assertTrue("Couldn't find any annotation for " + pdbentry.getId()
173                   + " (file handle " + pdbentry.getFile() + ")", found);
174         }
175       }
176     }
177   }
178 }