Merge branch 'documentation/JAL-2325_release2101' into develop
[jalview.git] / test / jalview / ext / jmol / JmolVsJalviewPDBParserEndToEndTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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 jalview.datamodel.SequenceI;
24 import jalview.io.AppletFormatAdapter;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.util.HashSet;
29 import java.util.Set;
30 import java.util.Vector;
31
32 import MCview.PDBfile;
33
34 /**
35  * This is not a unit test, rather it is a bulk End-to-End scan for sequences
36  * consistency for PDB files parsed with JmolParser vs. Jalview's PDBfile
37  * parser. The directory of PDB files to test must be provided in the launch
38  * args.
39  * 
40  * @author tcnofoegbu
41  *
42  */
43 public class JmolVsJalviewPDBParserEndToEndTest
44 {
45
46   public static void main(String[] args)
47   {
48     if (args == null || args[0] == null)
49     {
50       System.err
51               .println("You must provide a PDB directory in the launch argument");
52       return;
53     }
54
55     // args[0] must provide the directory of PDB files to run the test with
56     String testDir = args[0];
57     System.out.println("PDB directory : " + testDir);
58     File pdbDir = new File(testDir);
59     String testFiles[] = pdbDir.list();
60     testFileParser(testDir, testFiles);
61   }
62
63   public static void testFileParser(String testDir, String[] testFiles)
64   {
65     Set<String> failedFiles = new HashSet<String>();
66     int totalSeqScanned = 0, totalFail = 0;
67     for (String pdbStr : testFiles)
68     {
69       String testFile = testDir + "/" + pdbStr;
70       PDBfile mctest = null;
71       JmolParser jtest = null;
72       try
73       {
74         mctest = new PDBfile(false, false, false, testFile,
75                 AppletFormatAdapter.FILE);
76         jtest = new JmolParser(testFile, AppletFormatAdapter.FILE);
77       } catch (IOException e)
78       {
79         System.err.println("Exception thrown while parsing : " + pdbStr);
80       }
81       Vector<SequenceI> seqs = jtest.getSeqs();
82       Vector<SequenceI> mcseqs = mctest.getSeqs();
83
84       for (SequenceI sq : seqs)
85       {
86         try
87         {
88           String testSeq = mcseqs.remove(0).getSequenceAsString();
89           if (!sq.getSequenceAsString().equals(testSeq))
90           {
91             ++totalFail;
92             System.err.println("Test Failed for " + pdbStr + ". Diff:");
93             System.err.println(sq.getSequenceAsString());
94             System.err.println(testSeq);
95             failedFiles.add(pdbStr);
96           }
97           ++totalSeqScanned;
98         } catch (Exception e)
99         {
100           e.printStackTrace();
101         }
102       }
103     }
104     int count = 0;
105
106     System.out.println("\n\nTotal sequence Scanned : " + totalSeqScanned);
107     System.out.println("Total sequence passed : "
108             + (totalSeqScanned - totalFail));
109     System.out.println("Total sequence failed : " + totalFail);
110     System.out
111             .println("Success rate: "
112                     + ((totalSeqScanned - totalFail) * 100)
113                     / totalSeqScanned + "%");
114     System.out.println("\nList of " + failedFiles.size()
115             + " file(s) with sequence diffs:");
116     for (String problemFile : failedFiles)
117     {
118       System.out.println(++count + ". " + problemFile);
119     }
120   }
121 }