Rafal's code for NMR restraints
[django_unres.git] / django_simple / todo / forms.py
index 74779e2..c857602 100644 (file)
@@ -5,6 +5,9 @@ from .models import MD_START
 from .models import MD_LANG
 from .models import FF_CHOICE
 from django.utils.safestring import mark_safe
+#---v
+from django.core.files import File
+#---^
 import json
 import urllib
 
@@ -67,6 +70,36 @@ def pdb_missing_res_chain(file,chain):
                msg='wrong chain id'
    return(msg)
 
+#---v
+def nmr_bad_cols(file):
+  msg=''
+  for line in file:
+    line=line.rstrip()
+    if line:
+      data = line.split()
+      for x in (0, 1, 2):
+        lg = data[x].isdigit()
+        if not lg:
+          msg = 'NMR shifts data column '+repr(x+1)+' must be integer, not: '+data[x]
+          return(msg)
+      dlug = len(data)
+      if (dlug < 5):
+        msg = 'There have to be 5 or more columns in NMR shifts data file'
+        return(msg)
+      promien = data[3].replace('.', '')
+      lg2 = promien.isdigit()
+      if not lg2:
+          msg ='NMR shifts 4th data column must be decimal or integer radius, not: '+data[3]
+          return(msg)
+      lg3 = data[4].startswith('H') or data[4].startswith('Q')
+      if not lg3:
+        msg = 'NMR shifts 5th data column must be proton designation, not: '+data[4]
+        return(msg)
+    else:
+      msg = 'NMR shifts data contains blank lines. Remove them.'
+  return(msg)
+#---^
+
 
 def code_2d(line):
    msg=''
@@ -263,8 +296,17 @@ class TaskForm_md_a(forms.Form):
                      required=False,
                      widget=forms.Textarea(attrs={'cols': 70, 'rows': 2}))
 
+#---v
+     use_nmr = forms.BooleanField(required=False,label='use NMR restraints',
+                  help_text='this option requires the next four fields to be set.')
+     file3 = forms.FileField(label='Upload a NMR restraints file',required=False,
+                  help_text='at least 5 columns required:     49      22      1       5.0     HD2     H; <i>Hint: if you are using <u>NMR example data</u> then the field will be filled out automatically</i>')
+     nmr_scal = forms.IntegerField(label='NMR option: scal_peak',initial=20)
+     nmr_fordepth = forms.FloatField(label='NMR option: FORDEPTH_PEAK',initial=0.5)
+     nmr_slope = forms.FloatField(label='NMR option: SLOPE_PEAK',initial=0.05)
      file1 = forms.FileField(label='Upload a PDB file',required=False,
-                  help_text='starting structure for pdbstart/reference structure')
+                  help_text='starting structure for pdbstart/reference structure; <i>Hint: if you are using <u>NMR example data</u> then the field will be filled out automatically</i>')
+#---^
      pdbid = forms.CharField(min_length=4,max_length=6,required=False,
       widget=forms.TextInput(attrs={'size':6, 'maxlength':6, 'title':'PDB code or PDB code:chain id'}),
       label='or PDB code (:chain)')                   
@@ -300,13 +342,43 @@ class TaskForm_md_a(forms.Form):
      def clean(self):
              cleaned_data = super(TaskForm_md_a, self).clean()
 
+#---v
+             md_seq = cleaned_data.get("md_seq")
+             if (md_seq == 'TDELLERLRQLFEELHERGTEIVVEVHINGERDEIRVRNISKEELKKLLERIREKIEREGSSEVEVNVHSGGQTWTFNEK'):
+               reopn1 = open('files/6msp-from-THR.pdb', 'r')
+               file1 = File(reopn1)
+             else:
+#---^
+               file1 = cleaned_data.get("file1")
              md_start = cleaned_data.get("md_start") 
-             file1 = cleaned_data.get("file1")
              pdbid = cleaned_data.get("pdbid")
              md_seq = cleaned_data.get("md_seq")
              md_pdbref = cleaned_data.get("md_pdbref")
              md_2d = cleaned_data.get("md_2d")
               
+#---v
+             if (md_seq == 'TDELLERLRQLFEELHERGTEIVVEVHINGERDEIRVRNISKEELKKLLERIREKIEREGSSEVEVNVHSGGQTWTFNEK'):
+               reopn = open('files/N1008_AmbiR-trunc.txt', 'r')
+               file3 = File(reopn)
+             else:
+               file3 = cleaned_data.get("file3")
+             unres_ff = cleaned_data.get("unres_ff")
+             use_nmr = cleaned_data.get("use_nmr")
+             nmr_scal = cleaned_data.get("nmr_scal")
+                    nmr_fordepth = cleaned_data.get("nmr_fordepth")
+             nmr_slope = cleaned_data.get("nmr_slope")
+             if (use_nmr and file3):
+               msg=nmr_bad_cols(file3)
+               if msg != '':
+                 self.add_error('file3',msg)
+             else:
+               if (use_nmr and not file3):
+                 self.add_error('file3','"Use NMR restarints" and "Upload a NMR restr. file" must be used together')
+
+             if (use_nmr and (unres_ff != 'NEWCT-9P')):
+               self.add_error('unres_ff','"Use NMR restarints" requires NEWCT-9P force field')
+
+#---^
              if md_start == 'pdbstart' and not (file1 or pdbid):
                 msg = 'pdbstart with no PDB file or code'
                 self.add_error('file1', msg)
