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
|
#!/usr/bin/env python
# test_mothur.py
from __future__ import with_statement
from cStringIO import StringIO
from os import remove, rmdir
from tempfile import mkdtemp, mkstemp
from cogent.util.unit_test import TestCase, main
from cogent.app.mothur import Mothur, mothur_from_file
__author__ = "Kyle Bittinger"
__copyright__ = "Copyright 2007-2009, The Cogent Project"
__credits__ = ["Kyle Bittinger"]
__license__ = "GPL"
__version__ = "1.4.1"
__maintainer__ = "Kyle Bittinger"
__email__ = "kylebittinger@gmail.com"
__status__ = "Prototype"
class MothurTests(TestCase):
def setUp(self):
self.small_fasta = (
'>aaaaaa\nTAGGCTCTGATATAATAGCTCTC---------\n'
'>cccccc\n------------TGACTACGCAT---------\n'
'>bbbbbb\n----TATCGCTTCGACGATTCTCTGATAGAGA\n'
)
self.small_otus = (
'unique\t3\tcccccc\tbbbbbb\taaaaaa\t\n'
'0.62\t2\tbbbbbb,cccccc\taaaaaa\t\n'
'0.67\t1\taaaaaa,bbbbbb,cccccc\t\n'
)
self.small_otus_parsed = [
(float('0'), [['cccccc'], ['bbbbbb'], ['aaaaaa']]),
(float('0.62'), [['bbbbbb', 'cccccc'], ['aaaaaa']]),
(float('0.67'), [['aaaaaa', 'bbbbbb', 'cccccc']]),
]
self.complement_fasta = (
'>a\n--AGGGGTAATAA--\n'
'>b\n--TTATTACCCCT--\n'
'>c\n-------AAAAAA--\n'
)
self.complement_otus = (
'unique\t3\tc\ta\tb\t\n'
'0.50\t2\ta,c\tb\t\n'
'1.00\t1\tb,a,c\t\n'
)
def test_get_help(self):
"""Mothur.getHelp() should return help string"""
expected_help = (
'See manual, available on the MOTHUR wiki:\n'
'http://schloss.micro.umass.edu/mothur/'
)
self.assertEqual(Mothur.getHelp(), expected_help)
def test_compile_mothur_script(self):
"""Mothur._compile_mothur_script() should return valid Mothur script"""
app = Mothur()
app._input_filename = 'test.fasta'
observed_script = app._compile_mothur_script()
expected_script = (
'#unique.seqs(fasta=test.fasta); '
'dist.seqs(fasta=test.unique.fasta); '
'read.dist(column=test.unique.dist, name=test.names); '
'cluster(method=furthest)')
self.assertEqual(observed_script, expected_script)
def test_get_result_paths(self):
"""Mothur._get_result_paths() should guess correct output paths"""
app = Mothur()
app._input_filename = 'test.fasta'
observed_paths = {
'distance matrix': app._derive_dist_path(),
'otu list': app._derive_list_path(),
'rank abundance': app._derive_rank_abundance_path(),
'species abundance': app._derive_species_abundance_path(),
'unique names': app._derive_names_path(),
'unique seqs': app._derive_unique_path(),
}
expected_paths = {
'distance matrix': 'test.unique.dist',
'otu list': 'test.unique.fn.list',
'rank abundance': 'test.unique.fn.rabund',
'species abundance': 'test.unique.fn.sabund',
'unique names': 'test.names',
'unique seqs': 'test.unique.fasta',
}
self.assertEqual(observed_paths, expected_paths)
def test_working_directory(self):
"""Mothur.WorkingDir attribute should not be cast to FilePath object"""
app = Mothur(WorkingDir='/tmp')
self.assertEquals(str(app.WorkingDir), '/tmp')
def test_call_with_multiline_string(self):
"""Mothur.__call__() should return correct otu's for input as single string"""
app = Mothur()
result = app(self.small_fasta)
observed_otus = result['otu list'].read()
self.assertEquals(observed_otus, self.small_otus)
result.cleanUp()
def test_call_with_lines(self):
"""Mothur.__call__() should return correct otu's for input as lines"""
lines = self.small_fasta.split('\n')
app = Mothur(InputHandler='_input_as_lines')
result = app(lines)
observed_otus = result['otu list'].read()
self.assertEquals(observed_otus, self.small_otus)
result.cleanUp()
def test_call_with_path(self):
"""Mothur.__call__() should return correct otu's for input as path"""
working_dir = mkdtemp()
_, filename = mkstemp(dir=working_dir, suffix='.fasta')
with open(filename, 'w') as f:
f.write(self.small_fasta)
app = Mothur(InputHandler='_input_as_path', WorkingDir=working_dir)
result = app(filename)
observed_otus = result['otu list'].read()
self.assertEquals(observed_otus, self.small_otus)
remove(filename)
result.cleanUp()
rmdir(working_dir)
def test_call_with_working_dir(self):
"""Mothur.__call__() should return correct otu's when input dir is changed"""
working_dir = mkdtemp()
app = Mothur(WorkingDir=working_dir)
result = app(self.small_fasta)
observed_otus = result['otu list'].read()
self.assertEquals(observed_otus, self.small_otus)
result.cleanUp()
rmdir(working_dir)
def test_call_with_complement(self):
"""Mothur.__call__() should return correct otu's for input sequences which are reverse complements"""
app = Mothur()
result = app(self.complement_fasta)
observed_otus = result['otu list'].read()
self.assertEquals(observed_otus, self.complement_otus)
result.cleanUp()
def test_mothur_from_file(self):
"""mothur_from_file() should return parsed otus"""
f = StringIO(self.small_fasta)
f.seek(0)
parsed_otus = mothur_from_file(f)
self.assertEquals(parsed_otus, self.small_otus_parsed)
if __name__ == '__main__':
main()
|