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