JAL-654 format and refactor test demonstrating PDB secondary structure on alignment...
[jalview.git] / test / jalview / ext / jmol / PDBFileWithJmolTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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.*;
24
25 import java.util.Vector;
26
27 import jalview.datamodel.Alignment;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.SequenceI;
30 import jalview.gui.AlignFrame;
31 import jalview.io.AppletFormatAdapter;
32 import jalview.io.FileLoader;
33
34 import org.junit.Test;
35
36 /**
37  * @author jimp
38  * 
39  */
40 public class PDBFileWithJmolTest
41 {
42   String[] testFile = new String[]
43   { "./examples/1GAQ.txt" }; // ,
44
45   // "./examples/DNMT1_MOUSE.pdb"
46   // };
47
48   @Test
49   public void testAlignmentLoader() throws Exception
50   {
51     for (String f : testFile)
52     {
53       FileLoader fl = new jalview.io.FileLoader(false);
54       AlignFrame af = fl
55               .LoadFileWaitTillLoaded(f, AppletFormatAdapter.FILE);
56       validateSecStrRows(af.getViewport().getAlignment());
57     }
58   }
59
60   @Test
61   public void testFileParser() throws Exception
62   {
63     for (String pdbStr : testFile)
64     {
65       PDBFileWithJmol jtest = new PDBFileWithJmol(pdbStr,
66               jalview.io.AppletFormatAdapter.FILE);
67       Vector<SequenceI> seqs = jtest.getSeqs();
68
69       assertTrue(
70               "No sequences extracted from testfile\n"
71                       + (jtest.hasWarningMessage() ? jtest.getWarningMessage()
72                               : "(No warnings raised)"), seqs != null
73                       && seqs.size() > 0);
74       for (SequenceI sq : seqs)
75       {
76         AlignmentI al = new Alignment(new SequenceI[]
77         { sq });
78         validateSecStrRows(al);
79       }
80     }
81   }
82
83   private void validateSecStrRows(AlignmentI al)
84   {
85     if (!al.isNucleotide())
86     {
87       for (SequenceI asq : al.getSequences())
88       {
89         SequenceI sq = asq;
90         boolean hasDs = false;
91         while (sq.getDatasetSequence() != null
92                 && sq.getAnnotation() == null)
93         {
94           sq = sq.getDatasetSequence();
95           hasDs = true;
96         }
97         checkFirstAAIsAssoc(sq);
98         if (hasDs)
99         {
100           // also verify if alignment sequence has annotation on it
101           // that is correctly mapped
102           checkFirstAAIsAssoc(asq);
103         }
104       }
105     }
106   }
107
108   private void checkFirstAAIsAssoc(SequenceI sq)
109   {
110     assertTrue("No secondary structure assigned for protein sequence.",
111             sq.getAnnotation() != null && sq.getAnnotation().length >= 1
112                     && sq.getAnnotation()[0].hasIcons);
113     assertTrue(
114             "Secondary structure not associated for sequence "
115                     + sq.getName(), sq.getAnnotation()[0].sequenceRef == sq);
116   }
117 }