b78a0049a6d072ec8d7306126f9037c14727f135
[jalview.git] / test / jalview / io / Gff3tests.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.assertFalse;
25 import static org.testng.AssertJUnit.assertNotNull;
26 import static org.testng.AssertJUnit.assertNull;
27 import static org.testng.AssertJUnit.assertTrue;
28
29 import jalview.datamodel.Alignment;
30 import jalview.datamodel.AlignmentI;
31 import jalview.datamodel.SequenceDummy;
32 import jalview.datamodel.SequenceI;
33 import jalview.gui.AlignFrame;
34
35 import java.io.IOException;
36
37 import org.testng.annotations.Test;
38
39 public class Gff3tests
40 {
41
42   private static String exonerateSeqs = "examples/testdata/exonerateseqs.fa",
43           exonerateOutput = "examples/testdata/exonerateoutput.gff",
44           simpleGff3file = "examples/testdata/simpleGff3.gff";
45
46   @Test(groups = { "Functional" })
47   public void testExonerateImport()
48   {
49     // exonerate does not tag sequences after features, so we have a more
50     // conventional annotation import test here
51
52     FileLoader loader = new FileLoader(false);
53
54     AlignFrame af = loader.LoadFileWaitTillLoaded(exonerateSeqs,
55             FormatAdapter.FILE);
56
57     assertEquals("Unexpected number of DNA protein associations", 0, af
58             .getViewport().getAlignment().getCodonFrames().size());
59
60     af.loadJalviewDataFile(exonerateOutput, FormatAdapter.FILE, null, null);
61
62     assertTrue("Expected at least one DNA protein association", af
63             .getViewport().getAlignment().getDataset().getCodonFrames()
64             .size() > 0);
65
66   }
67
68   @Test(groups = { "Functional" })
69   public void simpleGff3FileIdentify()
70   {
71     assertEquals("Didn't recognise file correctly.", IdentifyFile.GFF3File,
72             new IdentifyFile().Identify(simpleGff3file, FormatAdapter.FILE));
73   }
74
75   @Test(groups = { "Functional" })
76   public void simpleGff3FileClass() throws IOException
77   {
78     AlignmentI dataset = new Alignment(new SequenceI[] {});
79     FeaturesFile ffile = new FeaturesFile(simpleGff3file,
80             FormatAdapter.FILE);
81
82     boolean parseResult = ffile.parse(dataset, null, null, false, false);
83     assertTrue("return result should be true", parseResult);
84     checkDatasetfromSimpleGff3(dataset);
85   }
86
87   @Test(groups = { "Functional" })
88   public void simpleGff3FileLoader() throws IOException
89   {
90     AlignFrame af = new FileLoader(false).LoadFileWaitTillLoaded(
91             simpleGff3file, FormatAdapter.FILE);
92     assertTrue(
93             "Didn't read the alignment into an alignframe from Gff3 File",
94             af != null);
95     checkDatasetfromSimpleGff3(af.getViewport().getAlignment().getDataset());
96   }
97
98   @Test(groups = { "Functional" })
99   public void simpleGff3RelaxedIdMatching() throws IOException
100   {
101     AlignmentI dataset = new Alignment(new SequenceI[] {});
102     FeaturesFile ffile = new FeaturesFile(simpleGff3file,
103             FormatAdapter.FILE);
104
105     boolean parseResult = ffile.parse(dataset, null, null, false, true);
106     assertTrue("return result (relaxedID matching) should be true",
107             parseResult);
108     checkDatasetfromSimpleGff3(dataset);
109   }
110
111   @Test(groups = { "Functional" })
112   public void readGff3File() throws IOException
113   {
114     Gff3File gff3reader = new Gff3File(simpleGff3file, FormatAdapter.FILE);
115     Alignment dataset = new Alignment(gff3reader.getSeqsAsArray());
116     gff3reader.addProperties(dataset);
117     checkDatasetfromSimpleGff3(dataset);
118
119   }
120
121   private void checkDatasetfromSimpleGff3(AlignmentI dataset)
122   {
123     assertEquals("no sequences extracted from GFF3 file", 2,
124             dataset.getHeight());
125
126     SequenceI seq1 = dataset.findName("seq1"), seq2 = dataset
127             .findName("seq2");
128     assertNotNull(seq1);
129     assertNotNull(seq2);
130     assertFalse(
131             "Failed to replace dummy seq1 with real sequence",
132             seq1 instanceof SequenceDummy
133                     && ((SequenceDummy) seq1).isDummy());
134     assertFalse(
135             "Failed to replace dummy seq2 with real sequence",
136             seq2 instanceof SequenceDummy
137                     && ((SequenceDummy) seq2).isDummy());
138     String placeholderseq = new SequenceDummy("foo").getSequenceAsString();
139     assertFalse("dummy replacement buggy for seq1",
140             placeholderseq.equals(seq1.getSequenceAsString()));
141     assertFalse("dummy replacement buggy for seq2",
142             placeholderseq.equals(seq2.getSequenceAsString()));
143     assertNotNull("No features added to seq1", seq1.getSequenceFeatures());// !=
144                                                                            // null);
145     assertEquals("Wrong number of features", 3,
146             seq1.getSequenceFeatures().length);
147     assertNull(seq2.getSequenceFeatures());
148     assertEquals(
149             "Wrong number of features",
150             0,
151             seq2.getSequenceFeatures() == null ? 0 : seq2
152                     .getSequenceFeatures().length);
153     assertTrue(
154             "Expected at least one CDNA/Protein mapping for seq1",
155             dataset.getCodonFrame(seq1) != null
156                     && dataset.getCodonFrame(seq1).size() > 0);
157
158   }
159   // @Test(groups ={ "Functional" })
160   // public final void testPrintGFFFormatSequenceIArrayMapOfStringObject()
161   // {
162   // fail("Not yet implemented");
163   // }
164   //
165   // @Test(groups ={ "Functional" })
166   // public final void testAlignFileBooleanStringString()
167   // {
168   // fail("Not yet implemented");
169   // }
170
171 }