annotate tools/hyphy/hyphy_nj_tree_wrapper.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #Dan Blankenberg
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 #takes fasta alignments, a distance metric and builds neighbor joining trees
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 import os, sys
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 from galaxy.tools.util import hyphy_util
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 #Retrieve hyphy path, this will need to be the same across the cluster
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 tool_data = sys.argv.pop()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 HYPHY_PATH = os.path.join( tool_data, "HYPHY" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 HYPHY_EXECUTABLE = os.path.join( HYPHY_PATH, "HYPHY" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 #Read command line arguments
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 input_filename = os.path.abspath(sys.argv[1].strip())
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 output_filename1 = os.path.abspath(sys.argv[2].strip())
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 output_filename2 = os.path.abspath(sys.argv[3].strip())
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 distance_metric = sys.argv[4].strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 temp_ps_filename = hyphy_util.get_filled_temp_filename("")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 #Guess if this is a single or multiple FASTA input file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 found_blank = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 is_multiple = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 for line in open(input_filename):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 line = line.strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 if line == "": found_blank = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 elif line.startswith(">") and found_blank:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 is_multiple = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 else: found_blank = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 NJ_tree_shared_ibf = hyphy_util.get_filled_temp_filename(hyphy_util.NJ_tree_shared_ibf)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 #set up NJ_tree file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 NJ_tree_filename = hyphy_util.get_filled_temp_filename(hyphy_util.get_NJ_tree(NJ_tree_shared_ibf))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 #setup Config file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 config_filename = hyphy_util.get_nj_tree_config_filename(input_filename, distance_metric, output_filename1, temp_ps_filename, NJ_tree_filename)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 if is_multiple:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 os.unlink(NJ_tree_filename)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 os.unlink(config_filename)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 NJ_tree_filename = hyphy_util.get_filled_temp_filename(hyphy_util.get_NJ_treeMF(NJ_tree_shared_ibf))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 config_filename = hyphy_util.get_nj_treeMF_config_filename(input_filename, output_filename1, temp_ps_filename, distance_metric, NJ_tree_filename)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 print "Multiple Alignment Analyses"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 else: print "Single Alignment Analyses"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 #Run Hyphy
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 hyphy_cmd = "%s BASEPATH=%s USEPATH=/dev/null %s" % (HYPHY_EXECUTABLE, HYPHY_PATH, config_filename)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 hyphy = os.popen(hyphy_cmd, 'r')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 #print hyphy.read()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 hyphy.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 #remove temporary files
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 os.unlink(NJ_tree_filename)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 os.unlink(config_filename)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 #Convert PS to PDF
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 if os.path.getsize(temp_ps_filename)>0: temp = os.popen("ps2pdf %s %s" % (temp_ps_filename, output_filename2), 'r').close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 os.unlink(temp_ps_filename)