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