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