Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backmerge: #1640 add cdx support (#1650) #1944

Merged
merged 1 commit into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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