00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 """
00020 Wrapper for the CAD score.
00021
00022 References:
00023
00024 Olechnovic K, Kulberkyte E, Venclovas C., CAD-score: A new contact area
00025 difference-based function for evaluation of protein structural models
00026 Proteins. 2012 Aug 30. [Epub ahead of print]
00027
00028 Authors: Valerio Mariani, Alessandro Barbato
00029 """
00030
00031 import subprocess, os, tempfile, platform
00032 from ost import settings, io
00033
00034 def _SetupFiles(model,reference):
00035
00036 tmp_dir_name=tempfile.mkdtemp()
00037 dia = 'PDB'
00038 for chain in model.chains:
00039 if chain.name==" ":
00040 raise RuntimeError("One of the chains in the model has no name. Cannot calculate CAD score")
00041 if len(chain.name) > 1:
00042 dia = 'CHARMM'
00043 break;
00044 for res in chain.residues:
00045 if len(res.name) > 3:
00046 dia = 'CHARMM'
00047 break;
00048 io.SavePDB(model, os.path.join(tmp_dir_name, 'model.pdb'), dialect=dia)
00049 dia = 'PDB'
00050 for chain in reference.chains:
00051 if chain.name==" ":
00052 raise RuntimeError("One of the chains in the reference has no name. Cannot calculate CAD score")
00053 if len(chain.name) > 1:
00054 dia = 'CHARMM'
00055 break;
00056 for res in chain.residues:
00057 if len(res.name) > 3:
00058 dia = 'CHARMM'
00059 break;
00060 io.SavePDB(reference, os.path.join(tmp_dir_name, 'reference.pdb'),dialect=dia)
00061 return tmp_dir_name
00062
00063 def _CleanupFiles(dir_name):
00064 import shutil
00065 shutil.rmtree(dir_name)
00066
00067
00068 class CADResult:
00069 """
00070 Holds the result of running CAD
00071
00072 .. attribute:: globalAA
00073
00074 The global CAD's atom-atom (AA) score
00075
00076 .. attribute:: localAA
00077
00078 Dictionary containing local CAD's atom-atom (AA) scores.
00079
00080 :type: dictionary (key: chain.resnum (e.g.: A.24), value: CAD local AA score (see CAD Documentation online)
00081 """
00082 def __init__(self, globalAA, localAA):
00083 self.globalAA=globalAA
00084 self.localAA=localAA
00085
00086 def _ParseCADGlobal(lines):
00087 interesting_lines=lines[1]
00088 aa=float(interesting_lines.split()[10])
00089 return aa
00090
00091 def _ParseCADLocal(lines):
00092 local_aa_dict={}
00093 for lin in lines[11:]:
00094 items=lin.split()
00095 chain=items[0]
00096 resnum=int(items[1])
00097 key=chain+'.'+str(resnum)
00098 aa=float(items[2])
00099 local_aa_dict[key]=aa
00100 return local_aa_dict
00101
00102 def _RunCAD(max_iter, tmp_dir):
00103 model_filename=os.path.join(tmp_dir, 'model.pdb')
00104 reference_filename=os.path.join(tmp_dir, 'reference.pdb')
00105 if platform.system() == "Windows":
00106 raise RuntimeError('CAD score not available on Windows')
00107 else:
00108 cad_calc_path=settings.Locate('CADscore_calc.bash')
00109 cad_read_g_path=settings.Locate('CADscore_read_global_scores.bash')
00110 cad_read_l_path=settings.Locate('CADscore_read_local_scores.bash')
00111 command1="\"%s\" -o %i -m \"%s\" -t \"%s\" -D \"%s\"" %(cad_calc_path, max_iter, model_filename, reference_filename, os.path.join(tmp_dir,"cadtemp"))
00112 command2="\"%s\" -D \"%s\"" %(cad_read_g_path, os.path.join(tmp_dir,"cadtemp"))
00113 command3="\"%s\" -m \"%s\" -t \"%s\" -D \"%s\" -c AA" %(cad_read_l_path, model_filename, reference_filename,os.path.join(tmp_dir,"cadtemp"))
00114 ps1=subprocess.Popen(command1, shell=True, stdout=subprocess.PIPE)
00115 ps1.wait()
00116 ps2=subprocess.Popen(command2, shell=True, stdout=subprocess.PIPE)
00117 ps2.wait()
00118 lines=ps2.stdout.readlines()
00119 try:
00120 globalAA=_ParseCADGlobal(lines)
00121 except:
00122 raise RuntimeError("CAD calculation failed")
00123 ps3=subprocess.Popen(command3, shell=True, stdout=subprocess.PIPE)
00124 ps3.wait()
00125 lines=ps3.stdout.readlines()
00126 try:
00127 localAA=_ParseCADLocal(lines)
00128 except:
00129 raise RuntimeError("CAD calculation failed")
00130
00131 return CADResult(globalAA,localAA)
00132
00133
00134 def CADScore(model, reference, max_iter=300):
00135 """
00136 Calculates global and local atom-atom (AA) CAD Scores
00137
00138
00139 :param model: The model structure.
00140 :type model: :class:`~ost.mol.EntityView` or :class:`~ost.mol.EntityHandle`
00141 :param reference: The reference structure
00142 :type model2: :class:`~ost.mol.EntityView` or :class:`~ost.mol.EntityHandle`
00143 :param max_iter: Optional. The maximum number of iteration for the tessellation algorithm before giving up. By default 300
00144 :returns: The result of the CAD score calculation
00145 :rtype: :class:`CADResult`
00146
00147 :raises: :class:`~ost.settings.FileNotFound` if any of the CAD score exacutables could not be located.
00148 :raises: :class:`RuntimeError` if the calculation failed
00149 """
00150 tmp_dir_name=_SetupFiles(model, reference)
00151 result=_RunCAD(max_iter, tmp_dir_name)
00152 _CleanupFiles(tmp_dir_name)
00153 return result
00154