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