Skip to content

Commit

Permalink
#4880 – Add changes according new requirements for IDT aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
svvald committed Jul 30, 2024
1 parent 7d6fc36 commit 4e44f14
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 29 deletions.
45 changes: 40 additions & 5 deletions packages/ketcher-core/src/application/editor/data/monomers.ket
Original file line number Diff line number Diff line change
Expand Up @@ -168890,6 +168890,9 @@
"id": "A",
"name": "A",
"class": "RNA",
"idtAliases": {
"base": "rA"
},
"templates": [
{
"$ref": "monomerTemplate-R___Ribose"
Expand All @@ -168907,6 +168910,9 @@
"id": "C",
"name": "C",
"class": "RNA",
"idtAliases": {
"base": "rC"
},
"templates": [
{
"$ref": "monomerTemplate-R___Ribose"
Expand All @@ -168924,6 +168930,9 @@
"id": "G",
"name": "G",
"class": "RNA",
"idtAliases": {
"base": "rG"
},
"templates": [
{
"$ref": "monomerTemplate-R___Ribose"
Expand All @@ -168941,6 +168950,9 @@
"id": "T",
"name": "T",
"class": "RNA",
"idtAliases": {
"base": "rT"
},
"templates": [
{
"$ref": "monomerTemplate-R___Ribose"
Expand All @@ -168958,6 +168970,9 @@
"id": "U",
"name": "U",
"class": "RNA",
"idtAliases": {
"base": "rU"
},
"templates": [
{
"$ref": "monomerTemplate-R___Ribose"
Expand All @@ -168976,7 +168991,12 @@
"name": "MOE(A)P",
"class": "RNA",
"idtAliases": {
"base": "2MOErA"
"base": "2MOErA",
"modifications": {
"endpoint5": "52MOErA",
"internal": "i2MOErA",
"endpoint3": "32MOErA"
}
},
"templates": [
{
Expand All @@ -168996,7 +169016,12 @@
"name": "MOE(5meC)P",
"class": "RNA",
"idtAliases": {
"base": "2MOErC"
"base": "2MOErC",
"modifications": {
"endpoint5": "52MOErC",
"internal": "i2MOErC",
"endpoint3": "32MOErC"
}
},
"templates": [
{
Expand All @@ -169016,7 +169041,12 @@
"name": "MOE(G)P",
"class": "RNA",
"idtAliases": {
"base": "2MOErG"
"base": "2MOErG",
"modifications": {
"endpoint5": "52MOErG",
"internal": "i2MOErG",
"endpoint3": "32MOErG"
}
},
"templates": [
{
Expand All @@ -169036,7 +169066,12 @@
"name": "MOE(T)P",
"class": "RNA",
"idtAliases": {
"base": "2MOErT"
"base": "2MOErT",
"modifications": {
"endpoint5": "52MOErT",
"internal": "i2MOErT",
"endpoint3": "32MOErT"
}
},
"templates": [
{
Expand All @@ -169056,7 +169091,7 @@
"name": "dR(U)P",
"class": "RNA",
"idtAliases": {
"base": "deoxyU"
"base": "U"
},
"templates": [
{
Expand Down
12 changes: 12 additions & 0 deletions packages/ketcher-macromolecules/src/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
import {
createEditor,
destroyEditor,
PresetPosition,
selectEditor,
selectEditorActiveTool,
selectTool,
Expand Down Expand Up @@ -229,6 +230,7 @@ function Editor({ theme, togglerComponent }: EditorProps) {
sequenceNode instanceof Nucleoside;

if (isNucleotideOrNucleoside) {
console.log(sequenceNode.sugar);
const monomers =
sequenceNode instanceof Nucleotide
? [
Expand All @@ -248,11 +250,21 @@ function Editor({ theme, togglerComponent }: EditorProps) {
});
});

let position: PresetPosition;
if (sequenceNode instanceof Nucleoside) {
position = 'chainEnd';
} else if (sequenceNode.sugar.attachmentPointsToBonds.R1 === null) {
position = 'chainStart';
} else {
position = 'chainMiddle';
}

debouncedShowPreview({
preset: {
monomers,
name: existingPreset?.name,
idtAliases: existingPreset?.idtAliases,
position,
},
style: {
left,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,10 @@ export const RnaPresetGroup = ({ presets, duplicatePreset, editPreset }) => {

debouncedShowPreview({
preset: {
monomers,
name: preset.name,
idtAliases: preset.idtAliases,
monomers,
position: 'library',
},
style,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import styled from '@emotion/styled';
export const AttachmentPointsList = styled.div`
flex: 1;
display: flex;
align-items: flex-start;
align-items: center;
flex-wrap: wrap;
gap: 8px;
`;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
import { IKetIdtAliases } from 'ketcher-core';
import { IDTAliasesContainer, IDTAliasesList, IDTTitle } from './styles';

interface IDTAliasesProps {
readonly aliases: IKetIdtAliases;
readonly idtAliasesText: string;
readonly preset?: boolean;
}

const IDTAliases = ({ aliases, preset }: IDTAliasesProps) => {
let idtAliasesText: string;
if (aliases.modifications) {
const modificationsValues = Object.values(aliases.modifications);
if (modificationsValues.length) {
idtAliasesText = modificationsValues.join(', ');
} else {
idtAliasesText = aliases.base;
}
} else {
idtAliasesText = aliases.base;
}

const IDTAliases = ({ idtAliasesText, preset }: IDTAliasesProps) => {
return (
<IDTAliasesContainer preset={preset}>
<IDTTitle>IDT: </IDTTitle>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import styled from '@emotion/styled';

export const IDTAliasesContainer = styled.span<{ preset?: boolean }>`
max-width: ${({ preset }) => (preset ? '100%' : '33%')};
max-width: ${({ preset }) => (preset ? '100%' : '50%')};
font-size: ${(props) => props.theme.ketcher.font.size.regular};
font-weight: 500;
line-height: normal;
Expand All @@ -31,4 +31,5 @@ export const IDTTitle = styled.span`

export const IDTAliasesList = styled.span`
color: #585858;
text-wrap: wrap;
`;
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { useAttachmentPoints } from '../../../hooks/useAttachmentPoints';
import { AttachmentPoints } from 'components/shared/AttachmentPoints/AttachmentPoints';
import { MonomerItemType } from 'ketcher-core';
import { IDTAliases } from 'components/shared/IDTAliases';
import useIDTAliasesTextForMonomer from '../../../hooks/useIDTAliasesTextForMonomer';

const MonomerPreview = ({ className }: IPreviewProps) => {
const preview = useAppSelector(selectShowPreview);
Expand All @@ -55,6 +56,11 @@ const MonomerPreview = ({ className }: IPreviewProps) => {
attachmentPointsToBonds: preview.attachmentPointsToBonds,
});

const idtAliasesText = useIDTAliasesTextForMonomer({
idtAliases,
attachmentPointsToBonds: preview.attachmentPointsToBonds,
});

return (
preview?.monomer && (
<ContainerDynamic
Expand Down Expand Up @@ -82,7 +88,7 @@ const MonomerPreview = ({ className }: IPreviewProps) => {
<AttachmentPoints
preparedAttachmentPointsData={preparedAttachmentPointsData}
/>
{idtAliases && <IDTAliases aliases={idtAliases} />}
{idtAliasesText && <IDTAliases idtAliasesText={idtAliasesText} />}
</InfoBlock>
</ContainerDynamic>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const StyledStructRender = styled(StructRender)`
`;

export const InfoBlock = styled.div`
width: 100%;
display: flex;
gap: 8px;
padding: 0 8px 4px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import styled from '@emotion/styled';
import { useAppSelector } from 'hooks';
import { PresetPreviewState, selectShowPreview } from 'state/common';
import { IconName } from 'ketcher-react/dist/components/Icon/types';
import useIDTAliasesTextForPreset from '../../../hooks/useIDTAliasesTextForPreset';

const icons: Extract<IconName, 'sugar' | 'base' | 'phosphate'>[] = [
'sugar',
Expand All @@ -51,13 +52,22 @@ const PresetPreview = ({ className }: IPreviewProps) => {
transform: ${preview.style.transform || ''};
`;
}, [preview]);
if (!preview || !('preset' in preview)) {
return undefined;
}

const { preset } = preview as PresetPreviewState;
const { idtAliases } = preset;
const [, baseMonomer] = preset.monomers;
const presetName = preset?.name ?? baseMonomer?.props.Name;
const position = preset.position;

const idtAliasesText = useIDTAliasesTextForPreset({
presetName,
position,
idtAliases,
});

if (!preview || !('preset' in preview)) {
return undefined;
}

return (
preview.preset && (
Expand All @@ -77,7 +87,9 @@ const PresetPreview = ({ className }: IPreviewProps) => {
</PresetMonomerRow>
),
)}
{idtAliases && <IDTAliases aliases={idtAliases} preset />}
{idtAliasesText && (
<IDTAliases idtAliasesText={idtAliasesText} preset />
)}
</ContainerDynamic>
)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import {
AttachmentPointName,
IKetIdtAliases,
PolymerBond,
Sugar,
} from 'ketcher-core';
import { useMemo } from 'react';

type Props = {
idtAliases: IKetIdtAliases | undefined;
attachmentPointsToBonds?: Record<AttachmentPointName, PolymerBond | null>;
};

const useIDTAliasesTextForMonomer = ({
idtAliases,
attachmentPointsToBonds,
}: Props) => {
return useMemo(() => {
if (!idtAliases) {
return null;
}

const { base, modifications } = idtAliases;

// For preview on canvas
if (attachmentPointsToBonds) {
if (!modifications) {
return null;
}

const { endpoint5, internal, endpoint3 } = modifications;

const { R1, R2 } = attachmentPointsToBonds;
// Handle phosphate exclusively
if (base === 'Phos') {
if (R1 !== null && R1.firstMonomer instanceof Sugar) {
return null;
}
}

if (R1 !== null && R2 === null) {
return endpoint3 ?? internal;
} else if (R1 === null && R2 !== null) {
return endpoint5 ?? internal;
} else if (R1 !== null && R2 !== null) {
return internal ?? endpoint5 ?? endpoint3;
} else {
return endpoint5 ?? internal ?? endpoint3;
}
}

// For preview in library
if (!modifications) {
return base;
}

const allModificationsHaveSameBase = Object.values(modifications).every(
(modification) => modification.includes(base),
);

if (allModificationsHaveSameBase) {
return base;
}

const baseToPositionsMap: Record<string, string[]> = {};
Object.values(modifications).forEach((modification) => {
const [position, base] = [modification.charAt(0), modification.slice(1)];
baseToPositionsMap[base] = baseToPositionsMap[base]
? [...baseToPositionsMap[base], position]
: [position];
});

return Object.entries(baseToPositionsMap)
.map(([base, positions]) => {
return `(${positions.join(', ')})${base}`;
})
.join(', ');
}, [idtAliases, attachmentPointsToBonds]);
};

export default useIDTAliasesTextForMonomer;
Loading

0 comments on commit 4e44f14

Please sign in to comment.