def clean(self): # we have to run this validator here again, because # chemicalformula2nominalmass expects proper formulae # django runs the validators _after_ this clean function validate_chemical_formula(self.chemical_formula) if self.isotope is True: if self.mass != chemicalformula2nominalmass(self.chemical_formula): raise ValidationError(u'Nominal mass and chemical formula are not compatible.') #check if either inchi or inchikey are there and either complete the other one or verify if self.inchi == '': if self.inchikey != '': tmpinchi = inchikey2inchi(self.inchikey) if tmpinchi: self.inchi = tmpinchi else: raise ValidationError(u'No chemical compound found for this InChI-Key.') else: #check if the given inchi has the InChI= in the beginning #additionally check for Standard-InChI if not self.inchi.startswith('InChI='): self.inchi = 'InChI=' + self.inchi if not self.inchi.startswith('InChI=1S'): raise ValidationError(u'InChI %s is not a Standard-InChI (starts with 1S)' % self.inchi) #get the rest if self.inchikey != '': inchikeycheck = inchi2inchikey(self.inchi) if inchikeycheck != self.inchikey: raise ValidationError(u'The given InChI and InChI-Key are not compatible.') else: tmpinchikey = inchi2inchikey(self.inchi) if tmpinchikey: self.inchikey = tmpinchikey else: raise ValidationError(u'Not a valid InChI-Key.') if self.chemical_formula != inchi2chemicalformula(self.inchi): raise ValidationError(u'InChI %s is not compatible with the stochiometric formula %s.' % (self.inchi, self.chemical_formula)) #we check if we already have a species with same chem formula, mass and isotope-status sp_search = Species.objects.filter(chemical_formula__exact=self.chemical_formula).filter(mass__exact=self.mass).filter(isotope__exact=self.isotope) #exclude this very instance sp_search = sp_search.exclude(id__exact=self.id) if len(sp_search) > 0: #indeed we do #in this case they should be isomeres and therefore have a different inchi if self.inchi in sp_search.values_list('inchi'): raise ValidationError(u'A species with this chemical formula and InChI already exists in the database') if self.inchi == '': raise ValidationError(u'Isomeres need to be distinguished via their InChI')
def clean(self): # we have to run this validator here again, because # chemicalformula2nominalmass expects proper formulae # django runs the validators _after_ this clean function validate_chemical_formula(self.chemical_formula) if self.isotope is True: if self.mass != chemicalformula2nominalmass(self.chemical_formula): raise ValidationError( u'Nominal mass and chemical formula are not compatible.') #check if either inchi or inchikey are there and either complete the other one or verify if self.inchi == '': if self.inchikey != '': tmpinchi = inchikey2inchi(self.inchikey) if tmpinchi: self.inchi = tmpinchi else: raise ValidationError( u'No chemical compound found for this InChI-Key.') else: #check if the given inchi has the InChI= in the beginning #additionally check for Standard-InChI if not self.inchi.startswith('InChI='): self.inchi = 'InChI=' + self.inchi if not self.inchi.startswith('InChI=1S'): raise ValidationError( u'InChI %s is not a Standard-InChI (starts with 1S)' % self.inchi) #get the rest if self.inchikey != '': inchikeycheck = inchi2inchikey(self.inchi) if inchikeycheck != self.inchikey: raise ValidationError( u'The given InChI and InChI-Key are not compatible.') else: tmpinchikey = inchi2inchikey(self.inchi) if tmpinchikey: self.inchikey = tmpinchikey else: raise ValidationError(u'Not a valid InChI-Key.') if self.chemical_formula != inchi2chemicalformula(self.inchi): raise ValidationError( u'InChI %s is not compatible with the stochiometric formula %s.' % (self.inchi, self.chemical_formula))
def clean(self): # we have to run this validator here again, because # chemicalformula2nominalmass expects proper formulae # django runs the validators _after_ this clean function validate_chemical_formula(self.chemical_formula) if self.isotope is True: if self.mass != chemicalformula2nominalmass(self.chemical_formula): raise ValidationError(u'Nominal mass and chemical formula are not compatible.') #check if either inchi or inchikey are there and either complete the other one or verify if self.inchi == '': if self.inchikey != '': tmpinchi = inchikey2inchi(self.inchikey) if tmpinchi: self.inchi = tmpinchi else: raise ValidationError(u'No chemical compound found for this InChI-Key.') else: #check if the given inchi has the InChI= in the beginning #additionally check for Standard-InChI if not self.inchi.startswith('InChI='): self.inchi = 'InChI=' + self.inchi if not self.inchi.startswith('InChI=1S'): raise ValidationError(u'InChI %s is not a Standard-InChI (starts with 1S)' % self.inchi) #get the rest if self.inchikey != '': inchikeycheck = inchi2inchikey(self.inchi) if inchikeycheck != self.inchikey: raise ValidationError(u'The given InChI and InChI-Key are not compatible.') else: tmpinchikey = inchi2inchikey(self.inchi) if tmpinchikey: self.inchikey = tmpinchikey else: raise ValidationError(u'Not a valid InChI-Key.') if self.chemical_formula != inchi2chemicalformula(self.inchi): raise ValidationError(u'InChI %s is not compatible with the stochiometric formula %s.' % (self.inchi, self.chemical_formula))
def clean(self): # we have to run this validator here again, because # chemicalformula2nominalmass expects proper formulae # django runs the validators _after_ this clean function validate_chemical_formula(self.chemical_formula) if self.isotope is True: if self.mass != chemicalformula2nominalmass(self.chemical_formula): raise ValidationError( u'Nominal mass and chemical formula are not compatible.') #check if either inchi or inchikey are there and either complete the other one or verify if self.inchi == '': if self.inchikey != '': tmpinchi = inchikey2inchi(self.inchikey) if tmpinchi: self.inchi = tmpinchi else: raise ValidationError( u'No chemical compound found for this InChI-Key.') else: #check if the given inchi has the InChI= in the beginning #additionally check for Standard-InChI if not self.inchi.startswith('InChI='): self.inchi = 'InChI=' + self.inchi if not self.inchi.startswith('InChI=1S'): raise ValidationError( u'InChI %s is not a Standard-InChI (starts with 1S)' % self.inchi) #get the rest if self.inchikey != '': inchikeycheck = inchi2inchikey(self.inchi) if inchikeycheck != self.inchikey: raise ValidationError( u'The given InChI and InChI-Key are not compatible.') else: tmpinchikey = inchi2inchikey(self.inchi) if tmpinchikey: self.inchikey = tmpinchikey else: raise ValidationError(u'Not a valid InChI-Key.') if self.chemical_formula != inchi2chemicalformula(self.inchi): raise ValidationError( u'InChI %s is not compatible with the stochiometric formula %s.' % (self.inchi, self.chemical_formula)) #we check if we already have a species with same chem formula, mass and isotope-status sp_search = Species.objects.filter( chemical_formula__exact=self.chemical_formula).filter( mass__exact=self.mass).filter(isotope__exact=self.isotope) #exclude this very instance sp_search = sp_search.exclude(id__exact=self.id) if len(sp_search) > 0: #indeed we do #in this case they should be isomeres and therefore have a different inchi if self.inchi in sp_search.values_list('inchi'): raise ValidationError( u'A species with this chemical formula and InChI already exists in the database' ) if self.inchi == '': raise ValidationError( u'Isomeres need to be distinguished via their InChI')