JAL-2189 format tests
[jalview.git] / test / jalview / ext / jmol / JmolVsJalviewPDBParserEndToEndTest.java
1 package jalview.ext.jmol;
2
3 import jalview.datamodel.SequenceI;
4 import jalview.io.AppletFormatAdapter;
5
6 import java.io.File;
7 import java.io.IOException;
8 import java.util.HashSet;
9 import java.util.Set;
10 import java.util.Vector;
11
12 import MCview.PDBfile;
13
14 /**
15  * This is not a unit test, rather it is a bulk End-to-End scan for sequences
16  * consistency for PDB files parsed with JmolParser vs. Jalview's PDBfile
17  * parser. The directory of PDB files to test must be provided in the launch
18  * args.
19  * 
20  * @author tcnofoegbu
21  *
22  */
23 public class JmolVsJalviewPDBParserEndToEndTest
24 {
25
26   public static void main(String[] args)
27   {
28     if (args == null || args[0] == null)
29     {
30       System.err
31               .println("You must provide a PDB directory in the launch argument");
32       return;
33     }
34
35     // args[0] must provide the directory of PDB files to run the test with
36     String testDir = args[0];
37     System.out.println("PDB directory : " + testDir);
38     File pdbDir = new File(testDir);
39     String testFiles[] = pdbDir.list();
40     testFileParser(testDir, testFiles);
41   }
42
43   public static void testFileParser(String testDir, String[] testFiles)
44   {
45     Set<String> failedFiles = new HashSet<String>();
46     int totalSeqScanned = 0, totalFail = 0;
47     for (String pdbStr : testFiles)
48     {
49       String testFile = testDir + "/" + pdbStr;
50       PDBfile mctest = null;
51       JmolParser jtest = null;
52       try
53       {
54         mctest = new PDBfile(false, false, false, testFile,
55                 AppletFormatAdapter.FILE);
56         jtest = new JmolParser(testFile, AppletFormatAdapter.FILE);
57       } catch (IOException e)
58       {
59         System.err.println("Exception thrown while parsing : " + pdbStr);
60       }
61       Vector<SequenceI> seqs = jtest.getSeqs();
62       Vector<SequenceI> mcseqs = mctest.getSeqs();
63
64       for (SequenceI sq : seqs)
65       {
66         try
67         {
68           String testSeq = mcseqs.remove(0).getSequenceAsString();
69           if (!sq.getSequenceAsString().equals(testSeq))
70           {
71             ++totalFail;
72             System.err.println("Test Failed for " + pdbStr + ". Diff:");
73             System.err.println(sq.getSequenceAsString());
74             System.err.println(testSeq);
75             failedFiles.add(pdbStr);
76           }
77           ++totalSeqScanned;
78         } catch (Exception e)
79         {
80           e.printStackTrace();
81         }
82       }
83     }
84     int count = 0;
85
86     System.out.println("\n\nTotal sequence Scanned : " + totalSeqScanned);
87     System.out.println("Total sequence passed : "
88             + (totalSeqScanned - totalFail));
89     System.out.println("Total sequence failed : " + totalFail);
90     System.out
91             .println("Success rate: "
92                     + ((totalSeqScanned - totalFail) * 100)
93                     / totalSeqScanned + "%");
94     System.out.println("\nList of " + failedFiles.size()
95             + " file(s) with sequence diffs:");
96     for (String problemFile : failedFiles)
97     {
98       System.out.println(++count + ". " + problemFile);
99     }
100   }
101 }