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