File: make_tep.py

package info (click to toggle)
qiime 1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 29,704 kB
  • sloc: python: 77,837; haskell: 379; sh: 113; makefile: 103
file content (221 lines) | stat: -rwxr-xr-x 7,567 bytes parent folder | download
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
#!/usr/bin/env python
# File created on 15 Jun 2011
from __future__ import division

__author__ = "Meg Pirrung"
__copyright__ = "Copyright 2011, The QIIME project"
__credits__ = ["Meg Pirrung"]
__license__ = "GPL"
__version__ = "1.4.0"
__maintainer__ = "Meg Pirrung"
__email__ = "meganap@gmail.com"
__status__ = "Release"

from os.path import split,splitext
from qiime.util import parse_command_line_parameters, make_option
from qiime.util import load_qiime_config, create_dir
from qiime.parse import parse_otu_table
import os

script_info = {}
script_info['brief_description'] = "Makes TopiaryExplorer project file"
script_info['script_description'] = "This script makes a TopiaryExplorer project file (.tep) and a jnlp file with the data location preloaded.\n\nWARNING: The jnlp file relies on an absolute path, if you move the .tep file, the generated jnlp will no longer work. However, you can still open the .tep file from your normal TopiaryExplorer install."
script_info['script_usage'] = []
script_info['script_usage'].append(("Example","Create .tep file and .jnlp file:","make_tep.py -i otu_table.txt -m mapping_file.txt -t otus.tre -o my_data"))
# script_info['script_usage'].append(("""""","Create .tep file and .jnlp file, then launch TopiaryExplorer:","make_tep.py -i otu_table.txt -m mapping_file.txt -t otus.tre -o my_data -l"))
script_info['output_description']= "The result of this script is written to a .tep file and a .jnlp file, both with the name supplied by -o"
script_info['required_options'] = [\
 # Example required option
 make_option('-i','--otu_file',type="string",help='Path to read otu table',dest='otu_table_fp'),\
 make_option('-m','--mapping_file',type="string",help='Path to read mapping file',dest='mapping_fp'),\
 make_option('-t','--tree_file',type="string",help='Path to read tree',dest='tree_fp')
]
script_info['optional_options'] = [\
 # Example optional option
 make_option('-o','--output_dir',type="new_dirpath",help='the output directory [default: %default]', dest='out_fp'),\
 make_option('-p','--prefs_file_dir',type="string",help='Path to prefs file', dest='prefs_file_fp'),\

 # make_option('--create_jnlp',action='store_true',
 #  help='create a jnlp file [default: %default]'),\
  make_option('-w','--web',action='store_true',default=False, help='web codebase jnlp flag [default: %default]', dest='web_flag'),
  make_option('-u','--url_path',type="string",help='url path', dest='url')
 # make_option('-l','--launch_TE',action='store_false', default="false",help='Option to launch TopiaryExplorer [default: %default]', dest='launch')
]
script_info['version'] = __version__

jnlp_top_block = """<?xml version="1.0" encoding="utf-8"?>

<jnlp codebase=\""""

jnlp_middle_block = """\">
    
    <information>
    <title>TopiaryExplorer</title>
    <vendor>University of Colorado</vendor>
    <description>TopiaryExplorer</description>
    
    <offline-allowed/>
     

  </information>
  
  <security>
      <all-permissions/>
  </security>
  
  <resources>
    <j2se version="1.6+" initial-heap-size="500M" max-heap-size="2000m" />
    
    <jar href="topiaryexplorer0.9.4.jar" />
    <jar href="lib/core.jar" />
    <jar href="lib/itext.jar" />
    <jar href="lib/pdf.jar" />
    <jar href="lib/ojdbc14.jar" />
    <jar href="lib/opengl.jar" />
    <jar href="lib/mysql-connector-java-5.1.10-bin.jar" />
    <jar href="lib/javaws.jar" />
    <jar href="lib/classes12.jar" />
    <jar href="lib/jogl.jar" />
    <jar href="lib/guava-r09.jar" />

  </resources>
  
  <application-desc main-class="topiaryexplorer.TopiaryExplorer">
  <argument>"""

jnlp_bottom_block = """</argument>
    </application-desc>
</jnlp>
"""

