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