Пример #1
0
    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')
Пример #2
0
    def clean(self):
        if self.cas != u"":
            validations.validate_CAS(self.cas)

        if self.no_chemspider_sync is False:
            if self.csid is not None:
                chemobj = fitlib.chemlib.ChemicalObject(csid=self.csid)
            else:
                chemobj = fitlib.chemlib.ChemicalObject(
                    name=self.name, inchi=self.inchi, inchikey=self.inchikey, cas=self.cas
                )

            chemobj.complete()

            if (self.inchi != "") and (self.inchi != chemobj.inchi):
                raise ValidationError(
                    "The given InChI and the one retrieved from Chemspider do not match. %s %s"
                    % (self.inchi, chemobj.inchi)
                )

            if (self.inchikey != "") and (self.inchikey != chemobj.inchikey):
                raise ValidationError(
                    "The given InChI-Key and the one retrieved from Chemspider do not match. %s %s"
                    % (self.inchikey, chemobj.inchikey)
                )

            if (self.csid is not None) and (self.csid != chemobj.csid):
                raise ValidationError(
                    "The given CSID and the one retrieved from Chemspider do not match. %s %s"
                    % (str(self.csid), chemobj.csid)
                )

            self.inchi = chemobj.inchi
            self.inchikey = chemobj.inchikey
            self.csid = chemobj.csid

        if self.inchi != "" and self.chemical_formula != "":
            if self.chemical_formula != inchi2chemicalformula(self.inchi):
                raise ValidationError("Chemical Formula does not match InChI")
        elif self.chemical_formula == "" and self.inchi != "":
            self.chemical_formula = inchi2chemicalformula(self.inchi)
Пример #3
0
    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))
Пример #4
0
    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))
Пример #5
0
    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')