Merge develop to Release_2_8_3_Branch
[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.SequenceFeature;
11 import jalview.datamodel.SequenceI;
12 import jalview.gui.AlignFrame;
13 import jalview.gui.Desktop;
14 import jalview.structure.StructureMapping;
15 import jalview.structure.StructureSelectionManager;
16
17 import java.io.File;
18 import java.util.Vector;
19
20 import org.junit.AfterClass;
21 import org.junit.Before;
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24
25 public class AnnotatedPDBFileInputTest
26 {
27
28   AlignmentI al;
29
30   String pdbStr = "examples/1gaq.txt";
31
32   String pdbId;
33
34   @Before
35   public void setup() throws Exception
36   {
37     FileLoader loader = new FileLoader(false);
38     AlignFrame af = loader.LoadFileWaitTillLoaded(pdbStr,
39             FormatAdapter.FILE);
40     al = af.getViewport().getAlignment();
41     pdbId = ((PDBEntry) al.getSequenceAt(0).getDatasetSequence().getPDBId()
42             .get(0)).getId();
43   }
44
45   @Test
46   public void checkNoDuplicates()
47   {
48     // not strictly a requirement, but strange things may happen if multiple
49     // instances of the same annotation are placed in the alignment annotation
50     // vector
51     assertNotNull(al.getAlignmentAnnotation());
52     // verify that all sequence annotation is doubly referenced
53     AlignmentAnnotation[] avec = al.getAlignmentAnnotation();
54     for (int p = 0; p < avec.length; p++)
55     {
56       for (int q = p + 1; q < avec.length; q++)
57       {
58         assertNotEquals(
59                 "Found a duplicate annotation row " + avec[p].label,
60                 avec[p], avec[q]);
61       }
62     }
63   }
64
65   @Test
66   public void checkPDBannotationSource()
67   {
68
69     for (SequenceI asq : al.getSequences())
70     {
71       for (AlignmentAnnotation aa : asq.getAnnotation())
72       {
73
74         System.out.println("CalcId: " + aa.getCalcId());
75         assertTrue(MCview.PDBfile.isCalcIdForFile(aa, pdbId));
76       }
77     }
78   }
79
80   /**
81    * Check sequence features have been added
82    */
83   @Test
84   public void checkPDBSequenceFeatures()
85   {
86     StructureSelectionManager ssm = StructureSelectionManager
87             .getStructureSelectionManager(Desktop.instance);
88     StructureMapping[] mappings = ssm.getMapping("1gaq");
89     // suspect we really want to make assertions on sequence features
90     // in these mappings' sequencess
91     /*
92      * 1GAQ/A
93      */
94     SequenceFeature[] sf = al.getSequenceAt(0).getSequenceFeatures();
95     assertEquals(296, sf.length);
96     assertEquals("RESNUM", sf[0].getType());
97     assertEquals("GLU:19 1gaqA", sf[0].getDescription());
98     assertEquals("RESNUM", sf[295].getType());
99     assertEquals("TYR:314 1gaqA", sf[295].getDescription());
100
101     /*
102      * 1GAQ/B
103      */
104     sf = al.getSequenceAt(1).getSequenceFeatures();
105     assertEquals(98, sf.length);
106     assertEquals("RESNUM", sf[0].getType());
107     assertEquals("ALA:1 1gaqB", sf[0].getDescription());
108     assertEquals("RESNUM", sf[97].getType());
109     assertEquals("ALA:98 1gaqB", sf[97].getDescription());
110
111     /*
112      * 1GAQ/C
113      */
114     sf = al.getSequenceAt(2).getSequenceFeatures();
115     assertEquals(296, sf.length);
116     assertEquals("RESNUM", sf[0].getType());
117     assertEquals("GLU:19 1gaqC", sf[0].getDescription());
118     assertEquals("RESNUM", sf[295].getType());
119     assertEquals("TYR:314 1gaqC", sf[295].getDescription());
120   }
121
122   @Test
123   public void checkAnnotationWiring()
124   {
125     assertTrue(al.getAlignmentAnnotation() != null);
126     // verify that all sequence annotation is doubly referenced
127     for (AlignmentAnnotation aa : al.getAlignmentAnnotation())
128     {
129       if (aa.sequenceRef != null)
130       {
131         assertTrue(al.getSequences().contains(aa.sequenceRef));
132         assertNotNull(aa.sequenceRef.getAnnotation());
133         boolean found = false;
134         for (AlignmentAnnotation sqan : aa.sequenceRef.getAnnotation())
135         {
136           if (sqan == aa)
137           {
138             found = true;
139             break;
140           }
141         }
142         assertTrue(
143                 "Couldn't find sequence associated annotation "
144                         + aa.label
145                         + " on the sequence it is associated with.\nSequence associated editing will fail.",
146                 found);
147       }
148     }
149   }
150
151   /**
152    * @throws java.lang.Exception
153    */
154   @BeforeClass
155   public static void setUpBeforeClass() throws Exception
156   {
157     jalview.bin.Jalview.main(new String[]
158     { "-props", "test/src/jalview/io/testProps.jvprops" });
159   }
160
161   /**
162    * @throws java.lang.Exception
163    */
164   @AfterClass
165   public static void tearDownAfterClass() throws Exception
166   {
167     jalview.gui.Desktop.instance.closeAll_actionPerformed(null);
168
169   }
170
171   @Test
172   public void testJalviewProjectRelocationAnnotation() throws Exception
173   {
174
175     String inFile = "examples/1gaq.txt";
176     String tfile = File.createTempFile("JalviewTest", ".jvp")
177             .getAbsolutePath();
178     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
179             inFile, FormatAdapter.FILE);
180     assertTrue("Didn't read input file " + inFile, af != null);
181     assertTrue("Failed to store as a project.",
182             af.saveAlignment(tfile, "Jalview"));
183     af.closeMenuItem_actionPerformed(true);
184     af = null;
185     af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(tfile,
186             FormatAdapter.FILE);
187     assertTrue("Failed to import new project", af != null);
188     for (SequenceI asq : af.getViewport().getAlignment().getSequences())
189     {
190       SequenceI sq = asq;
191       while (sq.getDatasetSequence() != null)
192       {
193         sq = sq.getDatasetSequence();
194       }
195       assertNotNull(sq.getPDBId());
196       assertEquals("Expected only one PDB ID", sq.getPDBId().size(), 1);
197       for (PDBEntry pdbentry : (Vector<PDBEntry>) sq.getPDBId())
198       {
199         System.err.println("PDB Entry " + pdbentry.getId() + " "
200                 + pdbentry.getFile());
201         boolean exists = false, found = false;
202         for (AlignmentAnnotation ana : sq.getAnnotation())
203         {
204           System.err.println("CalcId " + ana.getCalcId());
205           if (ana.getCalcId() != null
206                   && MCview.PDBfile.isCalcIdHandled(ana.getCalcId()))
207           {
208             exists = true;
209             if (MCview.PDBfile.isCalcIdForFile(ana,
210                     pdbentry.getId()))
211             {
212               found = true;
213             }
214           }
215         }
216         if (exists)
217         {
218           assertTrue("Couldn't find any annotation for " + pdbentry.getId()
219                   + " (file handle " + pdbentry.getFile() + ")", found);
220         }
221       }
222     }
223   }
224 }