/tools/hyphy/hyphy_nj_tree_wrapper.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 58 lines · 37 code · 10 blank · 11 comment · 8 complexity · 2737a87a705fd8b543c7ee7bec0aff40 MD5 · raw file

  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. #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_filename1 = os.path.abspath(sys.argv[2].strip())
  13. output_filename2 = os.path.abspath(sys.argv[3].strip())
  14. distance_metric = sys.argv[4].strip()
  15. temp_ps_filename = hyphy_util.get_filled_temp_filename("")
  16. #Guess if this is a single or multiple FASTA input file
  17. found_blank = False
  18. is_multiple = False
  19. for line in open(input_filename):
  20. line = line.strip()
  21. if line == "": found_blank = True
  22. elif line.startswith(">") and found_blank:
  23. is_multiple = True
  24. break
  25. else: found_blank = False
  26. NJ_tree_shared_ibf = hyphy_util.get_filled_temp_filename(hyphy_util.NJ_tree_shared_ibf)
  27. #set up NJ_tree file
  28. NJ_tree_filename = hyphy_util.get_filled_temp_filename(hyphy_util.get_NJ_tree(NJ_tree_shared_ibf))
  29. #setup Config file
  30. config_filename = hyphy_util.get_nj_tree_config_filename(input_filename, distance_metric, output_filename1, temp_ps_filename, NJ_tree_filename)
  31. if is_multiple:
  32. os.unlink(NJ_tree_filename)
  33. os.unlink(config_filename)
  34. NJ_tree_filename = hyphy_util.get_filled_temp_filename(hyphy_util.get_NJ_treeMF(NJ_tree_shared_ibf))
  35. config_filename = hyphy_util.get_nj_treeMF_config_filename(input_filename, output_filename1, temp_ps_filename, distance_metric, NJ_tree_filename)
  36. print "Multiple Alignment Analyses"
  37. else: print "Single Alignment Analyses"
  38. #Run Hyphy
  39. hyphy_cmd = "%s BASEPATH=%s USEPATH=/dev/null %s" % (HYPHY_EXECUTABLE, HYPHY_PATH, config_filename)
  40. hyphy = os.popen(hyphy_cmd, 'r')
  41. #print hyphy.read()
  42. hyphy.close()
  43. #remove temporary files
  44. os.unlink(NJ_tree_filename)
  45. os.unlink(config_filename)
  46. #Convert PS to PDF
  47. if os.path.getsize(temp_ps_filename)>0: temp = os.popen("ps2pdf %s %s" % (temp_ps_filename, output_filename2), 'r').close()
  48. os.unlink(temp_ps_filename)