Skip to content

Commit

Permalink
#1640 add cdx support (#1650)
Browse files Browse the repository at this point in the history
* add cdx format to types

* add check for cdx format

* add magic word for cdx for recognizing on server

* delete magik number

* fix format check

* update indigo to 1.7.3

* fix format check

* clear string from unnecessary info

* clear string from \n symbols from clipboard

* update indigo version

Co-authored-by: Nikita_Vozisov <Nikita_Vozisov@epam.com>
  • Loading branch information
ZaimBeshich1 and Nitvex authored Dec 16, 2022
1 parent 3c4d31b commit 10ce268
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ const formatProperties: FormatPropertiesMap = {
ChemicalMimeType.CDXML,
['.cdxml'],
true
),
cdx: new SupportedFormatProperties(
'Base64 CDX',
ChemicalMimeType.CDX,
['.cdx'],
true
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export class FormatterFactory {
case SupportedFormat.smilesExt:
case SupportedFormat.smarts:
case SupportedFormat.cdxml:
case SupportedFormat.cdx:
default:
formatter = new ServerFormatter(
this.#structService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,32 @@ export function identifyStructFormat(
return SupportedFormat.mol
}
}

if (
sanitizedString[0] === '<' &&
sanitizedString.indexOf('<molecule') !== -1
) {
return SupportedFormat.cml
}

const clearStr = sanitizedString.replace(/\s/g, '')
const anyLetterAnyDigitContainsSlashesEndsWithEqualSign =
/^[a-zA-Z0-9+/]*={0,2}$/
if (
anyLetterAnyDigitContainsSlashesEndsWithEqualSign.test(clearStr) &&
clearStr.length % 4 === 0
) {
return SupportedFormat.cdx
}

if (sanitizedString.slice(0, 5) === 'InChI') {
return SupportedFormat.inChI
}

if (sanitizedString.indexOf('\n') === -1) {
if (
sanitizedString.indexOf('\n') === -1 &&
sanitizedString === sanitizedString.toUpperCase()
) {
// TODO: smiles regexp
return SupportedFormat.smiles
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export enum SupportedFormat {
inChIAuxInfo = 'inChIAuxInfo',
cml = 'cml',
ket = 'ket',
cdxml = 'cdxml'
cdxml = 'cdxml',
cdx = 'cdx'
}

export type FormatterFactoryOptions = Partial<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export enum ChemicalMimeType {
DaylightSmarts = 'chemical/x-daylight-smarts',
InChI = 'chemical/x-inchi',
InChIAuxInfo = 'chemical/x-inchi-aux',
CDX = 'chemical/x-cdx',
CDXML = 'chemical/x-cdxml',
CML = 'chemical/x-cml',
KET = 'chemical/x-indigo-ket'
Expand Down
12 changes: 10 additions & 2 deletions packages/ketcher-react/src/script/ui/state/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
SGroup,
getStereoAtomsMap,
identifyStructFormat,
Struct
Struct,
SupportedFormat
} from 'ketcher-core'

import { supportedSGroupTypes } from './constants'
Expand Down Expand Up @@ -51,12 +52,19 @@ export function loadStruct(struct) {
}
}

function parseStruct(struct: Struct, server, options?): Promise<Struct> {
function parseStruct(
struct: string | Struct,
server,
options?
): Promise<Struct> {
if (typeof struct === 'string') {
options = options || {}
const { rescale, fragment, ...formatterOptions } = options

const format = identifyStructFormat(struct)
if (format === SupportedFormat.cdx) {
struct = `base64::${struct.replace(/\s/g, '')}`
}
const factory = new FormatterFactory(server)

const service = factory.create(format, formatterOptions)
Expand Down
6 changes: 3 additions & 3 deletions packages/ketcher-react/src/script/ui/utils/fileOpener.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ export function fileOpener(server) {
}

function throughFileReader(file) {
const isCDX = file.name.endsWith('cdx')
return new Promise((resolve, reject) => {
const rd = new FileReader() // eslint-disable-line no-undef

rd.onload = () => {
const content = rd.result
const content = isCDX ? rd.result.slice(37) : rd.result
if (file.msClose) file.msClose()
resolve(content)
}

rd.onerror = (event) => {
reject(event)
}

rd.readAsText(file, 'UTF-8')
isCDX ? rd.readAsDataURL(file) : rd.readAsText(file, 'UTF-8')
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class SaveDialog extends Component {
'svg',
'png',
'cdxml'
// 'cdx' TO DO: Uncomment, when export will be ready on Indigo side
)

this.saveSchema = saveSchema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export enum SupportedFormat {
InChI = 'inchi',
InChIAuxInfo = 'inchi-aux',
Ket = 'ket',
CDX = 'cdx',
CDXML = 'cdxml'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ function convertMimeTypeToOutputFormat(
format = SupportedFormat.CDXML
break
}
case ChemicalMimeType.CDX: {
format = SupportedFormat.CDX
break
}
default: {
throw new Error('Unsupported chemical mime type')
}
Expand Down

0 comments on commit 10ce268

Please sign in to comment.