JAL-1620 version bump and release notes
[jalview.git] / test / jalview / ext / jmol / PDBFileWithJmolTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ext.jmol;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import jalview.datamodel.Alignment;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.SequenceI;
28 import jalview.gui.AlignFrame;
29 import jalview.io.AppletFormatAdapter;
30 import jalview.io.FileLoader;
31
32 import java.util.Vector;
33
34 import org.junit.Test;
35
36 import MCview.PDBfile;
37
38 /**
39  * @author jimp
40  * 
41  */
42 public class PDBFileWithJmolTest
43 {
44   String[] testFile = new String[]
45   { "./examples/1GAQ.txt", "./test/jalview/ext/jmol/1QCF.pdb" }; // ,
46
47   // "./examples/DNMT1_MOUSE.pdb"
48   // };
49
50   @Test
51   public void testAlignmentLoader() throws Exception
52   {
53     for (String f : testFile)
54     {
55       FileLoader fl = new jalview.io.FileLoader(false);
56       AlignFrame af = fl
57               .LoadFileWaitTillLoaded(f, AppletFormatAdapter.FILE);
58       validateSecStrRows(af.getViewport().getAlignment());
59     }
60   }
61
62   @Test
63   public void testFileParser() throws Exception
64   {
65     for (String pdbStr : testFile)
66     {
67       PDBfile mctest = new PDBfile(false, false, false, pdbStr,
68               AppletFormatAdapter.FILE);
69       PDBFileWithJmol jtest = new PDBFileWithJmol(pdbStr,
70               jalview.io.AppletFormatAdapter.FILE);
71       Vector<SequenceI> seqs = jtest.getSeqs(), mcseqs = mctest.getSeqs();
72
73       assertTrue(
74               "No sequences extracted from testfile\n"
75                       + (jtest.hasWarningMessage() ? jtest.getWarningMessage()
76                               : "(No warnings raised)"), seqs != null
77                       && seqs.size() > 0);
78       for (SequenceI sq : seqs)
79       {
80         assertEquals("JMol didn't process " + pdbStr
81                 + " to the same sequence as MCView",
82                 sq.getSequenceAsString(), mcseqs.remove(0)
83                         .getSequenceAsString());
84         AlignmentI al = new Alignment(new SequenceI[]
85         { sq });
86         validateSecStrRows(al);
87       }
88     }
89   }
90
91
92   private void validateSecStrRows(AlignmentI al)
93   {
94     if (!al.isNucleotide())
95     {
96       for (SequenceI asq : al.getSequences())
97       {
98         SequenceI sq = asq;
99         boolean hasDs = false;
100         while (sq.getDatasetSequence() != null
101                 && sq.getAnnotation() == null)
102         {
103           sq = sq.getDatasetSequence();
104           hasDs = true;
105         }
106         checkFirstAAIsAssoc(sq);
107         if (hasDs)
108         {
109           // also verify if alignment sequence has annotation on it
110           // that is correctly mapped
111           checkFirstAAIsAssoc(asq);
112         }
113       }
114     }
115   }
116
117   private void checkFirstAAIsAssoc(SequenceI sq)
118   {
119     assertTrue("No secondary structure assigned for protein sequence.",
120             sq.getAnnotation() != null && sq.getAnnotation().length >= 1
121                     && sq.getAnnotation()[0].hasIcons);
122     assertTrue(
123             "Secondary structure not associated for sequence "
124                     + sq.getName(), sq.getAnnotation()[0].sequenceRef == sq);
125   }
126 }