use pdbid to load pdb and set example input
[django_unres.git] / django_simple / todo / forms.py
1 from django import forms
2 from .models import Task
3 from .models import MIN_CHOICE
4 from .models import MD_START
5 from .models import MD_LANG
6 from .models import FF_CHOICE
7 import json
8 import urllib
9
10
11 class MultiWidgetBasic(forms.MultiWidget):
12     def __init__(self, count, attrs=None):
13         self.count=count
14         widgets = []
15         for i in range(self.count):
16            widgets.append(forms.TextInput())
17         super(MultiWidgetBasic, self).__init__(widgets, attrs)
18
19     template_name = 'multi.html'
20
21     def decompress(self, value):
22         if value:
23             return json.loads(value)
24         else:
25             return ['', '']
26
27
28 class MultiExampleField(forms.fields.MultiValueField):
29     def __init__(self, count, *args, **kwargs):
30         self.count=count
31         self.widget = MultiWidgetBasic(self.count)
32         list_fields = []
33         for i in range(self.count):
34            list_fields.append(forms.fields.CharField(max_length=15))
35         super(MultiExampleField, self).__init__(list_fields, *args, **kwargs)
36
37     def compress(self, values):
38         ## compress list to single object                                               
39         return json.dumps(values)
40
41
42 class TaskForm(forms.Form):
43     name = forms.CharField(max_length=20)
44
45
46 class TaskForm_min(forms.Form):
47      name = forms.CharField(max_length=20)
48      file1 = forms.FileField(label='Upload a PDB file',required=False,
49       help_text='continuous (without breaks) protein chains,use TER to divide chains')
50      pdbid = forms.CharField(min_length=4,max_length=4,required=False,
51       widget=forms.TextInput(attrs={'size':4, 'maxlength':4, 'title':'PDB code'}),
52       label='or PDB code')                   
53
54      def clean(self):
55              cleaned_data = super(TaskForm_min, self).clean()
56
57              pdbid = cleaned_data.get("pdbid") 
58              file1 = cleaned_data.get("file1")
59               
60              if not pdbid and not file1:
61                 msg = 'provide pdb file or pdb code'
62                 self.add_error('file1', msg)
63               
64              if pdbid:
65                test=urllib.urlopen('http://files.rcsb.org/download/'+pdbid+'.pdb')     
66                if test.code != 200:
67                  msg = 'wrong pdb code'
68                  self.add_error('pdbid', msg)
69                test.close()
70             
71 class TaskForm_min_a(forms.Form):
72      name = forms.CharField(max_length=20)
73
74      unres_ff = forms.ChoiceField(choices=FF_CHOICE,widget=forms.RadioSelect,
75                            label='Force Field',initial='E0LL2Y')
76      min_choice = forms.ChoiceField(choices=MIN_CHOICE,label='minimization algorithm')
77      min_overlap = forms.BooleanField(required=False,label='remove overlap')
78      min_searchsc = forms.BooleanField(required=False,label='MC for sidechain overlap')
79      min_maxmin = forms.IntegerField(label='MAXMIN',initial=10000,
80                   help_text='maximum number of iterations')
81      min_maxfun = forms.IntegerField(label='MAXFUN',initial=15000,
82                   help_text='maximum number of function evaluations')
83      file1 = forms.FileField(label='Upload a PDB file',
84         help_text='continuous (without breaks) protein chains,use TER to divide chains')
85
86      min_unres_pdb = forms.BooleanField(required=False,label='uploaded input unres PDB',
87                   help_text='(CA and CB atoms only, CB represents SC in UNRES)')
88      min_pdbout = forms.BooleanField(required=False,label='output PDB',initial='true')
89      boxx = forms.FloatField(label='Box X',initial=1000.0,
90                        help_text='box x dimension')
91      boxy = forms.FloatField(label='Box Y',initial=1000.0,
92                        help_text='box y dimension')
93      boxz = forms.FloatField(label='Box Z',initial=1000.0,
94                        help_text='box z dimension')
95
96
97 class TaskForm_md(forms.Form):
98      name = forms.CharField(max_length=20)
99
100      md_start = forms.ChoiceField(choices=MD_START,widget=forms.RadioSelect,
101                       label='starting structure',initial='extconf')
102      md_seq = forms.CharField(label='Sequence',
103                      help_text='aminoacid sequence using one letter code<br>'+
104                      'field is ignored when uploading starting/reference PDB file',
105                      required=False,
106                      widget=forms.Textarea(attrs={'cols': 70, 'rows': 2}))
107      file1 = forms.FileField(label='Upload a PDB file',required=False,
108                   help_text='starting structure for pdbstart/reference structure')
109      md_pdbref = forms.BooleanField(required=False,label='PDB reference structure')
110      md_temp = forms.FloatField(label='temperature',initial=300,
111                   help_text='bath temperature')
112      md_nstep = forms.IntegerField(label='NSTEP',initial=200000,
113                   help_text='total number of steps')
114      md_seed = forms.IntegerField(label='SEED',initial=-39912345,
115                   help_text='seed for random number generator')
116                   
117      def clean(self):
118              cleaned_data = super(TaskForm_md, self).clean()
119
120              md_start = cleaned_data.get("md_start") 
121              file1 = cleaned_data.get("file1")
122              md_seq = cleaned_data.get("md_seq")
123              md_pdbref = cleaned_data.get("md_pdbref")
124               
125              if md_start == 'pdbstart' and not file1:
126                 msg = 'pdbstart with no PDB file'
127                 self.add_error('file1', msg)
128
129              if md_pdbref and not file1:
130                 msg = 'pdbref with no PDB file'
131                 self.add_error('file1', msg)
132
133
134              if md_start != 'pdbstart' and not md_pdbref and not md_seq:
135                 msg = 'extended/random chain with no sequence'
136                 self.add_error('md_seq', msg)
137
138                         
139 class TaskForm_md_a(forms.Form):
140      name = forms.CharField(max_length=20)
141
142      unres_ff = forms.ChoiceField(choices=FF_CHOICE,widget=forms.RadioSelect,
143                            label='Force Field',initial='E0LL2Y')
144      md_start = forms.ChoiceField(choices=MD_START,widget=forms.RadioSelect,
145                       label='starting structure',initial='extconf')
146      md_seq = forms.CharField(label='Sequence',
147                      help_text='aminoacid sequence using one letter code<br>'+
148                      'field is ignored when uploading starting/reference PDB file',
149                      required=False,
150                      widget=forms.Textarea(attrs={'cols': 70, 'rows': 2}))
151      file1 = forms.FileField(label='Upload a PDB file',required=False,
152                   help_text='starting structure for pdbstart/reference structure')
153      md_pdbref = forms.BooleanField(required=False,label='PDB reference structure')                  
154      md_temp = forms.FloatField(label='temperature',initial=300,
155                   help_text='bath temperature')
156      md_nstep = forms.IntegerField(label='NSTEP',initial=200000,
157                   help_text='total number of steps')
158      md_seed = forms.IntegerField(label='SEED',initial=-39912345,
159                   help_text='seed for random number generator')
160
161      md_ntwe = forms.IntegerField(label='NTWE',initial=1000,
162                help_text='write statfile every ntwe steps')
163      md_ntwx = forms.IntegerField(label='NTWX',initial=1000,
164                help_text='write trajectory every ntwe steps')
165      md_dt = forms.FloatField(label='DT',initial=0.2,
166                   help_text='time step [mtu]')
167      md_lang = forms.ChoiceField(choices=MD_LANG,label='thermostat')
168      md_tau = forms.FloatField(label='tau_bath',initial=1.0,
169                   help_text='coupling to the thermal bath (Berendsen)')
170      md_scal_fric = forms.FloatField(label='scal_froc',initial=0.02,
171                   help_text='scaling of the friction coefficients (Langevin)')
172      md_mdpdb = forms.BooleanField(required=False,label='trajectory as PDB')
173
174      boxx = forms.FloatField(label='Box X',initial=1000.0,
175                        help_text='box x dimension')
176      boxy = forms.FloatField(label='Box Y',initial=1000.0,
177                        help_text='box y dimension')
178      boxz = forms.FloatField(label='Box Z',initial=1000.0,
179                        help_text='box z dimension')
180
181      def clean(self):
182              cleaned_data = super(TaskForm_md_a, self).clean()
183
184              md_start = cleaned_data.get("md_start") 
185              file1 = cleaned_data.get("file1")
186              md_seq = cleaned_data.get("md_seq")
187              md_pdbref = cleaned_data.get("md_pdbref")
188               
189              if md_start == 'pdbstart' and not file1:
190                 msg = 'pdbstart with no PDB file'
191                 self.add_error('file1', msg)
192
193              if md_pdbref and not file1:
194                 msg = 'pdbref with no PDB file'
195                 self.add_error('file1', msg)
196
197
198              if md_start != 'pdbstart' and not md_pdbref and not md_seq:
199                 msg = 'extended/random chain with no sequence'
200                 self.add_error('md_seq', msg)
201
202
203 class TaskForm_remd(forms.Form):
204      name = forms.CharField(max_length=20)
205
206      md_start = forms.ChoiceField(choices=MD_START,widget=forms.RadioSelect,
207                       label='starting structure',initial='extconf')
208      md_seq = forms.CharField(label='Sequence',
209                      help_text='aminoacid sequence using one letter code<br>'+
210                         'field is ignored when uploading starting/reference PDB file',
211                      required=False,
212                      widget=forms.Textarea(attrs={'cols': 70, 'rows': 2}))
213      file1 = forms.FileField(label='Upload a PDB file',required=False,
214                   help_text='starting structure for pdbstart/reference structure')
215      md_pdbref = forms.BooleanField(required=False,label='PDB reference structure')                  
216      md_nstep = forms.IntegerField(label='NSTEP',initial=200000,
217                   help_text='total number of steps')
218      md_seed = forms.IntegerField(label='SEED',initial=-39912345,
219                   help_text='seed for random number generator')
220                   
221      def clean(self):
222              cleaned_data = super(TaskForm_remd, self).clean()
223
224              md_start = cleaned_data.get("md_start") 
225              file1 = cleaned_data.get("file1")
226              md_seq = cleaned_data.get("md_seq")
227              md_pdbref = cleaned_data.get("md_pdbref")
228               
229              if md_start == 'pdbstart' and not file1:
230                 msg = 'pdbstart with no PDB file'
231                 self.add_error('file1', msg)
232
233              if md_pdbref and not file1:
234                 msg = 'pdbref with no PDB file'
235                 self.add_error('file1', msg)
236
237              if md_start != 'pdbstart' and not md_pdbref and not md_seq:
238                 msg = 'extended/random chain with no sequence'
239                 self.add_error('md_seq', msg)
240
241                              
242 class TaskForm_remd_a(forms.Form):
243      name = forms.CharField(max_length=20)
244
245      unres_ff = forms.ChoiceField(choices=FF_CHOICE,widget=forms.RadioSelect,
246                            label='Force Field',initial='E0LL2Y')
247      md_start = forms.ChoiceField(choices=MD_START,widget=forms.RadioSelect,
248                       label='starting structure',initial='extconf')
249      md_seq = forms.CharField(label='Sequence',
250                      help_text='aminoacid sequence using one letter code<br>'+
251                       'field is ignored when uploading starting/reference PDB file',
252                      required=False,
253                      widget=forms.Textarea(attrs={'cols': 70, 'rows': 2}))
254      file1 = forms.FileField(label='Upload a PDB file',required=False,
255                   help_text='starting structure for pdbstart/reference structure')
256      md_pdbref = forms.BooleanField(required=False,label='PDB reference structure')                  
257      md_nstep = forms.IntegerField(label='NSTEP',initial=200000,
258                   help_text='total number of steps')
259      md_seed = forms.IntegerField(label='SEED',initial=-39912345,
260                   help_text='seed for random number generator')
261      md_ntwe = forms.IntegerField(label='NTWE',initial=1000,
262                help_text='write statfile every ntwe steps')
263      md_dt = forms.FloatField(label='DT',initial=0.2,
264                   help_text='time step [mtu]')
265      md_lang = forms.ChoiceField(choices=MD_LANG,label='thermostat')
266      md_tau = forms.FloatField(label='tau_bath',initial=1.0,
267                   help_text='coupling to the thermal bath (Berendsen)')
268      md_scal_fric = forms.FloatField(label='scal_froc',initial=0.02,
269                   help_text='scaling of the friction coefficients (Langevin)')
270      remd_nrep = forms.IntegerField(label='NREP',initial=8,
271                   help_text='number of replicas')
272      remd_nstex = forms.IntegerField(label='NSTEX',initial=1000,
273                   help_text='exchange and write trajectory every nstex steps')
274      md_ntwx = forms.IntegerField(label='NTWX',initial=1000,
275                help_text='write trajectory every ntwx steps')
276      remd_cluter_temp = forms.FloatField(label='TEMPER',
277                   help_text='temperature for cluster analysis',initial=280)                  
278 #     remd_traj1file = forms.BooleanField(required=False,label='single trajectory file',initial='true')
279 #     remd_rest1file = forms.BooleanField(required=False,label='single restart file',initial='true')
280
281      boxx = forms.FloatField(label='Box X',initial=1000.0,
282                        help_text='box x dimension')
283      boxy = forms.FloatField(label='Box Y',initial=1000.0,
284                        help_text='box y dimension')
285      boxz = forms.FloatField(label='Box Z',initial=1000.0,
286                        help_text='box z dimension')
287
288
289      def clean(self):
290              cleaned_data = super(TaskForm_remd_a, self).clean()
291
292              md_start = cleaned_data.get("md_start") 
293              file1 = cleaned_data.get("file1")
294              md_seq = cleaned_data.get("md_seq")
295              md_pdbref = cleaned_data.get("md_pdbref")
296               
297              if md_start == 'pdbstart' and not file1:
298                 msg = 'pdbstart with no PDB file'
299                 self.add_error('file1', msg)
300
301              if md_pdbref and not file1:
302                 msg = 'pdbref with no PDB file'
303                 self.add_error('file1', msg)
304
305
306              if md_start != 'pdbstart' and not md_pdbref and not md_seq:
307                 msg = 'extended/random chain with no sequence'
308                 self.add_error('md_seq', msg)
309
310
311 class TaskForm_list(forms.Form):
312     name = forms.CharField(max_length=20,disabled=True,required=False)
313     nrep = forms.IntegerField(disabled=True,required=False,label='NREP')
314
315     def __init__(self, count, *args, **kwargs):
316         super(TaskForm_list, self).__init__(*args, **kwargs)
317         self.count=count
318         self.fields['temperatures'] = MultiExampleField(self.count)
319         self.fields['multiplexing'] = MultiExampleField(self.count)        
320      
321
322