def validate_inchikey_computable(molfile: str) -> None: """Validates that an InChIKey can be computed from the provided molfile. Args: molfile: The molfile string Raises: ValidationError: If the InChIKey cannot be computed. """ try: get_inchikey(molfile) except IndigoException: raise ValidationError( "InChIKey not computable for provided structure.")
def to_internal_value(self, data): matched_fields = set(self.alt_structures) & set( self.initial_data.keys()) formatted_fields = [ format_value(f) for f in sorted(self.alt_structures) ] formatted_matched_fields = [ format_value(f) for f in sorted(matched_fields) ] if not matched_fields: raise ValidationError({ "non_field_errors": f"One of {sorted(formatted_fields)} required." }) if len(matched_fields) > 1: raise ValidationError({ "non_field_errors": (f"Only one of {formatted_fields} allowed. " f"Recieved {formatted_matched_fields}.") }) data = super().to_internal_value(data) # calls field validators structure = next(k for k in self.alt_structures if k in data) data["molfile_v3000"] = get_molfile_v3000(data.pop(structure)) if "inchikey" not in data: data["inchikey"] = get_inchikey(data["molfile_v3000"]) return data
def filter_smiles(self, queryset, name, value): validate_smiles(value) validate_inchikey_computable(value) # There is an inconsistency w/ inchikey conversion, discussion below # https://github.com/Chemical-Curation/chemcurator_django/pull/224#issuecomment-675129692 value = get_molfile_v3000(value) inchikey = get_inchikey(value) return queryset.filter(inchikey=inchikey)
def filter_molfile_v2000(self, queryset, name, value): validate_molfile_v2000(value) validate_inchikey_computable(value) inchikey = get_inchikey(value) return queryset.filter(inchikey=inchikey)
def get_calculated_inchikey(self, obj): return get_inchikey(obj.molfile_v3000)
def _inchikey(self): """Computes the inchikey from the molfile.""" try: return get_inchikey(self.molfile_v3000) except IndigoException: return None