PageRenderTime 17ms CodeModel.GetById 9ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/taxonomy/t2ps_wrapper.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 67 lines | 36 code | 12 blank | 19 comment | 3 complexity | 85b98a59c3097d4255e86f8b8b90ba37 MD5 | raw file
 1"""
 2Wrapper for tree2PS-fast
 3Requires ps2pdf (a part of ghostscript package) to be installed
 4
 5t2ps_wrapper.py <taxonomy file> <output PDF file> <max_tree_level> <font_size> <max_leaves> <count_duplicate_tax_id>
 6
 7    taxonomy file    - tree file produced by taxonomy2tree program written by Sergei Kosakovski Pond
 8    output PDF file  - tree image
 9    max_tree_level   - integer from 0 to 21; 0 = show all levels
10    font_size        - integer from 2 to 255 (8 is the best)
11    max_leaves       - integer from 0 to infinity (0 = show all)
12    count_duplicate  - 0 (do not count) or 1 (count)
13    
14anton nekrutenko | anton@bx.psu.edu
15tree2PS-fast is written by Sergei Kosakovski Pond | sergeilkp@mac.com
16"""
17
18import string, sys, tempfile, subprocess
19
20def stop_err(msg):
21    sys.stderr.write(msg)
22    sys.exit()
23
24
25try:
26    tree_file = sys.argv[1]
27    pdf_file  = sys.argv[2]
28    max_tree_level = sys.argv[3]
29    font_size = sys.argv[4]
30    max_leaves = sys.argv[5]
31    dups = sys.argv[6]
32except:
33    stop_err('Check arguments\n')
34
35newick_file = tempfile.NamedTemporaryFile('w')    
36ps_file = tempfile.NamedTemporaryFile('w')
37
38# Execute taxonomy2tree
39    
40try:
41    t2t_cmd = 'taxonomy2tree %s %s %s /dev/null 1 > /dev/null 2>&1' % ( tree_file, max_tree_level, newick_file.name )
42    retcode = subprocess.call( t2t_cmd, shell=True )
43    if retcode < 0:
44        print >>sys.stderr, "Execution of taxonomy2tree terminated by signal", -retcode
45except OSError, e:
46    print >>sys.stderr, "Execution of taxonomy2tree failed:", e
47
48
49# Execute tree2PS-fast
50    
51try:
52    t2ps_cmd = 'tree2PS-fast %s %s %s %s %s %s' % ( newick_file.name, ps_file.name, max_tree_level, font_size, max_leaves, dups )
53    retcode = subprocess.call( t2ps_cmd, shell=True )
54    if retcode < 0:
55        print >>sys.stderr, "Execution of tree2PS-fast terminated by signal", -retcode
56except OSError, e:
57    print >>sys.stderr, "Execution of tree2PS-fast failed:", e
58    
59# Convert PS to PDF
60
61try:
62    ps2pdf_cmd = 'ps2pdf %s %s' % ( ps_file.name, pdf_file )
63    retcode = subprocess.call( ps2pdf_cmd, shell=True )
64    if retcode < 0:
65        print >>sys.stderr, "Execution of ps2pdf terminated by signal", -retcode
66except OSError, e:
67    print >>sys.stderr, "Execution of ps2pdf failed:", e