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