Merge branch 'develop' into features/JAL-653_JAL-1766_htslib_refseqsupport
[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     assertNotEquals("Expected at least one DNA protein association",
63             0, af.getViewport().getAlignment().getDataset()
64             .getCodonFrames().size(),
65             );
66
67   }
68
69   @Test(groups = { "Functional" })
70   public void simpleGff3FileIdentify()
71   {
72     assertEquals("Didn't recognise file correctly.", IdentifyFile.GFF3File,
73             new IdentifyFile().Identify(simpleGff3file, FormatAdapter.FILE));
74   }
75
76   @Test(groups = { "Functional" })
77   public void simpleGff3FileClass() throws IOException
78   {
79     AlignmentI dataset = new Alignment(new SequenceI[] {});
80     FeaturesFile ffile = new FeaturesFile(simpleGff3file,
81             FormatAdapter.FILE);
82
83     boolean parseResult = ffile.parse(dataset, null, null, false, false);
84     assertTrue("return result should be true", parseResult);
85     checkDatasetfromSimpleGff3(dataset);
86   }
87
88   @Test(groups = { "Functional" })
89   public void simpleGff3FileLoader() throws IOException
90   {
91     AlignFrame af = new FileLoader(false).LoadFileWaitTillLoaded(
92             simpleGff3file, FormatAdapter.FILE);
93     assertTrue(
94             "Didn't read the alignment into an alignframe from Gff3 File",
95             af != null);
96     checkDatasetfromSimpleGff3(af.getViewport().getAlignment().getDataset());
97   }
98
99   @Test(groups = { "Functional" })
100   public void simpleGff3RelaxedIdMatching() throws IOException
101   {
102     AlignmentI dataset = new Alignment(new SequenceI[] {});
103     FeaturesFile ffile = new FeaturesFile(simpleGff3file,
104             FormatAdapter.FILE);
105
106     boolean parseResult = ffile.parse(dataset, null, null, false, true);
107     assertTrue("return result (relaxedID matching) should be true",
108             parseResult);
109     checkDatasetfromSimpleGff3(dataset);
110   }
111
112   @Test(groups = { "Functional" })
113   public void readGff3File() throws IOException
114   {
115     Gff3File gff3reader = new Gff3File(simpleGff3file, FormatAdapter.FILE);
116     Alignment dataset = new Alignment(gff3reader.getSeqsAsArray());
117     gff3reader.addProperties(dataset);
118     checkDatasetfromSimpleGff3(dataset);
119
120   }
121
122   private void checkDatasetfromSimpleGff3(AlignmentI dataset)
123   {
124     assertEquals("no sequences extracted from GFF3 file", 2,
125             dataset.getHeight());
126
127     SequenceI seq1 = dataset.findName("seq1"), seq2 = dataset
128             .findName("seq2");
129     assertNotNull(seq1);
130     assertNotNull(seq2);
131     assertFalse(
132             "Failed to replace dummy seq1 with real sequence",
133             seq1 instanceof SequenceDummy
134                     && ((SequenceDummy) seq1).isDummy());
135     assertFalse(
136             "Failed to replace dummy seq2 with real sequence",
137             seq2 instanceof SequenceDummy
138             && ((SequenceDummy) seq2).isDummy(),
139             "Failed to replace dummy seq2 with real sequence");
140     String placeholderseq = new SequenceDummy("foo").getSequenceAsString();
141     assertFalse("dummy replacement buggy for seq1",
142             placeholderseq.equals(seq1.getSequenceAsString()));
143     assertFalse("dummy replacement buggy for seq2",
144             placeholderseq.equals(seq2.getSequenceAsString()));
145     assertNotNull("No features added to seq1", seq1.getSequenceFeatures());
146     assertEquals("Wrong number of features", 3,
147             seq1.getSequenceFeatures().length);
148     assertNull(seq2.getSequenceFeatures());
149     assertEquals(
150             "Wrong number of features",
151             0,
152             seq2.getSequenceFeatures() == null ? 0 : seq2
153                     .getSequenceFeatures().length);
154     assertTrue(
155             "Expected at least one CDNA/Protein mapping for seq1",
156             dataset.getCodonFrame(seq1) != null
157             && dataset.getCodonFrame(seq1).size() > 0,
158             "Expected at least one CDNA/Protein mapping for seq1");
159
160   }
161   // @Test(groups ={ "Functional" })
162   // public final void testPrintGFFFormatSequenceIArrayMapOfStringObject()
163   // {
164   // fail("Not yet implemented");
165   // }
166   //
167   // @Test(groups ={ "Functional" })
168   // public final void testAlignFileBooleanStringString()
169   // {
170   // fail("Not yet implemented");
171   // }
172
173 }