File: RNAToolsTest.java

package info (click to toggle)
biojava-live 1%3A1.7.1-8
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 55,160 kB
  • sloc: java: 180,820; xml: 6,908; sql: 510; makefile: 50
file content (414 lines) | stat: -rw-r--r-- 15,023 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
 *                    BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 *
 */

package org.biojava.bio.seq;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import junit.framework.TestCase;

import org.biojava.bio.Annotation;
import org.biojava.bio.BioError;
import org.biojava.bio.BioException;
import org.biojava.bio.seq.impl.SimpleSequence;
import org.biojava.bio.seq.io.SymbolTokenization;
import org.biojava.bio.symbol.Alphabet;
import org.biojava.bio.symbol.AlphabetManager;
import org.biojava.bio.symbol.FiniteAlphabet;
import org.biojava.bio.symbol.IllegalAlphabetException;
import org.biojava.bio.symbol.IllegalSymbolException;
import org.biojava.bio.symbol.ManyToOneTranslationTable;
import org.biojava.bio.symbol.SimpleGeneticCodeTable;
import org.biojava.bio.symbol.Symbol;
import org.biojava.bio.symbol.SymbolList;
import org.biojava.bio.symbol.TranslationTable;
import org.biojava.utils.ChangeListener;
import org.biojava.utils.ChangeType;
import org.biojava.utils.ChangeVetoException;

/**
 * <code>RNAToolsTest</code> tests are to ensure that the static methods
 * provided by <code>RNATools</code> work as advertised (including under
 * error conditions) and are internally consistent.
 *
 * The classes to which <code>RNATools</code> delegates e.g.
 * <code>AlphabetManager</code> and <code>SymbolTokenization</code> should
 * be tested in their own unit tests.
 *
 * @author Moses Hohman
 * @author gwaldon
 */
public class RNAToolsTest extends TestCase {
    public RNAToolsTest(String name){
      super(name);
    }
    public void testSymbols() throws IllegalSymbolException {
        assertEquals("a", getRNATokenization().tokenizeSymbol(RNATools.a()));
        assertEquals("c", getRNATokenization().tokenizeSymbol(RNATools.c()));
        assertEquals("g", getRNATokenization().tokenizeSymbol(RNATools.g()));
        assertEquals("u", getRNATokenization().tokenizeSymbol(RNATools.u()));
    }

    public void testCreateRNASequenceGetName() throws IllegalSymbolException {
        String sequenceName = "the name";
        Sequence sequence = RNATools.createRNASequence("uacg-", sequenceName);
        assertEquals(sequenceName, sequence.getName());
    }

    public void testCreateRNASequenceAnnotationIsEmpty() throws IllegalSymbolException {
        Sequence sequence = RNATools.createRNASequence("uacg-", "the name");
        assertTrue(sequence.getAnnotation().asMap().isEmpty());
    }

    public void testCreateRNASequenceURNIsBlank() throws IllegalSymbolException {
        Sequence sequence = RNATools.createRNASequence("uacg-", "the name");
        assertEquals("", sequence.getURN());
    }

    public void testCreateRNASequenceSequenceCorrect() throws IllegalSymbolException {
        String sequenceString = "uacg-";
        assertEquals(sequenceString, ((SimpleSequence) RNATools.createRNASequence(sequenceString, "the name")).seqString());
    }

    public void testGetCodonAlphabet() {
        List threeCopiesOfRNAAlphabet = Collections.nCopies(3, RNATools.getRNA());
        assertEquals(RNATools.getCodonAlphabet(), AlphabetManager.getCrossProductAlphabet(threeCopiesOfRNAAlphabet));
    }

    public void testCreateRNA() throws IllegalAlphabetException, IllegalSymbolException {
        assertEquals("uacg-", getRNATokenization().tokenizeSymbolList(RNATools.createRNA("uacg-")));
    }

    public void testComplementA() throws IllegalSymbolException {
        assertEquals(RNATools.u(), RNATools.complement(RNATools.a()));
    }

    public void testComplementC() throws IllegalSymbolException {
        assertEquals(RNATools.g(), RNATools.complement(RNATools.c()));
    }

    public void testComplementG() throws IllegalSymbolException {
        assertEquals(RNATools.c(), RNATools.complement(RNATools.g()));
    }

    public void testComplementU() throws IllegalSymbolException {
        assertEquals(RNATools.a(), RNATools.complement(RNATools.u()));
    }

    public void testComplementSymbolFromAnotherAlphabet() {
        try {
            RNATools.complement(DNATools.t());
            fail("Should have thrown an IllegalSymbolException");
        } catch (IllegalSymbolException e) {
        }
    }

    public void testComplementBadSymbol() throws IllegalSymbolException {
        try {
            RNATools.complement(createBadSymbol());
            fail("Should have thrown a BioError");
        } catch (BioError e) {
        }
    }

    public void testComplementList() throws IllegalAlphabetException, IllegalSymbolException {
        assertEquals(RNATools.createRNA("augc"), RNATools.complement(RNATools.createRNA("uacg")));
    }

    public void testReverseComplement() throws IllegalAlphabetException, IllegalSymbolException {
        assertEquals(RNATools.createRNA("cgua"), RNATools.reverseComplement(RNATools.createRNA("uacg")));
    }

    public void testTranscribe() throws IllegalAlphabetException, IllegalSymbolException {
        assertEquals(RNATools.createRNA("gcau"), RNATools.transcribe(DNATools.createDNA("gcat")));
    }

    public void testTranslate() throws IllegalAlphabetException, IllegalSymbolException {
        assertEquals(ProteinTools.createProtein(
                "FFLLSSSSYY**CC*W"
                + "LLLLPPPPHHQQRRRR"
                + "IIIMTTTTNNKKSSRR"
                + "VVVVAAAADDEEGGGG"),
                RNATools.translate(RNATools.createRNA(
                        "uuuuucuuauug" + "ucuuccucaucg" + "uauuacuaauag" + "uguugcugaugg"
                + "cuucuccuacug" + "ccucccccaccg" + "caucaccaacag" + "cgucgccgacgg"
                + "auuaucauaaug" + "acuaccacaacg" + "aauaacaaaaag" + "aguagcagaagg"
                + "guugucguagug" + "gcugccgcagcg" + "gaugacgaagag" + "gguggcggaggg"
                )));
    }

    public void testForSymbolA() throws IllegalSymbolException {
        assertEquals(RNATools.a(), RNATools.forSymbol('a'));
    }

    public void testForSymbolC() throws IllegalSymbolException {
        assertEquals(RNATools.c(), RNATools.forSymbol('c'));
    }

    public void testForSymbolG() throws IllegalSymbolException {
        assertEquals(RNATools.g(), RNATools.forSymbol('g'));
    }

    public void testForSymbolU() throws IllegalSymbolException {
        assertEquals(RNATools.u(), RNATools.forSymbol('u'));
    }

    public void testForSymbolWithBadChar() {
        try {
            RNATools.forSymbol('$');
            fail("Should have thrown an IllegalSymbolException");
        } catch (IllegalSymbolException e) {
        }
    }

    public void testForIndex0() throws IllegalSymbolException {
        assertEquals(RNATools.a(), RNATools.forIndex(0));
    }

    public void testForIndex1() throws IllegalSymbolException {
        assertEquals(RNATools.g(), RNATools.forIndex(1));
    }

    public void testForIndex2() throws IllegalSymbolException {
        assertEquals(RNATools.c(), RNATools.forIndex(2));
    }

    public void testForIndex3() throws IllegalSymbolException {
        assertEquals(RNATools.u(), RNATools.forIndex(3));
    }

    public void testForIndexWithBadIndex() {
        try {
            RNATools.forIndex(4);
            fail("Should have thrown an IndexOutOfBoundsException");
        } catch (IndexOutOfBoundsException e) {
        }
    }

    public void testIndexA() throws IllegalSymbolException {
        assertEquals(0, RNATools.index(RNATools.a()));
    }

    public void testIndexG() throws IllegalSymbolException {
        assertEquals(1, RNATools.index(RNATools.g()));
    }

    public void testIndexC() throws IllegalSymbolException {
        assertEquals(2, RNATools.index(RNATools.c()));
    }

    public void testIndexU() throws IllegalSymbolException {
        assertEquals(3, RNATools.index(RNATools.u()));
    }

    public void testIndexSymbolFromAnotherAlphabet() {
        try {
            RNATools.index(DNATools.t());
            fail("Should have thrown an IllegalSymbolException");
        } catch (IllegalSymbolException e) {
        }
    }

    public void testIndexBadSymbol() {
        try {
            RNATools.index(createBadSymbol());
            fail("Should have thrown an IllegalSymbolException");
        } catch (IllegalSymbolException e) {
        }
    }

