~ubuntu-branches/ubuntu/trusty/qiime/trusty

« back to all changes in this revision

Viewing changes to tests/test_pycogent_backports/test_pplacer.py

  • Committer: Package Import Robot
  • Author(s): Andreas Tille
  • Date: 2013-06-17 18:28:26 UTC
  • mfrom: (9.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20130617182826-376az5ad080a0sfe
Tags: 1.7.0+dfsg-1
Upload preparations done for BioLinux to Debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/env python
2
 
 
3
 
__author__ = "Jesse Stombaugh"
4
 
__copyright__ = "Copyright 2007-2011, The Cogent Project"
5
 
__credits__ = ["Jesse Stombaugh"]
6
 
__license__ = "GPL"
7
 
__version__ = "1.6.0dev"
8
 
__maintainer__ = "Jesse Stombaugh"
9
 
__email__ = "jesse.stombaugh@colorado.edu"
10
 
__status__ = "Release"
11
 
 
12
 
from os import getcwd, remove, rmdir, mkdir
13
 
from os.path import splitext
14
 
from cogent.util.unit_test import TestCase, main
15
 
from cogent.util.misc import flatten
16
 
from random import randint
17
 
from qiime.pycogent_backports.pplacer import Pplacer, insert_sequences_into_tree
18
 
from cogent.app.util import ApplicationError,get_tmp_filename
19
 
from cogent.parse.fasta import MinimalFastaParser
20
 
from cogent.core.tree import PhyloNode
21
 
from cogent.core.moltype import RNA,DNA
22
 
from StringIO import StringIO
23
 
from cogent.core.alignment import Alignment
24
 
 
25
 
class Genericpplacer(TestCase):
26
 
 
27
 
    def setUp(self):
28
 
        '''setup the files for testing pplacer'''
29
 
        
30
 
        # create a list of files to cleanup
31
 
        self._paths_to_clean_up = []
32
 
        self._dirs_to_clean_up = []
33
 
        
34
 
        # get a tmp filename to use
35
 
        basename=splitext(get_tmp_filename())[0]
36
 
        
37
 
        # create and write out RAxML stats file
38
 
        self.stats_fname=basename+'.stats'
39
 
        stats_out=open(self.stats_fname,'w')
40
 
        stats_out.write(RAXML_STATS)
41
 
        stats_out.close()
42
 
        self._paths_to_clean_up.append(self.stats_fname)
43
 
        
44
 
        # create and write out reference sequence file
45
 
        self.refseq_fname=basename+'_refseqs.fasta'
46
 
        refseq_out=open(self.refseq_fname,'w')
47
 
        refseq_out.write(REF_SEQS)
48
 
        refseq_out.close()
49
 
        self._paths_to_clean_up.append(self.refseq_fname)
50
 
        
51
 
        # create and write out query sequence file
52
 
        self.query_fname=basename+'_queryseqs.fasta'
53
 
        query_out=open(self.query_fname,'w')
54
 
        query_out.write(QUERY_SEQS)
55
 
        query_out.close()
56
 
        self._paths_to_clean_up.append(self.query_fname)
57
 
        
58
 
        # create and write out starting tree file
59
 
        self.tree_fname=basename+'.tre'
60
 
        tree_out=open(self.tree_fname,'w')
61
 
        tree_out.write(REF_TREE)
62
 
        tree_out.close()
63
 
        self._paths_to_clean_up.append(self.tree_fname) 
64
 
 
65
 
    def writeTmp(self, outname):
66
 
        """Write data to temp file"""
67
 
        t = open(outname, "w+")
68
 
        t.write(PHYLIP_FILE)
69
 
        t.close()
70
 
 
71
 
    #
72
 
    def tearDown(self): 
73
 
        """cleans up all files initially created"""
74
 
        # remove the tempdir and contents
75
 
        map(remove,self._paths_to_clean_up)
76
 
        map(rmdir,self._dirs_to_clean_up)
77
 
 
78
 
class pplacerTests(Genericpplacer):
79
 
    """Tests for the pplacer application controller"""
80
 
    
81
 
    def test_pplacer(self):
82
 
        """Base command-calls"""
83
 
        
84
 
        app=Pplacer()
85
 
        
86
 
        self.assertEqual(app.BaseCommand, \
87
 
                         ''.join(['cd "',getcwd(),'/"; ','pplacer']))
88
 
        
89
 
        app.Parameters['--help'].on()
90
 
        self.assertEqual(app.BaseCommand, \
91
 
                         ''.join(['cd "',getcwd(),'/"; ','pplacer --help']))
92
 
    
93
 
    def test_change_working_dir(self):
94
 
        """Change working dir"""
95
 
        
96
 
        working_dir='/tmp/Pplacer'
97
 
        self._dirs_to_clean_up.append(working_dir)
98
 
        
99
 
        # define working directory for output
100
 
        app = Pplacer(WorkingDir=working_dir)
101
 
        
102
 
        self.assertEqual(app.BaseCommand, \
103
 
                       ''.join(['cd "','/tmp/Pplacer','/"; ','pplacer']))
104
 
 
105
 
 
106
 
    def test_insert_sequences_into_tree(self):
107
 
        """Inserts sequences into Tree"""
108
 
        
109
 
        params={}
110
 
        # generate temp filename for output
111
 
        params["-r"] = self.refseq_fname
112
 
        params["-t"] = self.tree_fname
113
 
        params["-s"] = self.stats_fname
114
 
        params["--out-dir"] = "/tmp"
115
 
        
116
 
        aln_ref_query=MinimalFastaParser(StringIO(QUERY_SEQS))
117
 
        aln = Alignment(aln_ref_query)
118
 
        seqs, align_map = aln.toPhylip()
119
 
        tree = insert_sequences_into_tree(seqs, DNA, params=params,
120
 
                                          write_log=False)
121
 
        
122
 
        # rename tips back to query names
123
 
        for node in tree.tips():
124
 
            if node.Name in align_map:
125
 
                node.Name = align_map[node.Name]
126
 
        
127
 
        self.assertEqual(tree.getNewick(with_distances=True), RESULT_TREE)
128
 
        
129
 
        
130
 
JSON_RESULT="""\
131
 
{"tree":
132
 
  "((seq0000004:0.08408[0],seq0000005:0.13713[1])0.609:0.00215[2],seq0000003:0.02032[3],(seq0000001:0.00014[4],seq0000002:0.00014[5])0.766:0.00015[6]):0[7];",
133
 
  "placements":
134
 
  [
135
 
    {"p":
136
 
      [[0, -113.210938, 0.713818, 0.064504, 0.000006],
137
 
        [1, -114.929894, 0.127954, 0.137122, 0.000007],
138
 
        [2, -114.932766, 0.127587, 0.000008, 0.000006],
139
 
        [6, -117.743534, 0.007675, 0.000141, 0.027211],
140
 
        [3, -117.743759, 0.007674, 0.020310, 0.027207],
141
 
        [4, -117.747386, 0.007646, 0.000131, 0.027266],
142
 
        [5, -117.747396, 0.007646, 0.000131, 0.027266]
143
 
      ], "n": ["seq0000006"]
144
 
    },
145
 
    {"p": [[0, -113.476305, 1.000000, 0.035395, 0.000006]], "n":
146
 
      ["seq0000007"]
147
 
    }
148
 
  ], "metadata":
149
 
  {"invocation":
150
 
    "pplacer -t %s -r %s -s %s --out-dir \/tmp %s"
151
 
  }, "version": 1, "fields":
152
 
  ["edge_num", "likelihood", "like_weight_ratio", "distal_length",
153
 
    "pendant_length"
154
 
  ]
155
 
}
156
 
""".replace('\n','').replace(' ','')
157
 
 
158
 
 
159
 
QUERY_SEQS= """\
160
 
>6
161
 
TGCATGTCAGTATAGCTTTGGTGAAACTGCGAATGGCTCATTAAATCAGT
162
 
>7
163
 
TGCATGTCAGTATAACTTTGGTGAAACTGCGAATGGCTCATTAAATCAGT
164
 
""" 
165
 
 
166
 
 
167
 
REF_SEQS= """\
168
 
>seq0000011
169
 
TGCATGTCAGTATAGCTTTAGTGAAACTGCGAATGGCTCATTAAATCAGT
170
 
>seq0000012
171
 
TGCATGTCAGTATAGCTTTAGTGAAACTGCGAATGGCTNNTTAAATCAGT
172
 
>seq0000013
173
 
TGCATGTCAGTATAGCATTAGTGAAACTGCGAATGGCTCATTAAATCAGT
174
 
>seq0000014
175
 
TCCATGTCAGTATAACTTTGGTGAAACTGCGAATGGCTCATTAAATCAGG
176
 
>seq0000015
177
 
NNNNNNNNNNTATATCTTATGTGAAACTTCGAATGCCTCATTAAATCAGT
178
 
"""
179
 
 
180
 
REF_TREE="""((seq0000014:0.08408,seq0000015:0.13713)0.609:0.00215,seq0000013:0.02032,(seq0000011:0.00014,seq0000012:0.00014)0.766:0.00015);
181
 
"""
182
 
 
183
 
RESULT_TREE="""((((seq0000014:0.0353946,7:6.11352e-06):0.0291093,6:6.11352e-06):0.019576,seq0000015:0.13713)0.609:0.00215,seq0000013:0.02032,(seq0000011:0.00014,seq0000012:0.00014)0.766:0.00015);"""
184
 
 
185
 
RAXML_STATS="""
186
 
 
187
 
 
188
 
This is RAxML version 7.2.6 released by Alexandros Stamatakis in February 2010.
189
 
 
190
 
With greatly appreciated code contributions by:
191
 
Andre Aberer (TUM)
192
 
Simon Berger (TUM)
193
 
John Cazes (TACC)
194
 
Michael Ott (TUM)
195
 
Nick Pattengale (UNM)
196
 
Wayne Pfeiffer (SDSC)
197
 
 
198
 
 
199
 
Alignment has 18 distinct alignment patterns
200
 
 
201
 
Proportion of gaps and completely undetermined characters in this alignment: 4.80%
202
 
 
203
 
RAxML rapid hill-climbing mode
204
 
 
205
 
Using 1 distinct models/data partitions with joint branch length optimization
206
 
 
207
 
 
208
 
Executing 1 inferences on the original alignment using 1 distinct randomized MP trees
209
 
 
210
 
All free model parameters will be estimated by RAxML
211
 
ML estimate of 25 per site rate categories
212
 
 
213
 
Likelihood of final tree will be evaluated and optimized under GAMMA
214
 
 
215
 
GAMMA Model parameters will be estimated up to an accuracy of 0.1000000000 Log Likelihood units
216
 
 
217
 
Partition: 0
218
 
Alignment Patterns: 18
219
 
Name: No Name Provided
220
 
DataType: DNA
221
 
Substitution Matrix: GTR
222
 
 
223
 
 
224
 
 
225
 
 
226
 
RAxML was called as follows:
227
 
 
228
 
raxmlHPC -m GTRCAT -s test_raxml.phy -n results 
229
 
 
230
 
 
231
 
Inference[0]: Time 0.072128 CAT-based likelihood -85.425107, best rearrangement setting 2
232
 
alpha[0]: 1.000000 rates[0] ac ag at cg ct gt: 0.000017 0.037400 0.859448 1.304301 0.000017 1.000000 
233
 
 
234
 
 
235
 
Conducting final model optimizations on all 1 trees under GAMMA-based models ....
236
 
 
237
 
Inference[0] final GAMMA-based Likelihood: -107.575676 tree written to file /home/RAxML_result.results
238
 
 
239
 
 
240
 
Starting final GAMMA-based thorough Optimization on tree 0 likelihood -107.575676 .... 
241
 
 
242
 
Final GAMMA-based Score of best tree -107.575676
243
 
 
244
 
Program execution info written to /home/RAxML_info.results
245
 
Best-scoring ML tree written to: /home/RAxML_bestTree.results
246
 
 
247
 
Overall execution time: 0.078965 secs or 0.000022 hours or 0.000001 days
248
 
"""
249
 
 
250
 
if __name__ == '__main__':
251
 
    main()