Skip to content

Commit

Permalink
fixing ketcher issues for hf2
Browse files Browse the repository at this point in the history
  • Loading branch information
NikoAnderson committed Aug 7, 2024
1 parent 7e3e8a6 commit ca8b7bf
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ export class EditorImplementation implements Editor {

getSmiles(): Observable<string> {
if (this.ketcher != null) {
return from(this.ketcher.getSmiles()).pipe(switchMap(data => {
return data;}));
return from(this.ketcher.getSmiles());
} else if (this.jsdraw != null) {
return new Observable<string>(observer => {
observer.next(this.jsdraw.getSmiles());
Expand Down Expand Up @@ -143,6 +142,8 @@ export class EditorImplementation implements Editor {

setMolecule(molfile: string): void {
if (this.ketcher && this.ketcher != null) {
console.log('setting ketcher');
this.ketcher.setMolecule(molfile);
this.ketcher.setMolecule(molfile);
} else if (this.jsdraw && this.jsdraw != null) {
// from simple tests, this should push the current molecule down
Expand Down
66 changes: 54 additions & 12 deletions src/app/core/structure-editor/structure-editor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class StructureEditorComponent implements OnInit, AfterViewInit, OnDestro
window.removeEventListener('drop', this.preventDrag);
window.removeEventListener('dragover', this.preventDrag);
window.removeEventListener('paste', this.checkPaste);
(<HTMLCanvasElement>this.myCanvas.nativeElement).removeEventListener('click', this.click);
(<HTMLCanvasElement>this.myCanvas.nativeElement).removeEventListener('click', this.listener);
delete this.ketcher;
let parentElement = document.getElementById('ketcherwrapper');
let childElement = document.getElementById('root');
Expand Down Expand Up @@ -118,14 +118,14 @@ export class StructureEditorComponent implements OnInit, AfterViewInit, OnDestro
}

listener = () => {
/* var elmR=document.getElementById("root");
var elmR=document.getElementById("root");
if(this.structureEditor==="ketcher"){
if( elmR && elmR.querySelector(":focus-within")){
this.getSketcher().activated=true;
}else{
this.getSketcher().activated=false;
}
}*/
}
}

private preventDrag = (event: DragEvent) => {
Expand All @@ -135,8 +135,8 @@ export class StructureEditorComponent implements OnInit, AfterViewInit, OnDestro

// override JSDraw for Molvec paste event. Using the JSDraw menu copy function seems to ignore this at first
checkPaste = (event: ClipboardEvent ) => {

if ((this.jsdraw || this.ketcher ) && this.getSketcher().activated) {
console.log('check paste');
if ((this.jsdraw || this.ketcher )&& this.getSketcher().activated) {
event.preventDefault();
event.stopPropagation();
event.stopImmediatePropagation();
Expand All @@ -156,7 +156,7 @@ export class StructureEditorComponent implements OnInit, AfterViewInit, OnDestro

ngOnInit() {
// window.addEventListener('keyup',this.listener);
// window.addEventListener('click',this.listener);
window.addEventListener('click',this.listener);
this.overlayContainer = this.overlayContainerService.getContainerElement();


Expand Down Expand Up @@ -346,7 +346,6 @@ export class StructureEditorComponent implements OnInit, AfterViewInit, OnDestro
this.ketcher.getMolfile().then(result => {
let mfile = [null];
mfile[0]= result;
// this.getSketcher().setFile(mfile[0], "mol");
this.getSketcher().setFile(mfile[0], "mol");
})
}
Expand Down Expand Up @@ -398,13 +397,38 @@ export class StructureEditorComponent implements OnInit, AfterViewInit, OnDestro
sendToMolvec(img: string) {
this.canvasMessage = '';
this.loadingService.setLoading(true);
console.log('sending to molvec');
console.log(img);
this.structureService.molvec(img).subscribe(response => {
const mol = response.molfile;
if (this.ketcher) {
// this.ketcher.setMolecule(mol);
console.log(response);
if (this.ketcher && this.structureEditor === 'ketcher') {
this.ketcher.setMolecule(mol);
setTimeout(() => {
this.editor.setMolecule(mol);
}, 100);
this.loadedMolfile.emit(mol);

this.loadingService.setLoading(false);
this.structureService.molvec(img).subscribe(resp => {
setTimeout(() => {
this.editor.setMolecule(resp.molfile);
this.ketcher.setMolecule(mol);
}, 100);
}, error => {
this.canvasMessage = 'Structure not detectable';
this.loadingService.setLoading(false);
});

}
this.loadedMolfile.emit(mol);
this.loadingService.setLoading(false);
else {
console.log('not ketcher');
this.jsdraw.setMolfile(mol);
this.loadingService.setLoading(false);
this.loadedMolfile.emit(mol);

}

}, error => {
this.canvasMessage = 'Structure not detectable';
this.loadingService.setLoading(false);
Expand Down Expand Up @@ -450,6 +474,7 @@ export class StructureEditorComponent implements OnInit, AfterViewInit, OnDestro
if (items[i].type.indexOf('image') !== -1) {
event.preventDefault();
event.stopPropagation();
this.canvasMessage = '';
valid = true;
send.type = 'image';
const reader = new FileReader();
Expand All @@ -465,13 +490,30 @@ export class StructureEditorComponent implements OnInit, AfterViewInit, OnDestro
} else if (items[i].type === 'text/plain') {
const text = event.clipboardData.getData('text/plain');
if (text.indexOf('<div') === -1) {
console.log('plaintext');
event.preventDefault();
event.stopPropagation();
this.canvasMessage = '';
this.loadingService.setLoading(true);
this.structureService.interpretStructure(text).subscribe(response => {

if (response.structure && response.structure.molfile) {
this.ketcher.setMolecule(response.structure.molfile);

this.editor.setMolecule(response.structure.molfile);

this.loadedMolfile.emit(response.structure.molfile);

if(response.structure.smiles === '') {
this.canvasMessage = 'empty or invalid structure pasted';
}
} else {
this.canvasMessage = 'Structure text not recognized';
}
this.loadingService.setLoading(false);

},error =>{
this.loadingService.setLoading(false);
this.canvasMessage = 'empty or invalid structure pasted';
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { SubstanceFormStructuralUnitsService } from '../structural-units/substan
import { SubstanceFormStructureService } from './substance-form-structure.service';
import { ActivatedRoute, NavigationExtras, Router } from '@angular/router';
import { StructureEditorComponent } from '@gsrs-core/structure-editor';
import { take } from 'rxjs/operators';
import { ConfigService } from '@gsrs-core/config';

@Component({
selector: 'app-substance-form-structure-card',
Expand Down Expand Up @@ -49,7 +51,8 @@ export class SubstanceFormStructureCardComponent extends SubstanceFormBase imple
private substanceService: SubstanceService,
private substanceFormStructuralUnitsService: SubstanceFormStructuralUnitsService,
private activatedRoute: ActivatedRoute,
private router: Router
private router: Router,
private configService: ConfigService
) {
super();
}
Expand Down Expand Up @@ -155,6 +158,7 @@ export class SubstanceFormStructureCardComponent extends SubstanceFormBase imple
this.structureService.interpretStructure(molfile).subscribe(response => {
this.processStructurePostResponse(response);
this.structure.molfile = molfile;
this.smiles = response.structure.smiles;
});
}
}
Expand Down Expand Up @@ -225,13 +229,13 @@ export class SubstanceFormStructureCardComponent extends SubstanceFormBase imple
}

openStructureExportDialog(): void {

this.structureEditor.getSmiles().pipe(take(1)).subscribe(resp => {
const dialogRef = this.dialog.open(StructureExportComponent, {
height: 'auto',
width: '650px',
data: {
molfile: this.mol,
smiles: this.smiles,
smiles: resp,
type: this.substanceType
}
});
Expand All @@ -242,6 +246,7 @@ export class SubstanceFormStructureCardComponent extends SubstanceFormBase imple
}, () => {
this.overlayContainer.style.zIndex = null;
});
});
}

openNameResolverDialog(): void {
Expand Down Expand Up @@ -303,16 +308,20 @@ export class SubstanceFormStructureCardComponent extends SubstanceFormBase imple
structure: response.structure.id
}
};
if (this.configService.configData && this.configService.configData.gsrsHomeBaseUrl) {
console.log(this.configService.configData.gsrsHomeBaseUrl);
let url = this.configService.configData.gsrsHomeBaseUrl + '/structure-search?structure=' + response.structure.id;
window.open(url, '_blank');
} else {

const url = this.router.serializeUrl(
this.router.createUrlTree(['/structure-search/'], {
queryParams: navigationExtras.queryParams})
);

this.loadingService.setLoading(false);
window.open(url, '_blank');


}
const urlTree = this.router.createUrlTree(['/structure-search/'], {
queryParams: navigationExtras.queryParams,
queryParamsHandling: 'merge',
preserveFragment: true
});
console.log(urlTree);
window.open(urlTree.toString(), '_blank');
}, error => {
this.loadingService.setLoading(false);
});
Expand Down

0 comments on commit ca8b7bf

Please sign in to comment.