JAL-1269 refactored Annotate3d test and added additional ID test case
[jalview.git] / test / jalview / ext / paradise / TestAnnotate3D.java
1 package jalview.ext.paradise;
2
3 import static org.junit.Assert.assertTrue;
4
5 import jalview.datamodel.AlignmentI;
6 import jalview.datamodel.SequenceI;
7 import jalview.ext.paradise.Annotate3D;
8 import jalview.io.FastaFile;
9 import jalview.io.FormatAdapter;
10
11 import java.io.BufferedReader;
12 import java.io.File;
13 import java.io.Reader;
14 import java.util.Iterator;
15
16 import org.junit.Assert;
17 import org.junit.Test;
18
19 import MCview.PDBfile;
20
21 import compbio.util.FileUtil;
22
23 public class TestAnnotate3D
24 {
25
26   @Test
27   public void test1GIDbyId() throws Exception
28   {
29     // use same ID as standard tests given at https://bitbucket.org/fjossinet/pyrna-rest-clients
30     Iterator<Reader> ids = Annotate3D.getRNAMLForPDBId("1GID");
31     assertTrue("Didn't retrieve 1GID by id.", ids != null);
32     testRNAMLcontent(ids,null);
33   }
34
35   @Test
36   public void testIdVsContent2GIS() throws Exception
37   {
38     Iterator<Reader> ids = Annotate3D.getRNAMLForPDBId("2GIS");
39     assertTrue("Didn't retrieve 2GIS by id.", ids != null);
40     Iterator<Reader> files = Annotate3D.getRNAMLForPDBFileAsString(FileUtil
41             .readFileToString(new File("examples/2GIS.pdb")));
42     assertTrue("Didn't retrieve using examples/2GIS.pdb.", files != null);
43     int i = 0;
44     while (ids.hasNext() && files.hasNext())
45     {
46       BufferedReader file = new BufferedReader(files.next()), id = new BufferedReader(
47               ids.next());
48       String iline, fline;
49       do
50       {
51         iline = id.readLine();
52         fline = file.readLine();
53         if (iline != null)
54           System.out.println(iline);
55         if (fline != null)
56           System.out.println(fline);
57         // next assert fails for latest RNAview - because the XMLID entries
58         // change between file and ID based RNAML generation.
59         assertTrue(
60                 "Results differ for ID and file upload based retrieval (chain entry "
61                         + (++i) + ")",
62                 ((iline == fline && iline == null) || (iline != null
63                         && fline != null && iline.equals(fline))));
64
65       } while (iline != null);
66     }
67   }
68
69   /**
70    * test to demonstrate JAL-1142 - compare sequences in RNAML returned from
71    * Annotate3d vs those extracted by Jalview from the originl PDB file
72    * 
73    * @throws Exception
74    */
75   @Test
76   public void testPDBfileVsRNAML() throws Exception
77   {
78     PDBfile pdbf = new PDBfile("examples/2GIS.pdb", FormatAdapter.FILE);
79     Assert.assertTrue(pdbf.isValid());
80     // Comment - should add new FileParse constructor like new FileParse(Reader
81     // ..). for direct reading
82     Iterator<Reader> readers = Annotate3D
83             .getRNAMLForPDBFileAsString(FileUtil.readFileToString(new File(
84                     "examples/2GIS.pdb")));
85     testRNAMLcontent(readers, pdbf);
86   }
87
88   private void testRNAMLcontent(Iterator<Reader> readers, PDBfile pdbf)
89           throws Exception
90   {
91     StringBuffer sb = new StringBuffer();
92     int r = 0;
93     while (readers.hasNext())
94     {
95       System.out.println("Testing RNAML input number " + (++r));
96       BufferedReader br = new BufferedReader(readers.next());
97       String line;
98       while ((line = br.readLine()) != null)
99       {
100         sb.append(line + "\n");
101       }
102       assertTrue("No data returned by Annotate3D", sb.length() > 0);
103       AlignmentI al = new FormatAdapter().readFile(sb.toString(),
104               FormatAdapter.PASTE, "RNAML");
105       if (al==null || al.getHeight()==0) {
106         System.out.println(sb.toString());
107       }
108       assertTrue("No alignment returned.", al != null);
109       assertTrue("No sequences in returned alignment.", al.getHeight() > 0);
110       if (pdbf != null)
111       {
112         for (SequenceI sq : al.getSequences())
113         {
114           {
115             SequenceI struseq = null;
116             String sq_ = new String(sq.getSequence()).toLowerCase();
117             for (SequenceI _struseq : pdbf.getSeqsAsArray())
118             {
119               if (new String(_struseq.getSequence()).toLowerCase().equals(
120                       sq_))
121               {
122                 struseq = _struseq;
123                 break;
124               }
125             }
126             if (struseq == null)
127             {
128               Assert.fail("Couldn't find this sequence in original input:\n"
129                       + new FastaFile().print(new SequenceI[]
130                       { sq })
131                       + "\n\nOriginal input:\n"
132                       + new FastaFile().print(pdbf.getSeqsAsArray()) + "\n");
133             }
134           }
135         }
136       }
137     }
138   }
139 }