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