/tools/hyphy/hyphy_branch_lengths_wrapper.py

https://bitbucket.org/ialbert/galaxy-genetrack · Python · 55 lines · 35 code · 8 blank · 12 comment · 7 complexity · 62b893166a9295fc611b06a554e51663 MD5 · raw file

  1. #Dan Blankenberg
  2. #takes commandline tree def and input multiple fasta alignment file and runs the branch length ananlysis
  3. import os, sys
  4. from galaxy import eggs
  5. from galaxy.tools.util import hyphy_util
  6. #Retrieve hyphy path, this will need to be the same across the cluster
  7. tool_data = sys.argv.pop()
  8. HYPHY_PATH = os.path.join( tool_data, "HYPHY" )
  9. HYPHY_EXECUTABLE = os.path.join( HYPHY_PATH, "HYPHY" )
  10. #Read command line arguments
  11. input_filename = os.path.abspath(sys.argv[1].strip())
  12. output_filename = os.path.abspath(sys.argv[2].strip())
  13. tree_contents = sys.argv[3].strip()
  14. nuc_model = sys.argv[4].strip()
  15. base_freq = sys.argv[5].strip()
  16. model_options = sys.argv[6].strip()
  17. #Set up Temporary files for hyphy run
  18. #set up tree file
  19. tree_filename = hyphy_util.get_filled_temp_filename(tree_contents)
  20. #Guess if this is a single or multiple FASTA input file
  21. found_blank = False
  22. is_multiple = False
  23. for line in open(input_filename):
  24. line = line.strip()
  25. if line == "": found_blank = True
  26. elif line.startswith(">") and found_blank:
  27. is_multiple = True
  28. break
  29. else: found_blank = False
  30. #set up BranchLengths file
  31. BranchLengths_filename = hyphy_util.get_filled_temp_filename(hyphy_util.BranchLengths)
  32. if is_multiple:
  33. os.unlink(BranchLengths_filename)
  34. BranchLengths_filename = hyphy_util.get_filled_temp_filename(hyphy_util.BranchLengthsMF)
  35. print "Multiple Alignment Analyses"
  36. else: print "Single Alignment Analyses"
  37. #setup Config file
  38. config_filename = hyphy_util.get_branch_lengths_config_filename(input_filename, nuc_model, model_options, base_freq, tree_filename, output_filename, BranchLengths_filename)
  39. #Run Hyphy
  40. hyphy_cmd = "%s BASEPATH=%s USEPATH=/dev/null %s" % (HYPHY_EXECUTABLE, HYPHY_PATH, config_filename)
  41. hyphy = os.popen(hyphy_cmd, 'r')
  42. #print hyphy.read()
  43. hyphy.close()
  44. #remove temporary files
  45. os.unlink(BranchLengths_filename)
  46. os.unlink(tree_filename)
  47. os.unlink(config_filename)