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