<commit_before>from django import forms

from basicviz.models import Experiment
from decomposition.models import MotifSet
from basicviz.constants import EXPERIMENT_DECOMPOSITION_SOURCE
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _


class CreateExperimentForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(CreateExperimentForm, self).__init__(*args, **kwargs)
        self.fields['decompose_from'] = forms.ModelChoiceField(
            queryset=MotifSet.objects.all(),
            label='Decompose using Mass2Motifs in'
        )
        self.fields['ms2_file'].required = True
        self.fields['decompose_from'].required = False

    def is_valid(self):
      valid = super(CreateExperimentForm, self).is_valid()
      if not valid:
        return valid

      ms2_format = self.cleaned_data['experiment_ms2_format']
      ms2_file_name = self.cleaned_data['ms2_file'].name.lower()
      if ms2_format == '0':
        if not ms2_file_name.endswith('.mzml'):
          self.add_error('ms2_file', ValidationError(_('Error: Extension should be in .mzML format'), code='invalid'))
          return False
      elif ms2_format == '1':
        if not ms2_file_name.endswith('.msp'):
          self.add_error('ms2_file', ValidationError(_('Error: Extension should be in .msp format'), code='invalid'))
          return False
      elif ms2_format == '2':
        if not ms2_file_name.endswith('.mgf'):
          self.add_error('ms2_file', ValidationError(_('Error: Extension should be in .mgf format'), code='invalid'))
          return False

      return True

    class Meta:
        model = Experiment
        widgets = {
            'name': forms.TextInput(attrs={'style': 'width:300px'}),
            'description': forms.Textarea(attrs={'rows': 3, 'cols': 56}),
            'csv_file': forms.ClearableFileInput(),
            'ms2_file': forms.ClearableFileInput()
        }
        labels = {
            'name': '(Required) Experiment name. Note that this must be unique in the system',
            'description':'(Required) Experiment description.',
            'csv_file': 'MS1 file (CSV) [see below for formatting instructions]',
            'ms2_file': '(Required) MS2 file (mzML, msp, or mgf)',
            'isolation_window': 'Fragmentation isolation window. Used to match fragment spectra with MS1 peaks.',<commit_msg>a word<commit_after>from django import forms

from basicviz.models import Experiment
from decomposition.models import MotifSet
from basicviz.constants import EXPERIMENT_DECOMPOSITION_SOURCE
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _


class CreateExperimentForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(CreateExperimentForm, self).__init__(*args, **kwargs)
        self.fields['decompose_from'] = forms.ModelChoiceField(
            queryset=MotifSet.objects.all(),
            label='Decompose using Mass2Motifs in'
        )
        self.fields['ms2_file'].required = True
        self.fields['decompose_from'].required = False

    def is_valid(self):
      valid = super(CreateExperimentForm, self).is_valid()
      if not valid:
        return valid

      ms2_format = self.cleaned_data['experiment_ms2_format']
      ms2_file_name = self.cleaned_data['ms2_file'].name.lower()
      if ms2_format == '0':
        if not ms2_file_name.endswith('.mzml'):
          self.add_error('ms2_file', ValidationError(_('Error: Extension should be in .mzML format'), code='invalid'))
          return False
      elif ms2_format == '1':
        if not ms2_file_name.endswith('.msp'):
          self.add_error('ms2_file', ValidationError(_('Error: Extension should be in .msp format'), code='invalid'))
          return False
      elif ms2_format == '2':
        if not ms2_file_name.endswith('.mgf'):
          self.add_error('ms2_file', ValidationError(_('Error: Extension should be in .mgf format'), code='invalid'))
          return False

      return True

    class Meta:
        model = Experiment
        widgets = {
            'name': forms.TextInput(attrs={'style': 'width:300px'}),
            'description': forms.Textarea(attrs={'rows': 3, 'cols': 56}),
            'csv_file': forms.ClearableFileInput(),
            'ms2_file': forms.ClearableFileInput()
        }
        labels = {
            'name': '(Required) Experiment name. Note that this must be unique in the system',
            'description':'(Required) Experiment description.',
            'csv_file': 'MS1 file (CSV) [see above for formatting instructions]',
            'ms2_file': '(Required) MS2 file (mzML, msp, or mgf)',
            'isolation_window': 'Fragmentation isolation window. Used to match fragment spectra with MS1 peaks.',
{'fraction_non_alphanumeric': 0.11669770328988206, 'fraction_numerical': 0.012000827643285744, 'mean_word_length': 3.0418060200668897, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1330345', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1379, 'n_words': 350}