JAL-1894 update year/version in copyright
[jalview.git] / test / jalview / io / FeaturesFileTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (2.9.0b1)
3  * Copyright (C) 2015 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.io;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNotNull;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.SequenceFeature;
29 import jalview.gui.AlignFrame;
30
31 import java.awt.Color;
32 import java.io.File;
33 import java.io.IOException;
34 import java.util.Map;
35
36 import org.testng.annotations.Test;
37
38 public class FeaturesFileTest
39 {
40
41   static String TestFiles[][] = { { "Test example features import/export",
42       "examples/uniref50.fa", "examples/exampleFeatures.txt" } };
43
44   @Test(groups = { "Functional" })
45   public void testParse() throws Exception
46   {
47     testFeaturesFileIO("Features file test");
48   }
49
50   public static AlignmentI readAlignmentFile(File f) throws IOException
51   {
52     System.out.println("Reading file: " + f);
53     String ff = f.getPath();
54     FormatAdapter rf = new FormatAdapter();
55
56     AlignmentI al = rf.readFile(ff, AppletFormatAdapter.FILE,
57             new IdentifyFile().Identify(ff, AppletFormatAdapter.FILE));
58
59     al.setDataset(null); // creates dataset sequences
60     assertNotNull("Couldn't read supplied alignment data.", al);
61     return al;
62   }
63
64   /**
65    * Helper method for testing
66    * 
67    * @param testname
68    * @param f
69    *          alignment file
70    * @param featFile
71    *          features file to load on to the alignment
72    * @throws IOException
73    */
74   public static void testFeaturesFileIO(String testname) throws IOException
75   {
76     File f = new File("examples/uniref50.fa");
77     AlignmentI al = readAlignmentFile(f);
78     AlignFrame af = new AlignFrame(al, 500, 500);
79     Map<String, Object> colours = af.getFeatureRenderer()
80             .getFeatureColours();
81     FeaturesFile featuresFile = new FeaturesFile(
82             "examples/exampleFeatures.txt", FormatAdapter.FILE);
83     assertTrue("Test " + testname + "\nFailed to parse features file.",
84             featuresFile.parse(al.getDataset(), colours, true));
85
86     /*
87      * Refetch the colour map from the FeatureRenderer (to confirm it has been
88      * updated - JAL-1904), and verify (some) feature group colours
89      */
90     colours = af.getFeatureRenderer().getFeatureColours();
91     assertEquals("26 feature group colours not found", 26, colours.size());
92     assertEquals(colours.get("Cath"), new Color(0x93b1d1));
93     assertEquals(colours.get("ASX-MOTIF"), new Color(0x6addbb));
94
95     /*
96      * verify (some) features on sequences
97      */
98     SequenceFeature[] sfs = al.getSequenceAt(0).getDatasetSequence()
99             .getSequenceFeatures(); // FER_CAPAA
100     assertEquals(7, sfs.length);
101     SequenceFeature sf = sfs[0];
102     assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
103     assertEquals(39, sf.begin);
104     assertEquals(39, sf.end);
105     assertEquals("uniprot", sf.featureGroup);
106     assertEquals("METAL", sf.type);
107     sf = sfs[1];
108     assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
109     assertEquals(44, sf.begin);
110     assertEquals(44, sf.end);
111     assertEquals("uniprot", sf.featureGroup);
112     assertEquals("METAL", sf.type);
113     sf = sfs[2];
114     assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
115     assertEquals(47, sf.begin);
116     assertEquals(47, sf.end);
117     assertEquals("uniprot", sf.featureGroup);
118     assertEquals("METAL", sf.type);
119     sf = sfs[3];
120     assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
121     assertEquals(77, sf.begin);
122     assertEquals(77, sf.end);
123     assertEquals("uniprot", sf.featureGroup);
124     assertEquals("METAL", sf.type);
125     sf = sfs[4];
126     assertEquals("Fer2 Status: True Positive Pfam 8_8%LINK%",
127             sf.description);
128     assertEquals("Pfam 8_8|http://pfam.sanger.ac.uk/family/PF00111",
129             sf.links.get(0).toString());
130     assertEquals(8, sf.begin);
131     assertEquals(83, sf.end);
132     assertEquals("uniprot", sf.featureGroup);
133     assertEquals("Pfam", sf.type);
134     sf = sfs[5];
135     assertEquals("Ferredoxin_fold Status: True Positive ", sf.description);
136     assertEquals(3, sf.begin);
137     assertEquals(93, sf.end);
138     assertEquals("uniprot", sf.featureGroup);
139     assertEquals("Cath", sf.type);
140     sf = sfs[6];
141     assertEquals(
142             "High confidence server. Only hits with scores over 0.8 are reported. PHOSPHORYLATION (T) 89_8%LINK%",
143             sf.description);
144     assertEquals(
145             "PHOSPHORYLATION (T) 89_8|http://www.cbs.dtu.dk/cgi-bin/proview/webface-link?seqid=P83527&amp;service=NetPhos-2.0",
146             sf.links.get(0).toString());
147     assertEquals(89, sf.begin);
148     assertEquals(89, sf.end);
149     assertEquals("netphos", sf.featureGroup);
150     assertEquals("PHOSPHORYLATION (T)", sf.type);
151   }
152 }