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