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
|
#!/usr/bin/env python
# test_dotur.py
from os import getcwd, remove, rmdir, mkdir, path
import tempfile, shutil
from cogent.core.moltype import DNA, RNA, PROTEIN
from cogent.core.sequence import DnaSequence, RnaSequence
from cogent.core.alignment import DataError
from cogent.util.unit_test import TestCase, main
from cogent.util.misc import flatten
from cogent.app.dotur import Dotur, dotur_from_alignment, dotur_from_file,\
remap_seq_names
from cogent.parse.dotur import OtuListParser
__author__ = "Jeremy Widmann"
__copyright__ = "Copyright 2007-2009, The Cogent Project"
__credits__ = ["Jeremy Widmann"]
__license__ = "GPL"
__version__ = "1.4.1"
__maintainer__ = "Jeremy Widmann"
__email__ = "jeremy.widmann@colorado.edu"
__status__ = "Development"
def rna_distance(first,second):
first = RnaSequence(first)
return first.fracDiff(second)
class DoturTests(TestCase):
def setUp(self):
"""Dotur general setUp method for all tests"""
self.seqs1_unaligned = {'1':'ACUGCUAGCUAGUAGCGUACGUA',\
'2':'GCUACGUAGCUAC',\
'3':'GCGGCUAUUAGAUCGUA'}
self.seqs2_aligned = {'a': 'UAGGCUCUGAUAUAAUAGCUCUC---------',\
'c': '------------UGACUACGCAU---------',\
'b': '----UAUCGCUUCGACGAUUCUCUGAUAGAGA'}
self.seqs2_unaligned = {'a': 'UAGGCUCUGAUAUAAUAGCUCUC',\
'c': 'UGACUACGCAU',\
'b': 'UAUCGCUUCGACGAUUCUCUGAUAGAGA'}
#self.seqs1 aligned to self.seqs2 with self.seqs2 included.
self.seqs1_and_seqs2_aligned = \
{'a': 'UAGGCUCUGAUAUAAUAGC-UCUC---------',\
'b': '----UAUCGCUUCGACGAU-UCUCUGAUAGAGA',\
'c': '------------UGACUAC-GCAU---------',\
'1': '-ACUGCUAGCUAGUAGCGUACGUA---------',\
'2': '----------GCUACGUAG-CUAC---------',\
'3': '-----GCGGCUAUUAG-AU-CGUA---------',\
}
self.otu_list_string = \
"""unique 3 a b c
0.00 3 a b c
0.59 2 a,c b
0.78 1 a,c,b
"""
self.otu_res_list = [
[0.0,3,[['a'],['b'],['c']]],\
[0.0,3,[['a'],['b'],['c']]],\
[float(0.59),2,[['a','c'],['b']]],\
[float(0.78),1,[['a','c','b']]],\
]
self.distance_matrix_string = \
"""
3
a 0.0 0.78125 0.59375
b 0.78125 0.0 0.71875
c 0.59375 0.71875 0.0
"""
self.int_keys = {'seq_1': 'b', 'seq_0': 'a', 'seq_2': 'c'}
self.otu_lists_unmapped = [\
[['seq_0'], ['seq_1'], ['seq_2']],
[['seq_0'], ['seq_1'], ['seq_2']],
[['seq_0', 'seq_2'], ['seq_1']],
[['seq_0', 'seq_2', 'seq_1']],
]
self.otu_lists_mapped = [\
[['a'], ['b'], ['c']],
[['a'], ['b'], ['c']],
[['a', 'c'], ['b']],
[['a', 'c', 'b']],
]
self.temp_dir = tempfile.mkdtemp()
self.temp_dir_spaces = '/tmp/test_for_dotur/'
try:
mkdir(self.temp_dir_spaces)
except OSError:
pass
try:
#create sequence files
f = open(path.join(self.temp_dir, 'seqs1.sto'),'w')
f.write('')
f.close()
self.d_mat_file = path.join(self.temp_dir, 'dmat.txt')
d_mat = open(self.d_mat_file,'w')
d_mat.write(self.distance_matrix_string)
d_mat.close()
except OSError:
pass
def test_base_command(self):
"""Dotur BaseCommand should return the correct BaseCommand"""
c = Dotur()
self.assertEqual(c.BaseCommand,\
''.join(['cd "',getcwd(),'/"; ','dotur']))
c.Parameters['-l'].on()
self.assertEqual(c.BaseCommand,\
''.join(['cd "',getcwd(),'/"; ','dotur -l']))
def test_changing_working_dir(self):
"""Dotur BaseCommand should change according to WorkingDir"""
c = Dotur(WorkingDir='/tmp/dotur_test')
self.assertEqual(c.BaseCommand,\
''.join(['cd "','/tmp/dotur_test','/"; ','dotur']))
c = Dotur()
c.WorkingDir = '/tmp/dotur_test2'
self.assertEqual(c.BaseCommand,\
''.join(['cd "','/tmp/dotur_test2','/"; ','dotur']))
#removing the dirs is proof that they were created at the same time
#if the dirs are not there, an OSError will be raised
rmdir('/tmp/dotur_test')
rmdir('/tmp/dotur_test2')
def test_remap_seq_names(self):
"""remap_seq_names should function as expected."""
for unmapped, mapped in zip(self.otu_lists_unmapped,\
self.otu_lists_mapped):
self.assertEqual(remap_seq_names(unmapped,self.int_keys),mapped)
def test_dotur_from_alignment(self):
"""dotur_from_alignment should behave correctly."""
res = dotur_from_alignment(aln=self.seqs2_aligned,moltype=RNA,\
distance_function=rna_distance)
self.assertEqual(res,self.otu_res_list)
def test_dotur_from_file(self):
"""dotur_from_alignment should behave correctly."""
res = dotur_from_file(self.d_mat_file)
self.assertEqual(res,self.otu_res_list)
def test_general_cleanUp(self):
"""Last test executed: cleans up all files initially created"""
# remove the tempdir and contents
shutil.rmtree(self.temp_dir)
shutil.rmtree(self.temp_dir_spaces)
if __name__ == '__main__':
main()
|