new force field option
[django_unres.git] / django_simple / todo / jobfiles.py
1 import logging
2
3 from django.db.models.signals import post_save, post_delete
4 from django.dispatch import receiver
5 from .models import Task
6 import os
7 import shutil
8 import subprocess
9 import json
10 from string import Template
11 import textwrap
12
13 logging.basicConfig()
14 logger = logging.getLogger(__name__)
15
16 def write_ssbond(ssbond):
17      list=[]
18      for e in ssbond:
19            list.append(e[0])
20            list.append(e[1])
21      list_sorted=sorted(list)
22      
23      if len(list)>0:
24        return str(len(list))+' '+' '.join(map(str,list_sorted))+'\n'+\
25             str(len(ssbond))+' '+' '.join(map(str,list))+'\n'
26      else: 
27        return '0\n'
28
29
30 @receiver(post_save, sender=Task)
31 def write_on_task_save(sender, instance, **kwargs):
32    if instance.ready and not instance.done and instance.running==0:
33      if (not os.path.isdir(instance.jobdirname)):
34         os.mkdir(instance.jobdirname)
35 #     os.chdir(instance.jobdirname)
36  
37
38
39 #md=Template('nstep=$nstep ntwe=100  ntwx=100   dt=0.20 damax=10.0 lang=1'+
40 #   't_bath=300 reset_vel=0 respa ntime_split=1 maxtime_split=512 mdpdb')
41 #wrapper = textwrap.TextWrapper(width=70)
42 #word_list = wrapper.wrap(text=md.substitute(nstep=str(nstep)))
43
44      if  instance.unres_ff =='E0LL2Y':
45       w="""WSC=1.00000 WSCP=1.23315 WELEC=0.84476 WBOND=1.00000 WANG=0.62954              &
46 WSCLOC=0.10554 WTOR=1.34316 WTORD=1.26571 WCORRH=0.19212 WCORR5=0.00000        &
47 WCORR6=0.00000 WEL_LOC=0.37357 WTURN3=1.40323 WTURN4=0.64673 WTURN6=0.00000    &
48 WVDWPP=0.23173 WHPB=1.00000 WSCCOR=0.25                                        &
49 CUTOFF=7.00000 WCORR4=0.00000
50 """
51      else:
52       w="""WSC=0.82686 WSCP=0.96947 WELEC=0.79373 WBOND=1.00000 WANG=0.46542              &
53 WSCLOC=0.07969 WTOR=0.81684 WTORD=0.67806 WCORRH=0.00000 WCORR5=0.00000        &
54 WCORR6=0.00000 WEL_LOC=0.71100 WTURN3=2.30298 WTURN4=0.86517 WTURN6=0.00000    &
55 WSCCOR=0.14577 WVDWPP=0.17781 WHPB=1.00000 WSCP14=0.00000                      &
56 CUTOFF=7.00000 WCORR4=0.00000
57 """
58      
59
60      with open(instance.jobdirname+'/file.inp','w') as f:
61        f.write(instance.name + ' UNRES server job'+ '\n')
62
63        if instance.type == 'min' :
64
65           control_line = 'SEED='+str(instance.md_seed)+' ' \
66              + instance.min_choice \
67              + ' pdbstart pdbref BOXX=1000. BOXY=1000. BOXZ=1000. '
68           if not instance.min_overlap:
69             control_line = control_line+'overlap '
70           if not instance.min_searchsc:
71             control_line = control_line+'nosearchsc '
72           if instance.min_pdbout:
73             control_line = control_line+'pdbout '
74           if instance.min_unres_pdb:
75             control_line = control_line+'unres_pdb '
76             
77           type_line = 'print_min_ini print_min_res print_min_stat'\
78             +' MAXMIN='+str(instance.min_maxmin)\
79             +' MAXFUN='+str(instance.min_maxfun)
80            
81
82        seq=instance.md_seq.replace(" ","")
83        dimen3=len(seq)
84        if dimen3>1:
85           if seq[0]=='X':
86             dimen3-=1
87           if seq[-1]=='X':
88             dimen3-=1
89   
90        nogly ='CDSQKIPTFNHLRWAVEYM'
91        seq2 = ''.join([c for c in seq if c in nogly])
92   
93        dimen3=(dimen3+len(seq2))*3
94          
95             
96        if instance.type == 'md' :
97           control_line = 'SEED='+str(instance.md_seed)+' '\
98            +instance.md_start +' md one_letter BOXX=1000. BOXY=1000. BOXZ=1000. '
99           if instance.md_pdbref:
100              control_line = control_line+'pdbref '
101             
102           type_line = 'reset_vel=0'\
103              +' t_bath='+str(instance.md_temp)\
104              +' nstep='+str(instance.md_nstep)\
105              +' ntwe='+str(instance.md_ntwe)\
106              +' ntwx='+str(instance.md_ntwx)\
107              +' dt='+str(instance.md_dt)
108           if instance.md_lang == 'langevin':
109             type_line = type_line + ' lang=1 '
110             type_line = type_line + 'scal_fric='+str(instance.md_scal_fric)
111           if instance.md_lang == 'berendsen':
112             type_line = type_line + ' lang=0 tbf '
113             type_line = type_line + 'tau_bath='+str(instance.md_tau)
114           if instance.md_respa:
115             type_line = type_line + ' respa '
116           if instance.md_mdpdb:
117             type_line = type_line + ' mdpdb '
118
119        if instance.type == 'remd' :
120           control_line = 'SEED='+str(instance.md_seed)+' '\
121            +instance.md_start +' re one_letter BOXX=1000. BOXY=1000. BOXZ=1000. '
122           if instance.md_pdbref:
123              control_line = control_line+'pdbref '
124            
125             
126           type_line = 'reset_vel='+ str(instance.remd_nstex)\
127              +' nstep='+str(instance.md_nstep)\
128              +' ntwe='+str(instance.md_ntwe)\
129              +' ntwx='+str(instance.remd_nstex)\
130              +' dt='+str(instance.md_dt)
131           if instance.md_lang == 'langevin':
132             type_line = type_line + ' lang=1 '
133             type_line = type_line + 'scal_fric='+str(instance.md_scal_fric)
134           if instance.md_lang == 'berendsen':
135             type_line = type_line + ' lang=0 tbf '
136             type_line = type_line + 'tau_bath='+str(instance.md_tau)
137           if instance.md_respa:
138             type_line = type_line + ' respa '
139           if instance.md_mdpdb:
140             type_line = type_line + ' mdpdb '
141           
142           type_line2 = 'nrep='+str(instance.remd_nrep) \
143              +' nstex='+str(instance.remd_nstex) \
144              +' tlist mlist sync nsyn='+str(instance.remd_nstex)\
145              +' traj1file  rest1file '
146
147        if instance.md_start == 'pdbstart':
148              type_line = type_line + ' preminim cart'
149
150        wrapper = textwrap.TextWrapper(width=70)
151
152        word_list = wrapper.wrap(text=control_line)
153        for element in word_list[:-1]:
154            f.write('{:79}'.format(element)+'&\n')
155        f.write(word_list[-1]+'\n')
156
157        word_list = wrapper.wrap(text=type_line)
158        for element in word_list[:-1]:
159            f.write('{:79}'.format(element)+'&\n')
160        f.write(word_list[-1]+'\n')
161           
162        if instance.md_start == 'pdbstart':
163          f.write('print_min_ini print_min_res print_min_stat\n')
164
165        
166        if instance.type == 'remd' :
167           word_list = wrapper.wrap(text=type_line2)
168           for element in word_list[:-1]:
169               f.write('{:79}'.format(element)+'&\n')
170           f.write(word_list[-1]+'\n')
171
172           tmp1=json.loads(instance.remd_multi_t)
173           string1=" "
174           word_list = wrapper.wrap(text=string1.join(tmp1))
175           for element in word_list[:-1]:
176               f.write('{:79}'.format(element)+'&\n')
177           f.write(word_list[-1]+'\n')
178
179           tmp1=json.loads(instance.remd_multi_m)
180           string1=" "
181           word_list = wrapper.wrap(text=string1.join(tmp1))
182           for element in word_list[:-1]:
183               f.write('{:79}'.format(element)+'&\n')
184           f.write(word_list[-1]+'\n')
185
186        f.write(w)
187
188
189        if instance.type == 'min' or instance.md_start == 'pdbstart':
190           f.write('plik.pdb\n')
191           f.write(write_ssbond(instance.ssbond))
192           f.write('0\n')
193
194
195        if instance.type == 'md' and instance.md_start != 'pdbstart':
196
197           if instance.md_pdbref:
198              f.write('plik.pdb\n')
199           f.write(str(len(seq))+'\n')
200           for i in range(0,len(seq),80):
201              f.write(seq[i:i+80]+'\n')
202           f.write('0\n0\n')
203
204        if instance.type == 'remd' and instance.md_start != 'pdbstart':
205
206           if instance.md_pdbref:
207              f.write('plik.pdb\n')             
208           f.write(str(len(seq))+'\n')
209           for i in range(0,len(seq),80):
210              f.write(seq[i:i+80]+'\n')
211           f.write('0\n0\n')
212
213
214        
215         
216      if instance.type == 'min':
217       os.chdir(instance.jobdirname)
218       if  instance.unres_ff =='E0LL2Y':
219         shutil.copy('../files/pbs.csh','.')
220       else:
221         shutil.copy('../files/pbs_new.csh','pbs.csh')
222       ret_code = subprocess.Popen(' /opt/torque/bin/qsub pbs.csh', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
223       os.chdir('..')
224      elif instance.type == 'md':
225       os.chdir(instance.jobdirname)
226       if  instance.unres_ff =='E0LL2Y':
227         f1 = open('../files/pbs_md.csh', 'r')
228       else:
229         f1 = open('../files/pbs_md_new.csh', 'r') 
230       f2 = open('pbs.csh', 'w')
231       for line in f1:
232            f2.write(line.replace('$dimen3',str(dimen3) ))
233       f1.close()
234       f2.close()  
235
236       f1 = open('file.seq', 'w')
237       for i in range(0,len(seq),80):
238              f1.write(seq[i:i+80]+'\n')
239       f1.close()
240
241       ret_code = subprocess.Popen(' /opt/torque/bin/qsub pbs.csh', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)      
242       os.chdir('..')
243      else:
244 # instance.type == 'remd'
245       tmp1=json.loads(instance.remd_multi_m)
246       nreplicas=sum(map(int, tmp1))
247 # hardcoded for 4 core nodes with FGPROC=2
248       nodes=int(nreplicas*2/4)
249       reszta = nreplicas*2-nodes*4
250       if reszta == 0 :
251         pbs = '#PBS -l nodes='+str(nodes)+':ppn=4'
252       else:
253         pbs = '#PBS -l nodes='+str(nodes)+':ppn=4+1:ppn='+str(reszta)
254
255       os.chdir(instance.jobdirname)
256       if  instance.unres_ff =='E0LL2Y':
257         f1 = open('../files/pbs8.csh', 'r')
258       else:
259         f1 = open('../files/pbs8_new.csh', 'r')
260       f2 = open('pbs8.csh', 'w')
261       for line in f1:
262           if '#PBS -l nodes=4:ppn=4' in line:
263            f2.write(line.replace('#PBS -l nodes=4:ppn=4', pbs))
264           else:
265            tmp1=json.loads(instance.remd_multi_t)
266            f2.write(line.replace('$temperatures','"'+" ".join(tmp1)+'" '+str(nreplicas)))
267       f1.close()
268       f2.close()
269       os.chdir('..')  
270
271 # write wham & cluster input
272       with open(instance.jobdirname+'/file_wham.inp','w') as f:
273           if instance.md_nstep/instance.remd_nstex*nreplicas<=8000:
274               isampl=1
275           else:
276               isampl=int(instance.md_nstep/instance.remd_nstex*nreplicas/8000)
277
278           if instance.md_pdbref:
279             f.write('SEED='+str(instance.md_seed)+' isampl='+str(isampl)+
280                ' einicheck=1 rescale=2 delta=0.02 cxfile classify\n')
281           else:    
282             f.write('SEED='+str(instance.md_seed)+' isampl='+str(isampl)+
283               ' einicheck=1 rescale=2 delta=0.02 cxfile\n')
284           f.write('nres='+str(len(seq))+' one_letter\n')
285           for i in range(0,len(seq),80):
286              f.write(seq[i:i+80]+'\n')
287
288           f.write(write_ssbond(instance.ssbond))
289
290
291           f.write(w)
292           f.write('\n')
293           f.write('nt='+str(instance.remd_nrep)+' replica read_iset\n')
294           tmp1=json.loads(instance.remd_multi_t)
295           for element in tmp1:
296               f.write('nr=1 temp='+element+' fi=0.0 0.0 0.0 0.0 0.0\n')
297               f.write('kh= 0.0 Q0=0.0\n')
298           rec=instance.md_nstep/instance.remd_nstex
299           f.write('nfile_cx=1 rec_start='+str(rec/10)
300                   +' rec_end='+str(rec)+' totraj='+str(nreplicas)+'\n')
301           f.write('file_MD000\n')
302           if instance.md_pdbref:
303              f.write('pdbref\n')
304              f.write('plik.pdb\n')
305
306            
307       with open(instance.jobdirname+'/file_cluster.inp','w') as f:
308           f.write(instance.name + ' UNRES server job'+ '\n')
309           f.write('nres='+str(len(seq))
310              +' one_letter rescale=2 PRINT_CART PDBOUT=1 iopt=1'
311              +' temper='+str(instance.remd_cluter_temp)+'\n')
312           f.write(w)
313           for i in range(0,len(seq),80):
314              f.write(seq[i:i+80]+'\n')
315           f.write(write_ssbond(instance.ssbond))
316       
317       os.chdir(instance.jobdirname)  
318       ret_code = subprocess.Popen(' /opt/torque/bin/qsub pbs8.csh', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
319       os.chdir('..')
320      
321      print ret_code.stdout.readlines()
322      logger.warning("Dir \"%s\" has been saved." % instance.jobdirname)
323      
324 @receiver(post_delete, sender=Task)
325 def delete_on_task_del(sender, instance, **kwargs):     
326    if instance.jobdirname:
327       if os.path.isdir(instance.jobdirname):
328          shutil.rmtree(instance.jobdirname)
329          logger.warning("Dir \"%s\" has been rm." % instance.jobdirname)         
330