data_color_hsv = {
#'black1':  (0,0,20),
'red1': (0,100,100),
'blue1':    (240,100,100),
'orange1':  (28,98,95),
'green1':   (120,100,50.2),
'purple1':  (302,73,57),
'yellow1':  (60,100,100),
'cyan1':    (184, 49, 96),
'pink1':    (333,37,96),
'teal1':    (178,42,63),
'brown1':   (36,89,42),
'gray1':    (0,0,50.2),
'lime': (123,99,96),
'red2': (14,51,97),
'blue2':    (211,42,85),
'orange2':  (32,46,99),
'green2':   (142,36,79),
'purple2':  (269,29,75),
'yellow2':  (56,40,100),
#'black2':  (303,100,24),
'gray2':    (0, 0, 75.3),
#'teal2':   (192,100,24),
'red3': (325,100,93),
'blue3':    (197,100,100),
#'purple3': (271,43,36),
'brown2':   (33,45,77),
'green3':   (60,100,50.2),
'purple4':  (264,75,100),
#'yellow3': (60,66,75),
#'blue4':   (213,45,77),
'red4': (348,31,74),
'teal3':    (180,100,50.2),
#'brown3':  (60,100,28),
'red5': (0,100,50.2),
'green4':   (81,100,26),
#'purple5': (240,100,41),
'orange3':  (26,100,65)
#'brown4':  (25,100,20),
#'red6':    (17,100,63),
#'purple6':(272,100,44)
}

def make_te_prefs(prefs_dict):
    sample_coloring = prefs_dict['sample_coloring']
    lines = []
    for k in sample_coloring:
        for t in sample_coloring[k]['colors']:
            if(type(t) == tuple):
                lines.append(''.join([str(i)+',' for i in t[1]])+'\n')
            if(type(t) == str):
                lines.append(t+':'+''.join([str(i)+',' for i in data_color_hsv[sample_coloring[k]['colors'][t]]])+'\n')
        lines.append('>default'+k+':'+k+'\n')
    # print lines
    return lines

def main():
    option_parser, opts, args =\
       parse_command_line_parameters(**script_info)
    
    otu_table_fp = opts.otu_table_fp
    mapping_fp = opts.mapping_fp
    tree_fp = opts.tree_fp
    output_dir = opts.out_fp
    output_basename = splitext(split(otu_table_fp)[1])[0]
    
    if not output_dir:
        output_dir = 'make_tep_output/'
    
    create_dir(output_dir)
    
    tep_fp = '%s/%s.tep' % (output_dir,output_basename)      # opts.out_fp+'.tep'
    jnlp_fp = '%s/%s.jnlp' % (output_dir,output_basename)
    tepfile = open(tep_fp, 'w')
    otu_lines = open(otu_table_fp, 'U').readlines()
    sample_ids, otu_ids, otu_table, metadata = parse_otu_table(otu_lines)
    mapping_lines = open(mapping_fp, 'U')    
    tree_lines = open(tree_fp, 'U')
    
    lines = ['>>tre\n']
    lines += tree_lines.readlines() 
    lines += '\n'
    if(metadata):
        lines += '>>otm\n#OTU ID\tOTU Metadata\n'
        for i in range(len(otu_ids)):
            lines += otu_ids[i] + '\t'
            for m in metadata[i]:
                lines += m + ';'
            # lines = lines[:len(lines)-1]
            lines += '\n'
    lines += '>>osm\n'
    lines += otu_lines
    lines += '\n>>sam\n'
    lines += mapping_lines.readlines()
    
    if opts.prefs_file_fp:
        prefs_fp = opts.prefs_file_fp
        prefs_dict = eval(open(prefs_fp,'U').read())
        te_prefs = make_te_prefs(prefs_dict)
        lines += '\n>>pre\n'
        lines += te_prefs
    
    tepfile.writelines(lines)

    # if opts.create_jnlp:
    jnlpfile = open(jnlp_fp, 'w')
    lines = [jnlp_top_block]
    if(opts.web_flag):
        lines += 'http://topiaryexplorer.sourceforge.net/app/'
    else:
        topiaryexplorer_project_dir =\
         load_qiime_config()['topiaryexplorer_project_dir']
        if topiaryexplorer_project_dir == None:
            option_parser.error("Couldn't create jnlp file - topiaryexplorer_project_dir is not defined in your qiime_config. tep file was created sucessfully.")
            exit(0)
        else:
            lines += 'file:' + topiaryexplorer_project_dir
    
    lines += jnlp_middle_block
    if(opts.url):
        lines += opts.url
    else:
        lines += os.path.abspath(tep_fp)
    # lines += os.path.abspath(tep_fp)
    lines += jnlp_bottom_block
    jnlpfile.writelines(lines)
    
    
    
if __name__ == "__main__":
    main()