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