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

#1852 Implements automatic selection of mol encoding #1870

Merged
merged 8 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -23,6 +23,14 @@ type FormatPropertiesMap = {
}

const formatProperties: FormatPropertiesMap = {
molAuto: new SupportedFormatProperties(
// TODO: is it a valid name?
'MDL Molfile Auto Format detect',
ChemicalMimeType.Mol,
['.mol'],
true,
{ 'molfile-saving-mode': 'auto' }
),
mol: new SupportedFormatProperties(
'MDL Molfile V2000',
ChemicalMimeType.Mol,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,29 @@ export class FormatterFactory {

let formatter: StructFormatter
switch (format) {
case 'ket':
case SupportedFormat.ket:
formatter = new KetFormatter(new KetSerializer())
break

case 'rxn':
case SupportedFormat.rxn:
formatter = new RxnFormatter(new MolSerializer(molSerializerOptions))
break

case 'mol':
case SupportedFormat.mol:
formatter = new MolfileV2000Formatter(
new MolSerializer(molSerializerOptions)
)
break

case 'cml':
case 'inChIAuxInfo':
case 'inChI':
case 'molV3000':
case 'smiles':
case 'rxnV3000':
case 'smilesExt':
case 'smarts':
case 'cdxml':
case SupportedFormat.cml:
case SupportedFormat.inChIAuxInfo:
case SupportedFormat.inChI:
case SupportedFormat.molV3000:
case SupportedFormat.smiles:
case SupportedFormat.rxnV3000:
case SupportedFormat.smilesExt:
case SupportedFormat.smarts:
case SupportedFormat.cdxml:
default:
formatter = new ServerFormatter(
this.#structService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ export function identifyStructFormat(

try {
if (JSON.parse(sanitizedString)) {
return 'ket'
return SupportedFormat.ket
}
} catch (er) {} // eslint-disable-line

if (sanitizedString.indexOf('$RXN') !== -1) {
return 'rxn'
return SupportedFormat.rxn
}

if (sanitizedString.indexOf('V3000') !== -1) {
return 'molV3000'
return SupportedFormat.molV3000
}

const match = sanitizedString.match(/^(M {2}END|\$END MOL)$/m)
Expand All @@ -44,28 +44,28 @@ export function identifyStructFormat(
end === sanitizedString.length ||
sanitizedString.slice(end, end + 20).search(/^\$(MOL|END CTAB)$/m) !== -1
) {
return 'mol'
return SupportedFormat.mol
}
}
if (
sanitizedString[0] === '<' &&
sanitizedString.indexOf('<molecule') !== -1
) {
return 'cml'
return SupportedFormat.cml
}

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

if (sanitizedString.indexOf('\n') === -1) {
// TODO: smiles regexp
return 'smiles'
return SupportedFormat.smiles
}

if (sanitizedString.indexOf('<CDXML') !== -1) {
return 'cdxml'
return SupportedFormat.cdxml
}
// Molfile by default as Indigo does
return 'mol'
return SupportedFormat.mol
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class ServerFormatter implements StructFormatter {

const data: ConvertData | LayoutData = {
struct: undefined as any,
output_format: getPropertiesByFormat('ket').mime
output_format: getPropertiesByFormat(SupportedFormat.ket).mime
}

const withCoords = getPropertiesByFormat(this.#format).supportsCoords
Expand All @@ -115,8 +115,10 @@ export class ServerFormatter implements StructFormatter {

const formatError =
this.#format === 'smiles'
? `${getPropertiesByFormat('smilesExt').name} and opening of ${
getPropertiesByFormat('smiles').name
? `${
getPropertiesByFormat(SupportedFormat.smilesExt).name
} and opening of ${
getPropertiesByFormat(SupportedFormat.smiles).name
}`
: getPropertiesByFormat(this.#format).name

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,21 @@ export interface StructFormatter {
getStructureFromStringAsync: (stringifiedStruct: string) => Promise<Struct>
}

export type SupportedFormat =
| 'rxn'
| 'rxnV3000'
| 'mol'
| 'molV3000'
| 'smiles'
| 'smilesExt'
| 'smarts'
| 'inChI'
| 'inChIAuxInfo'
| 'cml'
| 'ket'
| 'cdxml'
export enum SupportedFormat {
mol = 'mol',
molV3000 = 'molV3000',
molAuto = 'molAuto',
rxn = 'rxn',
rxnV3000 = 'rxnV3000',
smiles = 'smiles',
smilesExt = 'smilesExt',
smarts = 'smarts',
inChI = 'inChI',
inChIAuxInfo = 'inChIAuxInfo',
cml = 'cml',
ket = 'ket',
cdxml = 'cdxml'
}

export type FormatterFactoryOptions = Partial<
MolSerializerOptions & StructServiceOptions
Expand Down
40 changes: 28 additions & 12 deletions packages/ketcher-core/src/application/ketcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function parseStruct(
}

function getStructure(
structureFormat: SupportedFormat = 'rxn',
structureFormat = SupportedFormat.rxn,
formatterFactory: FormatterFactory,
struct: Struct
): Promise<string> {
Expand Down Expand Up @@ -133,19 +133,23 @@ export class Ketcher {
}

getSmiles(isExtended = false): Promise<string> {
const format: SupportedFormat = isExtended ? 'smilesExt' : 'smiles'
const format = isExtended
? SupportedFormat.smilesExt
: SupportedFormat.smiles
return getStructure(format, this.#formatterFactory, this.editor.struct())
}

async getMolfile(molfileFormat: MolfileFormat = 'v2000'): Promise<string> {
async getMolfile(molfileFormat?: MolfileFormat): Promise<string> {
if (this.containsReaction()) {
throw Error(
'The structure cannot be saved as *.MOL due to reaction arrrows.'
)
}

const format: SupportedFormat =
molfileFormat === 'v3000' ? 'molV3000' : 'mol'
const formatPassed =
molfileFormat === 'v3000' ? SupportedFormat.molV3000 : SupportedFormat.mol
const format = molfileFormat ? formatPassed : SupportedFormat.molAuto

const molfile = await getStructure(
format,
this.#formatterFactory,
Expand All @@ -161,8 +165,8 @@ export class Ketcher {
'The structure cannot be saved as *.RXN: there is no reaction arrows.'
)
}
const format: SupportedFormat =
molfileFormat === 'v3000' ? 'rxnV3000' : 'rxn'
const format =
molfileFormat === 'v3000' ? SupportedFormat.rxnV3000 : SupportedFormat.rxn
const rxnfile = await getStructure(
format,
this.#formatterFactory,
Expand All @@ -173,28 +177,40 @@ export class Ketcher {
}

getKet(): Promise<string> {
return getStructure('ket', this.#formatterFactory, this.#editor.struct())
return getStructure(
SupportedFormat.ket,
this.#formatterFactory,
this.#editor.struct()
)
}

getSmarts(): Promise<string> {
return getStructure('smarts', this.#formatterFactory, this.#editor.struct())
return getStructure(
SupportedFormat.smarts,
this.#formatterFactory,
this.#editor.struct()
)
}

getCml(): Promise<string> {
return getStructure('cml', this.#formatterFactory, this.#editor.struct())
return getStructure(
SupportedFormat.cml,
this.#formatterFactory,
this.#editor.struct()
)
}

getInchi(withAuxInfo = false): Promise<string> {
return getStructure(
withAuxInfo ? 'inChIAuxInfo' : 'inChI',
withAuxInfo ? SupportedFormat.inChIAuxInfo : SupportedFormat.inChI,
this.#formatterFactory,
this.#editor.struct()
)
}

async generateInchIKey(): Promise<string> {
const struct: string = await getStructure(
'ket',
SupportedFormat.ket,
this.#formatterFactory,
this.#editor.struct()
)
Expand Down
2 changes: 1 addition & 1 deletion packages/ketcher-react/src/script/ui/action/copyAs.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
***************************************************************************/

import { KetSerializer, MolSerializer } from 'ketcher-core'
import { KetSerializer, MolSerializer, SupportedFormat } from 'ketcher-core'

export default function copyAs(type) {
const state = global.currentState
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
***************************************************************************/

import { Dialog } from '../../../../components'
import { FormatterFactory, Struct, StructService } from 'ketcher-core'
import {
FormatterFactory,
Struct,
StructService,
SupportedFormat
} from 'ketcher-core'
import { MIEW_OPTIONS } from '../../../../../data/schema/options-schema'
import classes from './Miew.module.less'
import { connect } from 'react-redux'
Expand Down Expand Up @@ -117,7 +122,7 @@ const MiewDialog = ({
(miew: MiewAsType) => {
miewRef.current = miew
const factory = new FormatterFactory(server)
const service = factory.create('cml')
const service = factory.create('cml' as SupportedFormat)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please also change to SupportedFormat.cml?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


service
.getStructureFromStructAsync(struct)
Expand Down