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