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
|
#!/usr/bin/env python
# File created on 24 Feb 2012
from __future__ import division
__author__ = "Greg Caporaso"
__copyright__ = "Copyright 2011, The QIIME project"
__credits__ = ["Greg Caporaso"]
__license__ = "GPL"
__version__ = "1.8.0"
__maintainer__ = "Greg Caporaso"
__email__ = "gregcaporaso@gmail.com"
from cogent.util.unit_test import TestCase, main
from cogent import LoadSeqs
from qiime.split import (split_mapping_file_on_field,
split_otu_table_on_sample_metadata,
split_fasta)
from qiime.util import (get_qiime_temp_dir,
remove_files,
get_tmp_filename)
from qiime.format import format_biom_table
from biom.parse import parse_biom_table
from biom.table import DenseOTUTable
class SplitTests(TestCase):
""" Tests of the split module """
def setUp(self):
""" """
self.mapping_f1 = mapping_f1.split('\n')
self.mapping_f2 = mapping_f2.split('\n')
self.mapping_exp = list(mapping_exp)
self.otu_table_f1 = otu_table_f1.split('\n')
def test_split_mapping_file_on_field(self):
""" split_mapping_file_on_field functions as expected with valid input
"""
actual = list(split_mapping_file_on_field(self.mapping_f1,'Treatment'))
actual.sort()
self.mapping_exp.sort()
self.assertEqual(actual,self.mapping_exp)
def test_split_otu_table_on_sample_metadata(self):
""" split_otu_table_on_sample_metadata functions as expected with valid input """
actual = list(split_otu_table_on_sample_metadata(self.otu_table_f1,
self.mapping_f1,
"Treatment"))
for id_, e in actual:
try:
parse_biom_table(e)
except:
print e
actual = [(id_,parse_biom_table(e)) for id_, e in actual]
exp = [(id_,parse_biom_table(e)) for id_, e in otu_table_exp1]
actual.sort()
exp.sort()
for a,e in zip(actual,exp):
self.assertEqual(a,e,"OTU tables are not equal:\n%s\n%s" % \
(format_biom_table(a[1]),format_biom_table(e[1])))
def test_split_otu_table_on_sample_metadata_extra_mapping_entries(self):
""" split_otu_table_on_sample_metadata functions as expected with extra mapping data """
actual = list(split_otu_table_on_sample_metadata(self.otu_table_f1,
self.mapping_f2,
"Treatment"))
actual = [(id_,parse_biom_table(e)) for id_, e in actual]
exp = [(id_,parse_biom_table(e)) for id_, e in otu_table_exp1]
actual.sort()
exp.sort()
for a,e in zip(actual,exp):
self.assertEqual(a,e,"OTU tables are not equal:\n%s\n%s" % \
(format_biom_table(a[1]),format_biom_table(e[1])))
def test_split_fasta_equal_num_seqs_per_file(self):
"""split_fasta funcs as expected when equal num seqs go to each file
"""
filename_prefix = get_tmp_filename(tmp_dir=get_qiime_temp_dir(),
prefix='split_fasta_tests',
suffix='',
result_constructor=str)
infile = ['>seq1','AACCTTAA','>seq2','TTAACC','AATTAA',\
'>seq3','CCTT--AA']
actual = split_fasta(infile, 1, filename_prefix)
actual_seqs = []
for fp in actual:
actual_seqs += list(open(fp))
remove_files(actual)
expected = ['%s.%d.fasta' % (filename_prefix,i) for i in range(3)]
self.assertEqual(actual,expected)
self.assertEqual(\
LoadSeqs(data=infile,aligned=False),\
LoadSeqs(data=actual_seqs,aligned=False))
def test_split_fasta_diff_num_seqs_per_file(self):
"""split_fasta funcs as expected when diff num seqs go to each file
"""
filename_prefix = get_tmp_filename(tmp_dir=get_qiime_temp_dir(),
prefix='split_fasta_tests',
suffix='',
result_constructor=str)
infile = ['>seq1','AACCTTAA','>seq2','TTAACC','AATTAA',\
'>seq3','CCTT--AA']
actual = split_fasta(infile, 2, filename_prefix)
actual_seqs = []
for fp in actual:
actual_seqs += list(open(fp))
remove_files(actual)
expected = ['%s.%d.fasta' % (filename_prefix,i) for i in range(2)]
# list of file paths is as expected
self.assertEqual(actual,expected)
# building seq collections from infile and the split files result in
# equivalent seq collections
self.assertEqual(\
LoadSeqs(data=infile,aligned=False),\
LoadSeqs(data=actual_seqs,aligned=False))
def test_split_fasta_diff_num_seqs_per_file_alt(self):
"""split_fasta funcs always catches all seqs
"""
# start with 59 seqs (b/c it's prime, so should make more
# confusing splits)
in_seqs = LoadSeqs(data=[('seq%s' % k,'AACCTTAA') for k in range(59)])
infile = in_seqs.toFasta().split('\n')
# test seqs_per_file from 1 to 1000
for i in range(1,1000):
filename_prefix = get_tmp_filename(tmp_dir=get_qiime_temp_dir(),
prefix='split_fasta_tests',
suffix='',
result_constructor=str)
actual = split_fasta(infile, i, filename_prefix)
actual_seqs = []
for fp in actual:
actual_seqs += list(open(fp))
# remove the files now, so if the test fails they still get
# cleaned up
remove_files(actual)
# building seq collections from infile and the split files result in
# equivalent seq collections
self.assertEqual(\
LoadSeqs(data=infile,aligned=False),\
LoadSeqs(data=actual_seqs,aligned=False))
mapping_f1 = """#SampleID BarcodeSequence LinkerPrimerSequence Treatment DOB Description
#Example mapping file for the QIIME analysis package. These 9 samples are from a study of the effects of exercise and diet on mouse cardiac physiology (Crawford, et al, PNAS, 2009).
PC.354 AGCACGAGCCTA YATGCTGCCTCCCGTAGGAGT Co_ntrol 20061218 Control_mouse_I.D._354
PC.355 AACTCGTCGATG YATGCTGCCTCCCGTAGGAGT Control 20061218 Control_mouse_I.D._355
PC.356 ACAGACCACTCA YATGCTGCCTCCCGTAGGAGT Co_ntrol 20061126 Control_mouse_I.D._356
PC.481 ACCAGCGACTAG YATGCTGCCTCCCGTAGGAGT Control 20070314 Control_mouse_I.D._481
PC.593 AGCAGCACTTGT YATGCTGCCTCCCGTAGGAGT Control 20071210 Control_mouse_I.D._593
PC.607 AACTGTGCGTAC YATGCTGCCTCCCGTAGGAGT Fast 20071112 Fasting_mouse_I.D._607
PC.634 ACAGAGTCGGCT YATGCTGCCTCCCGTAGGAGT Fast 20080116 Fasting_mouse_I.D._634
PC.635 ACCGCAGAGTCA YATGCTGCCTCCCGTAGGAGT Fast 20080116 Fasting_mouse_I.D._635
PC.636 ACGGTGAGTGTC YATGCTGCCTCCCGTAGGAGT Fast 20080116 Fasting_mouse_I.D._636
"""
mapping_f2 = """#SampleID BarcodeSequence LinkerPrimerSequence Treatment DOB Description
#Example mapping file for the QIIME analysis package. These 9 samples are from a study of the effects of exercise and diet on mouse cardiac physiology (Crawford, et al, PNAS, 2009).
PC.354 AGCACGAGCCTA YATGCTGCCTCCCGTAGGAGT Co_ntrol 20061218 Control_mouse_I.D._354
PC.355 AACTCGTCGATG YATGCTGCCTCCCGTAGGAGT Control 20061218 Control_mouse_I.D._355
PC.356 ACAGACCACTCA YATGCTGCCTCCCGTAGGAGT Co_ntrol 20061126 Control_mouse_I.D._356
PC.481 ACCAGCGACTAG YATGCTGCCTCCCGTAGGAGT Control 20070314 Control_mouse_I.D._481
PC.593 AGCAGCACTTGT YATGCTGCCTCCCGTAGGAGT Control 20071210 Control_mouse_I.D._593
PC.607 AACTGTGCGTAC YATGCTGCCTCCCGTAGGAGT Fast 20071112 Fasting_mouse_I.D._607
PC.634 ACAGAGTCGGCT YATGCTGCCTCCCGTAGGAGT Fast 20080116 Fasting_mouse_I.D._634
PC.635 ACCGCAGAGTCA YATGCTGCCTCCCGTAGGAGT Fast 20080116 Fasting_mouse_I.D._635
PC.636 ACGGTGAGTGTC YATGCTGCCTCCCGTAGGAGT Fast 20080116 Fasting_mouse_I.D._636
Fake.sample ACGGTGAGTGTC YATGCTGCCTCCCGTAGGAGT Other 20080116 Fasting_mouse_I.D._636
"""
mapping_exp = [("Co_ntrol", """#SampleID BarcodeSequence LinkerPrimerSequence Treatment DOB Description
PC.354 AGCACGAGCCTA YATGCTGCCTCCCGTAGGAGT Co_ntrol 20061218 Control_mouse_I.D._354
PC.356 ACAGACCACTCA YATGCTGCCTCCCGTAGGAGT Co_ntrol 20061126 Control_mouse_I.D._356"""),
("Control","""#SampleID BarcodeSequence LinkerPrimerSequence Treatment DOB Description
PC.355 AACTCGTCGATG YATGCTGCCTCCCGTAGGAGT Control 20061218 Control_mouse_I.D._355
PC.481 ACCAGCGACTAG YATGCTGCCTCCCGTAGGAGT Control 20070314 Control_mouse_I.D._481
PC.593 AGCAGCACTTGT YATGCTGCCTCCCGTAGGAGT Control 20071210 Control_mouse_I.D._593"""),
("Fast","""#SampleID BarcodeSequence LinkerPrimerSequence Treatment DOB Description
PC.607 AACTGTGCGTAC YATGCTGCCTCCCGTAGGAGT Fast 20071112 Fasting_mouse_I.D._607
PC.634 ACAGAGTCGGCT YATGCTGCCTCCCGTAGGAGT Fast 20080116 Fasting_mouse_I.D._634
PC.635 ACCGCAGAGTCA YATGCTGCCTCCCGTAGGAGT Fast 20080116 Fasting_mouse_I.D._635
PC.636 ACGGTGAGTGTC YATGCTGCCTCCCGTAGGAGT Fast 20080116 Fasting_mouse_I.D._636""")]
otu_table_f1 = """{
"id":null,
"format": "Biological Observation Matrix v0.9",
"format_url": "http://www.qiime.org/svn_documentation/documentation/biom_format.html",
"type": "OTU table",
"generated_by": "QIIME",
"date": "2011-12-19T19:00:00",
"rows":[
{"id":"GG_OTU_1", "metadata":null},
{"id":"GG_OTU_2", "metadata":null},
{"id":"GG_OTU_3", "metadata":null},
{"id":"GG_OTU_4", "metadata":null},
{"id":"GG_OTU_5", "metadata":null}
],
"columns": [
{"id":"PC.354", "metadata":null},
{"id":"PC.355", "metadata":null},
{"id":"PC.481", "metadata":null},
{"id":"PC.607", "metadata":null},
{"id":"PC.635", "metadata":null},
{"id":"PC.356", "metadata":null},
{"id":"PC.636", "metadata":null}
],
"matrix_type": "sparse",
"matrix_element_type": "int",
"shape": [5, 7],
"data":[[0,2,1],
[1,0,5],
[1,1,1],
[1,3,2],
[1,4,3],
[1,5,1],
[2,2,1],
[2,3,4],
[2,4,2],
[3,0,2],
[3,1,1],
[3,2,1],
[3,5,1],
[4,1,1],
[4,2,1],
[4,6,42]
]
}"""
otu_table_exp1 = [("Co_ntrol","""{
"id":null,
"format": "Biological Observation Matrix v0.9",
"format_url": "http://www.qiime.org/svn_documentation/documentation/biom_format.html",
"type": "OTU table",
"generated_by": "QIIME",
"date": "2011-12-19T19:00:00",
"rows":[
{"id":"GG_OTU_1", "metadata":null},
{"id":"GG_OTU_2", "metadata":null},
{"id":"GG_OTU_3", "metadata":null},
{"id":"GG_OTU_4", "metadata":null},
{"id":"GG_OTU_5", "metadata":null}
],
"columns": [
{"id":"PC.354", "metadata":null},
{"id":"PC.356", "metadata":null}
],
"matrix_type": "sparse",
"matrix_element_type": "float",
"shape": [5, 2],
"data":[
[1,0,5.0],
[1,1,1.0],
[3,0,2.0],
[3,1,1.0]
]
}"""),
("Control","""{
"id":null,
"format": "Biological Observation Matrix v0.9",
"format_url": "http://www.qiime.org/svn_documentation/documentation/biom_format.html",
"type": "OTU table",
"generated_by": "QIIME",
"date": "2011-12-19T19:00:00",
"rows":[
{"id":"GG_OTU_1", "metadata":null},
{"id":"GG_OTU_2", "metadata":null},
{"id":"GG_OTU_3", "metadata":null},
{"id":"GG_OTU_4", "metadata":null},
{"id":"GG_OTU_5", "metadata":null}
],
"columns": [
{"id":"PC.355", "metadata":null},
{"id":"PC.481", "metadata":null}
],
"matrix_type": "sparse",
"matrix_element_type": "float",
"shape": [5, 2],
"data":[[0,1,1.0],
[1,0,1.0],
[2,1,1.0],
[3,0,1.0],
[3,1,1.0],
[4,0,1.0],
[4,1,1.0]
]
}
"""),
("Fast","""{
"id":null,
"format": "Biological Observation Matrix v0.9",
"format_url": "http://www.qiime.org/svn_documentation/documentation/biom_format.html",
"type": "OTU table",
"generated_by": "QIIME",
"date": "2011-12-19T19:00:00",
"rows":[
{"id":"GG_OTU_1", "metadata":null},
{"id":"GG_OTU_2", "metadata":null},
{"id":"GG_OTU_3", "metadata":null},
{"id":"GG_OTU_4", "metadata":null},
{"id":"GG_OTU_5", "metadata":null}
],
"columns": [
{"id":"PC.607", "metadata":null},
{"id":"PC.635", "metadata":null},
{"id":"PC.636", "metadata":null}
],
"matrix_type": "sparse",
"matrix_element_type": "float",
"shape": [5, 3],
"data":[[1,0,2.0],
[1,1,3.0],
[2,0,4.0],
[2,1,2.0],
[4,2,42]
]
}""")]
if __name__ == "__main__":
main()
|