@@ -404,8 +476,17 @@ class TaskForm_remd_a(forms.Form):
                      help_text='single letter code: H helix, E extended/beta, C or - no restraints',
                      required=False,
                      widget=forms.Textarea(attrs={'cols': 70, 'rows': 2}))
+#---v
+     use_nmr = forms.BooleanField(required=False,label='use NMR restraints',
+                  help_text='this option requires the next four fields to be set.')
+     file3 = forms.FileField(label='Upload a NMR restraints file',required=False,
+       help_text='at least 5 columns required:     49      22      1       5.0     HD2     H; <i>Hint: if you are using <u>NMR example data</u> then the field will be filled out automatically</i>')
+     nmr_scal = forms.IntegerField(label='NMR option: scal_peak',initial=20)
+     nmr_fordepth = forms.FloatField(label='NMR option: FORDEPTH_PEAK',initial=0.5)
+     nmr_slope = forms.FloatField(label='NMR option: SLOPE_PEAK',initial=0.05)
+#---^
      file1 = forms.FileField(label='Upload a PDB file',required=False,
-                  help_text='starting structure for pdbstart/reference structure')
+                  help_text='starting structure for pdbstart/reference structure; <i>Hint: if you are using <u>NMR example data</u> then the field will be filled out automatically</i>')
      pdbid = forms.CharField(min_length=4,max_length=6,required=False,
       widget=forms.TextInput(attrs={'size':6, 'maxlength':6, 'title':'PDB code or PDB code:chain id'}),
       label='or PDB code (:chain)')                   
@@ -461,12 +542,40 @@ class TaskForm_remd_a(forms.Form):
              cleaned_data = super(TaskForm_remd_a, self).clean()
 
              md_start = cleaned_data.get("md_start") 
-             file1 = cleaned_data.get("file1")
-             pdbid = cleaned_data.get("pdbid")
+#---v
              md_seq = cleaned_data.get("md_seq")
+             if (md_seq == 'TDELLERLRQLFEELHERGTEIVVEVHINGERDEIRVRNISKEELKKLLERIREKIEREGSSEVEVNVHSGGQTWTFNEK'):
+               reopn1 = open('files/6msp-from-THR.pdb', 'r')
+               file1 = File(reopn1)
+             else:
+               file1 = cleaned_data.get("file1")
+#---^
+             pdbid = cleaned_data.get("pdbid")
              md_pdbref = cleaned_data.get("md_pdbref")
              md_2d = cleaned_data.get("md_2d")
-              
+#---v
+             if (md_seq == 'TDELLERLRQLFEELHERGTEIVVEVHINGERDEIRVRNISKEELKKLLERIREKIEREGSSEVEVNVHSGGQTWTFNEK'):
+               reopn = open('files/N1008_AmbiR-trunc.txt', 'r')
+               file3 = File(reopn)
+             else:
+               file3 = cleaned_data.get("file3")
+             unres_ff = cleaned_data.get("unres_ff")
+             use_nmr = cleaned_data.get("use_nmr")
+             nmr_scal = cleaned_data.get("nmr_scal")
+                    nmr_fordepth = cleaned_data.get("nmr_fordepth")
+             nmr_slope = cleaned_data.get("nmr_slope")
+             if (use_nmr and file3):
+               msg=nmr_bad_cols(file3)
+               if msg != '':
+                 self.add_error('file3',msg)
+             else:
+               if (use_nmr and not file3):
+                 self.add_error('file3','"Use NMR restarints" and "Upload a NMR restr. file" must be used together')
+
+             if (use_nmr and (unres_ff != 'NEWCT-9P')):
+               self.add_error('unres_ff','"Use NMR restarints" requires NEWCT-9P force field')
+
+#---^
              if md_start == 'pdbstart' and not (file1 or pdbid):
                 msg = 'pdbstart with no PDB file or code'
                 self.add_error('file1', msg)
@@ -671,5 +780,3 @@ class TaskForm_list(forms.Form):
         self.fields['temperatures'] = MultiExampleField(self.count)
         self.fields['multiplexing'] = MultiExampleField(self.count)        
      
-
-     
\ No newline at end of file