Advertisement
Guest User

Untitled

a guest
Sep 20th, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.77 KB | None | 0 0
  1. #!/usr/bin/python
  2. """
  3. This script pulls in homologs of proteins from PDB
  4. as determined by HHSuite. It then employs UCSF Chimera to
  5. structurally match them and get an indication of how well
  6. they score (RMSD) in order to pick the best simulation.
  7. """
  8.  
  9. from __future__ import print_function
  10. import os
  11. import subprocess
  12. import sys
  13. import argparse
  14. import traceback
  15. import warnings
  16. import glob
  17. import fnmatch
  18.  
  19. import pychimera
  20.  
  21. import chimera
  22. from chimera import openModels,\
  23.                 Molecule,\
  24.                 replyobj
  25. from chimera import runCommand as rc
  26. from MatchMaker import\
  27.          match,\
  28.          CP_SPECIFIC_SPECIFIC,\
  29.          GAP_OPEN,\
  30.          GAP_EXTEND,\
  31.          defaults,\
  32.          MATRIX,\
  33.          ITER_CUTOFF
  34.  
  35.  
  36. # If running using python interpreter and not pychimera:
  37. # os.environ['CHIMERADIR'] = '/home/wms_joe/Applications/CHIMERA1.11'
  38. # CHIMERADIR should point to the application root directory.
  39. # This can be found with:   `chimera --root`
  40.  
  41.  
  42. # Only needed if running via normal python interpreter, not pychimera
  43. # pychimera.patch_environ()
  44. # pychimera.enable_chimera()
  45.  
  46.  
  47. ##### Main code begins #####
  48.  
  49.  
  50.  
  51. # Step 1, parse the output of HHpred to get the nearest homolog.
  52.  
  53. def main():
  54.     def_cpus=12
  55.     try:
  56.         parser = argparse.ArgumentParser(description='This script compares protein structural homologs as determined with HHpred, to PDB models using UCSF CHIMERA, to gather metrics of structural similarity.')
  57.  
  58.         parser.add_argument(
  59.             '-d',\
  60.             '--database',\
  61.             action='store',
  62.             default=None,\
  63.             help='You can specify a different HHpred database (filepath) to use if you offer this parameter. Otherwise it defaults to PDB.')
  64.  
  65.         parser.add_argument(
  66.             '-f',\
  67.             '--fasta',\
  68.             action='store',\
  69.             help='The fasta amino acid sequence that corresponds to the simulated structure and the sequence you wish to query.')
  70.  
  71.         parser.add_argument(
  72.             '-s',\
  73.             '--simulations',\
  74.             action='store',\
  75.             help='The directory where the protein structure simulations are stored. They must be in PDB format and be named logically.')
  76.  
  77.         parser.add_argument(
  78.             '-r',\
  79.             '--rmsd',\
  80.             action='store',\
  81.             help='The file to store the RMSD values for each matching.')
  82.  
  83.         parser.add_argument(
  84.             '-t',\
  85.             '--threads',\
  86.             action='store',\
  87.             default=def_cpus,\
  88.             help='The number of threads that HHsearch can execute on.')
  89.  
  90.  
  91.         args = parser.parse_args()
  92.  
  93.     except:
  94.         print("An exception occured with argument parsing. Check your provided options.")
  95.         traceback.print_exc()
  96.  
  97.   # Acquire HHPred Database
  98.     if args.database is None:
  99.         db_cmd = 'find ~/Applications/HHSuite/databases/pdb70 -type f -name "pdb70_hhm.ffdata"'
  100.             db_path = subprocess.Popen(
  101.                     db_cmd,\
  102.                     shell=True,\
  103.                     stdin=subprocess.PIPE,\
  104.                     stdout=subprocess.PIPE,\
  105.                     stderr=subprocess.PIPE)
  106.  
  107.         (stdout,stderr) = db_path.communicate()
  108.             filelist = stdout.decode().split()
  109.         args.database = stdout
  110.     else:
  111.         print("No default database found and you haven't provided one directly.")
  112.  
  113.     if args.fasta is not None:
  114.         split = os.path.splitext(args.fasta)
  115.         basename = os.path.basename(split[0])
  116.     else:
  117.         print('No input fasta was provided. Check your input parameters')
  118.         sys.exit(1)
  119.  
  120.     # Run the HHsearch
  121.     if args.fasta is not None and args.database is not None:
  122.         print("Database in use is:")
  123.         print("Running " + args.fasta + " in HHsearch, against " + args.database + " on " + args.threads + " CPUs.")
  124.         hhresult_file = '{0}.hhr.fasta'.format(basename)
  125.         search_cmd = 'hhsearch -dbstrlen 50 -B 1 -b 1 -p 60 -Z 1 -E 1E-03 -nocons -nopred -nodssp -Ofas {3} -cpu {0} -i {1} -d {2}'.format(args.threads,args.fasta, args.database, hhresult_file)
  126.         print("Executing HHsearch with the command:")
  127.         print(search_cmd)
  128.         hh_process = subprocess.Popen(
  129.                     search_cmd,\
  130.                     shell=True,\
  131.                     stdin=subprocess.PIPE,\
  132.                     stderr=subprocess.PIPE)
  133.         hh_process.wait()
  134.  
  135.     else:
  136.         print("No fasta or database has been detected. Check your input parameters.")
  137.         sys.exit(1)
  138.  
  139.     # Parse HHpred output fasta and acquire the best hit
  140.  
  141.     headers = []
  142.     with open(hhresult_file) as result_fasta:
  143.         for line in result_fasta:
  144.             if line.startswith(">"):
  145.                 headers.append(line.replace(">",""))
  146.  
  147.     top_hit = headers[1] # HHpred puts the query seq in index 0, so 1 is your top hit.
  148.     print("Your best hit was the PDB id:")
  149.     print(top_hit)
  150.     print("Full results are found in: " + hhresult_file)
  151.     hhresult_file.close()
  152.  
  153.     # Acquire the protein simulations
  154.  
  155.     model_list = []
  156.     for root, dirnames, filenames in os.walk(directory):
  157.         for filename in fnmatch.filter(filenames, '*.pdb'):
  158.             if filename.startswith(basename):
  159.                 model_list.append(os.path.join(root, filename))
  160.  
  161.     print("---------------------------")
  162.     print("Found the following models:")
  163.     for model_path in model_list:
  164.         print(model_path)
  165.  
  166.  
  167.     # Get reference structure from PDB
  168.     rc("open " + top_hit)
  169.  
  170.     # Open model structures
  171.     for model_path in model_list:
  172.         rc("open " + model_path)
  173.         print("Opened: " + model_path)
  174.  
  175.     rmsd_file = '{0}.tsv'.format(rmsd)
  176.     with open(rmsd_file, "a") as rmsd_output_file:
  177.  
  178.         for j in len(model_list):
  179. #           reference, simulation, rmsd = match(
  180.  
  181. #  match each simulation loaded in vs the top_hit PDB fetched from the web
  182. #
  183. #
  184.             rmsd_output_file.write(reference + "\t" + simulation + "\t" + rmsd)
  185.  
  186.  
  187. #  Chimera Script from Eric's suggestion:
  188. #   s1, s2 = openModels.list(modelTypes=[Molecule])
  189. #   c1, c2 = s1.sequence('A'), s2.sequence('A')
  190. #
  191.  
  192. #   atoms1, atoms2, rmsd = match(CP_SPECIFIC_SPECIFIC, [(c1, c2)], defaults[MATRIX],
  193. #           "nw", defaults[GAP_OPEN], defaults[GAP_EXTEND],
  194. #           iterate=defaults[ITER_CUTOFF])[0]
  195. #   for a1, a2 in zip(atoms1, atoms2):
  196. #       print a1.residue, a2.residue, a1.xformCoord().distance(a2.xformCoord())
  197. #
  198.  
  199.  
  200. if __name__ == '__main__' :
  201.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement