import os,sys,string,re, time
import assign_helices
import analyspdb
import display_helices
import CalcModes
import util
import settings
from MMTK import *
sys.path.append('./utilities/pyutils')
sys.path.append('./applications/tmm')
##
## tmm_start.py - initiates the calculation of the normal modes and predicts the TM alpha helical segments.
##
def tmm_start (items, results):
items['resultid'] = items['jobid']
results['resultid'] = items['resultid']
# if the user input to TMM is a pdb code, we retrieve the PDB file from the flat file
if items['pdbcode']:
print '...submitted pdb code:',items['pdbcode']
results['pdbcode'] = items['pdbcode']
#
#Aug07: The flat db stores pdbfiles with lowercase names.
#added convertion of pdb-code to lowercase to avoid error if user writes in upper case
#Note: The pdb-standard specifies letters in the pdb-code to be upper case
#
cleaned_code = items['pdbcode'].lower()
path = settings.PDB_PATH
file = 'pdb'+cleaned_code+'.ent'
filename = path + file
try:
filehandle = open(filename,'r')
items['pdbstruct'] = filehandle.read()
except:
print 'This is not an existing pdb code'
results['modefile_name'] = ''
results['messages'] = 'This is not an existing pdb code'
results['status'] = 'FAILED'
return results
else:
print 'no pdb code entered'
print 'checking uploaded pdbfile'
# the pdb structure is now saved in the variable items['pdbstruct'] even though the input was a pdb code
if items['pdbstruct']:
if items['pdbcode'] == '':
results['pdbcode'] = items['jobid'][0:5]
items['pdbcode'] = items['jobid'][0:5]
print 'parsing the pdb file'
# If this test is true the file that has been entered is not a pdb file
if string.find(items['pdbstruct'],"ATOM") == -1:
results = items
results['pdbstruct'] = ' '
results['modefile_name'] = ''
results['status'] = 'FAILED'
results['messages'] = 'The file you entered is probably not a pdb-file. Please check that you have the right file and try again from the main page.'
return results
print 'pdbfile ok'
# Saving the pdb file to local disk
pdbfile_name = items['pdbfilename']
results['pdbfilename'] = items['pdbfilename']
pdbfile = open(pdbfile_name, 'w')
pdbfile.write(items['pdbstruct'])
pdbfile.close()
# Checking for the length of the structure. No structure should be larger than 20000 residues.
calphanb = analyspdb.analyspdb(items['pdbstruct'], items)
if (calphanb > 20000):
results['messages'] = 'This server does not support systems with more than 20000 residues'
results['status']='FAILED'
results['modefile_name'] = ''
return results
else:
print 'number of ca atoms:',calphanb
## using DSSP to find all alpha helices in the PDB structure
print 'Assigning helices to the protein...'
helices, residues, hydro, chains = assign_helices.AssignHelices( pdbfile_name )
#print residues
#print helices
print str(len(helices)) + ' found.'
## We calculate the normal modes, unless it exist on the hard drive though...
if items['pdbcode'] != items['jobid'][0:5]:
exist = exists(settings.MODE_DIR, items['pdbcode'])
## if exist is set to 0, files will not be looked up.
#exist = 0
if exist != 0:
saved_nmodes = len(load(exist))
print 'files stores ' + str(saved_nmodes) + ' modes'
user_wants_nmodes = int(float(calphanb) * 3.0 * float(items['nmodes']))
print 'user wants ' + str(user_wants_nmodes)
#if exist != 0 and saved_nmodes >= (user_wants_nmodes-10):
if exist != 0:
print 'Normal modes already calculated. Using file ' + exist + '.'
items['modefile_name'] = exist
else:
print 'Calculating the normal modes...'
items['modefile_name'] = './scratch/'+items['jobid'] + '.mode'
try:
print 'calcing'
html = CalcModes.CalcModes('./scratch/'+items['jobid']+'-2.pdb', items['modefile_name'], items['nmodes'])
print 'done'
util.saveModeFile(items['modefile_name'], settings.MODE_DIR, items['pdbcode'], items['nmodes'])
except AttributeError:
print 'modes cannot be calulated'
results['messages'] = 'An error occurred during the calculation of the normal modes. Please contact us at contact@bioinfo.no'
results['status']='FAILED'
results['modefile_name'] = ''
return results
else:
try:
print 'calcing'
items['modefile_name'] = './scratch/'+items['jobid'] + '.mode'
html = CalcModes.CalcModes('./scratch/'+items['jobid']+'-2.pdb', items['modefile_name'], items['nmodes'])
print 'done'
except AttributeError:
print 'modes cannot be calulated'
results['messages'] = 'An error occurred during the calculation of the normal modes. Please contact us at contact@bioinfo.no'
results['status']='FAILED'
results['modefile_name'] = ''
return results
## assigning some variables that will be sent right back to zope
print 'Finding which helices to use...'
items['display_0'] = pdbfile_name.replace('pdb', '0.png')
results['display_0'] = items['display_0']
results['vmdfilename'] = pdbfile_name.replace('pdb', 'vmd')
results['vmd'] = results['vmdfilename']
results['chainlist'] = util.turnList(chains)
if len(results['chainlist']) == 0:
results['chainlist'].append('')
## This is where we find all the TM alpha helices. They are stored in the variable 'bundle'
## I guess we produce the VMD file here as well
bundle, reason = display_helices.HelixWrapper( items, helices, pdbfile_name, hydro, residues, chains )
## All lists should be strings when it is sent to biaz and zope
name_list = ['']*len(helices)
results['bundle'], results['reason'], results['chains'], results['h_start'], results['h_end'], results['tm_helices'], results['residues'], results['h_name'] = util.convertToStrings(bundle, reason, helices, residues, name_list )
#print "tm_helices"
#print results['tm_helices']
#print "h_start"
#print results['h_start']
#print "h_end"
#print results['h_end']
#print "h_name"
#print results['h_name']
results['jobid'] = items['jobid']
results['modefile_name'] = items['modefile_name']
## Then we have to edit the VMD file to make it more convenient for the user
edit_vmd_file(results['vmdfilename'])
else:
results['status'] = 'FAILED'
results['messages'] = 'Please submit a PDB file or a PDB code'
results['modefile_name'] = ''
return results
return results
## Function for finding if the normal mode file of a protein exist
def exists(mode_dir, pdbcode):
directories = ['full_set', 'half_set', 'small_set', '200']
#directories = ['1']
for dir in directories:
dir = mode_dir + dir + '/'
name = dir + pdbcode + '.mode'
exists = 1
try:
about = os.stat(name)
print "Size of file is "+str(about[6])+ " bytes"
return name
except OSError:
print "File does not exist or is not accessible"
exists = 0
return 0
# The VMD file is allready made, but we have to change something in it...
def edit_vmd_file( vmdfilename ):
statefile = open(vmdfilename, 'r')
array = []
array = statefile.readlines()
statefile.close()
new_statefile = open(vmdfilename, 'w')
c=0
while c < len(array):
# changes and removes a bunch of crap
if array[c][0:7] == 'mol new':
str = 'mol new {myPdbFile.pdb} type pdb\n'
new_statefile.write(str)
elif array[c][0:5] == 'save_':
str = ''
elif array[c][0:5] == 'rende':
str = ''
elif array[c][0:4] == 'quit':
str = 'lugh'
elif array[c][0:4] == 'scal':
str = 'lugh'
elif array[c][0:4] == 'colo':
str = 'lugh'
elif array[c][0:4] == 'axes':
str = 'lugh'
elif array[c][0:8] == 'mol load':
str = 'lugh'
#print array[c][0:8]
else:
#print array[c]
new_statefile.write(array[c])
c=c+1
new_statefile.close()
return