    public void testGeneticCode() {
        try {
            // get the universal genetic code and give it a trashing
            ManyToOneTranslationTable geneticCode = RNATools.getGeneticCode(TranslationTable.UNIVERSAL);
            assertNotNull(geneticCode);

            // for the entire amino-acid alphabet, do the reverse lookup
            // and then confirm that the Set of Symbols return is correct
            FiniteAlphabet aaAlfa = ProteinTools.getTAlphabet();
            assertNotNull(aaAlfa);

            for (Iterator aaI = aaAlfa.iterator(); aaI.hasNext();) {
                Symbol residue = (Symbol) aaI.next();

                // get the List of codons that yield this amino-acid
                Set codons = geneticCode.untranslate(residue);
                assertNotNull(codons);

                // iterate thru the list confirming that they correspond
                // to the expected amino-acid
                for (Iterator codonI = codons.iterator(); codonI.hasNext(); ) {
                    Symbol codon = (Symbol) codonI.next();

                    // translate codon
                    Symbol xlatedResidue = geneticCode.translate(codon);

                    assertEquals(residue, xlatedResidue);
                }
            }
            
            // The following test is dependant of the DDBJ/EMBL/GenBank Feature Table,
            // Version 6.5  Apr 2006.
            // get all table names
            Set tableNames = RNATools.getGeneticCodeNames();
            assertEquals(17,tableNames.size()); //total 17 tables
            
            //test standard table
            ManyToOneTranslationTable geneticCode1 = RNATools.getGeneticCode(TranslationTable.UNIVERSAL);
            ManyToOneTranslationTable geneticCode2 = RNATools.getGeneticCode(1);
            assertEquals((Object)geneticCode1, (Object)geneticCode2);
            String description = ((SimpleGeneticCodeTable)geneticCode2).getDescription();
            assertEquals(description,"Standard Code");
            int number = ((SimpleGeneticCodeTable)geneticCode2).getTableNumber();
            assertEquals(1,number);
     
        }
        catch (IllegalSymbolException ise) {
        }
    }

    public void assertEquals(Symbol expected, Symbol actual) throws IllegalSymbolException {
        assertEquals(getRNATokenization().tokenizeSymbol(expected), getRNATokenization().tokenizeSymbol(actual));
    }

    public void assertEquals(SymbolList expected, SymbolList actual) throws IllegalAlphabetException, IllegalSymbolException {
        try {
            SymbolTokenization expectedTokenization = expected.getAlphabet().getTokenization("token");
            SymbolTokenization actualTokenization = actual.getAlphabet().getTokenization("token");
            assertEquals(expectedTokenization.tokenizeSymbolList(expected), actualTokenization.tokenizeSymbolList(actual));
        } catch (BioException e) {
            throw new BioError(e);
        }
    }

    private SymbolTokenization getRNATokenization() {
        try {
            return RNATools.getRNA().getTokenization("token");
        } catch (BioException e) {
            throw new BioError(e);
        }
    }

    private Symbol createBadSymbol() {
        return new Symbol() {
            public String getName() {
                return null;
            }

            public Alphabet getMatches() {
                return new FiniteAlphabet() {
                    public int size() {
                        return 0;
                    }

                    public String getName() {
                        return null;
                    }

                    public Annotation getAnnotation() {
                        return null;
                    }

                    public void addChangeListener(ChangeListener cl) {
                    }

                    public Iterator iterator() {
                        return null;
                    }

                    public List getAlphabets() {
                        return null;
                    }

                    public void addChangeListener(ChangeListener cl, ChangeType ct) {
                    }

                    public void addSymbol(Symbol s)
                            throws IllegalSymbolException, ChangeVetoException {
                    }

                    public Symbol getSymbol(List rl)
                            throws IllegalSymbolException {
                        return null;
                    }

                    public void removeChangeListener(ChangeListener cl) {
                    }

                    public void removeSymbol(Symbol s)
                            throws IllegalSymbolException, ChangeVetoException {
                    }

                    public Symbol getAmbiguity(Set syms)
                            throws IllegalSymbolException {
                        return null;
                    }

                    public void removeChangeListener(ChangeListener cl, ChangeType ct) {
                    }


                    public Symbol getGapSymbol() {
                        return null;
                    }

                    public boolean isUnchanging(ChangeType ct) {
                        return false;
                    }

                    public boolean contains(Symbol s) {
                        return false;
                    }

                    public void validate(Symbol s) throws IllegalSymbolException {
                    }

                    public SymbolTokenization getTokenization(String name) throws BioException {
                        return null;
                    }
                };
            }

            public Annotation getAnnotation() {
                return null;
            }

            public void addChangeListener(ChangeListener cl) {
            }

            public void addChangeListener(ChangeListener cl, ChangeType ct) {
            }

            public void removeChangeListener(ChangeListener cl) {
            }

            public void removeChangeListener(ChangeListener cl, ChangeType ct) {
            }

            public boolean isUnchanging(ChangeType ct) {
                return false;
            }
        };
    }
}