Skip to content

Commit

Permalink
Fix filtering + sorting logic. Perfect respect for flagged chemicals
Browse files Browse the repository at this point in the history
  • Loading branch information
KastanDay committed Jul 22, 2024
1 parent c9c93ba commit 3cb4256
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 52 deletions.
10 changes: 5 additions & 5 deletions src/app/components/chemscraper/results/results.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ <h3>
<div class="sort-text">
{{molecules.length}} results
<span *ngIf="similaritySortSMILE || countActiveFilters"> |
{{getNumberOfValidMoleculesAfterFilters(molecules, similaritySortSMILE)}}
{{moleculesToDisplay.length}}
exact matches
</span>
</div>
</div>
<div class="table-container">
<p-table #resultsTable [value]="molecules" dataKey="id" selectionMode="single"
<p-table #resultsTable [value]="moleculesToDisplay" dataKey="id" selectionMode="single"
[(selection)]="selectedMolecule" (onRowSelect)="onRowSelected($event)"
(onRowUnselect)="onRowUnselected($event)" [paginator]="true" [rows]="10"
[rowsPerPageOptions]="[5, 10, 25]" [scrollable]="true" scrollHeight="700px" styleClass="molecule_table"
Expand All @@ -158,8 +158,8 @@ <h3>
<tr>
<!-- <th><p-tableCheckbox></p-tableCheckbox></th> -->
<th pSortableColumn="name">Molecule<p-sortIcon field="name"></p-sortIcon></th>
<th>Extracted Structure</th>
<th>Original Structure</th>
<th class="extracted-structure-column">Extracted Structure</th>
<th class="extracted-structure-column">Original Structure</th>
<th
pTooltip="SMILES strings correspond to extracted structures and may contain asterisks (*), which represent an abbreviation that ChemScraper is currently unable to expand."
tooltipPosition="top" pSortableColumn="SMILE"><span
Expand Down Expand Up @@ -343,4 +343,4 @@ <h3>Right Sidebar</h3>
styleClass="p-button-primary"></p-button>
</ng-template>
</p-dialog>
</div>
</div>
94 changes: 47 additions & 47 deletions src/app/components/chemscraper/results/results.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export class ResultsComponent {
splitView: boolean = true;
highlightBoxes: HighlightBox[][];
searchText: string;
molecules: any[];
molecules: any[]; // all received from backend
moleculesToDisplay: any[]; // the ones shown to the user, after search + filtering.
filterPanelVisible: boolean = false;

tableFilterStateOptions: any[] = [{ label: 'Off', value: 'off' }, { label: 'On', value: 'on' }];
Expand Down Expand Up @@ -152,6 +153,7 @@ export class ResultsComponent {
this.fileNames = [];

this.molecules = [];
this.moleculesToDisplay = [];

// Temp file read function
// this.process_example_file();
Expand Down Expand Up @@ -180,6 +182,7 @@ export class ResultsComponent {
this.selectedSortOption = "Location In PDF";
this.sortData("Location In PDF")
this.similaritySortSMILE = "";
this.similaritySort(value)
return;
}
this.selectedSortOption = "Similarity";
Expand Down Expand Up @@ -223,18 +226,6 @@ export class ResultsComponent {

}

copySmilesToClipboard() {
navigator.clipboard.writeText(this.marvinJsSmiles).then(() => {
this.copySuccess = true;
setTimeout(() => {
this.copySuccess = false;
}, 5000);

}, () => {
alert('Failure: Unable to copy SMILES string to clipboard.');
});
}

copyAndPasteURL(): void {
const selBox = document.createElement('textarea');
selBox.style.position = 'fixed';
Expand Down Expand Up @@ -273,9 +264,10 @@ export class ResultsComponent {
if (jobID)
this._chemScraperService.getResult(jobID).subscribe(
(data) => {
console.log("In getResult.subscribe()", data);
// console.log("In getResult.subscribe()", data);
// this.showMessage('info', 'Info', `Got results for job ${jobID}. Data: ${data}`);
this.molecules = data;
this.moleculesToDisplay = data;
this.pages_count = Math.max(...this.molecules.map(molecule => parseInt(molecule.page_no)))

this.updateStatusStage(2);
Expand All @@ -299,6 +291,7 @@ export class ResultsComponent {
);
},
(error) => {
// TODO: Update UI, not just toast.
console.error(`Failed to fetch results for job ${jobID}. Error: ${error}`);
this.showMessage('error', 'Error', `Failed to fetch results for job ${jobID}. Error: ${error}`);
}
Expand Down Expand Up @@ -449,11 +442,6 @@ export class ResultsComponent {
this.showMarvinJsEditor = false;
}

// TODO: replace longest common substring with: https://github.com/moleculemaker/mmli-backend/blob/34f2568b138524f17385426fc53da84f3e24df97/app/routers/chemscraper.py#L58
getNumberOfValidMoleculesAfterFilters(molecules: Molecule[], smiles: string): number {
return this.filterBySmiles(molecules, smiles).length;
}

filterBySmiles(molecules: Molecule[], smiles: string): Molecule[] {
return molecules.filter(molecule => {
// Account for filter options
Expand Down Expand Up @@ -514,42 +502,52 @@ export class ResultsComponent {
});
}

similaritySort(smile: string) {
if (smile == '') {
return this.molecules;
similaritySort(smile: string): Molecule[] {
// ⭐️ MAIN SORT FUNCTION
console.log("In similarity sort with SMILE:", smile);

if (smile === '') {
this.moleculesToDisplay = this.filterBySmiles(this.molecules, smile);
}

this._marvinJsSmiles = smile;
this.similaritySortSMILE = smile;
this.updateSimilaritySortDisabledState();
// console.log("Similarity sort: " + smile, "Ascending: " + this.isAscending, "JobID: " + this.jobID);
if (this.jobID) {
this._chemScraperService.getSimilaritySortedOrder(this.jobID, smile).subscribe({
next: (response) => {
console.log("Similarity sort response: ", response);
// this.showMessage('Success', 'Similarity Sort results', `${response}`);
this.molecules.sort((data1: Molecule, data2: Molecule) => {
const indexA = response.indexOf(data1.id);
const indexB = response.indexOf(data2.id);
if (this.isAscending) {
return indexA - indexB;
}
return indexB - indexA;
});
this.goToRow(0);

// Collapse all rows
this.resultsTable.expandedRowKeys = {};
},
error: (error) => {
console.debug("No similarity match found. Err: ", error);
this.showMessage('info', `No exact matches found for '${smile}'`, 'The molecules are sorted by similarity, but no exact matches found for SMILE: ' + smile, 4000);
}
});

if (!this.jobID) {
this.moleculesToDisplay = [];
return this.moleculesToDisplay;
}
return this.molecules;

this._chemScraperService.getSimilaritySortedOrder(this.jobID, smile).subscribe({
next: (response) => {
// this.sortMoleculesBySimilarity(response);
this.molecules.sort((data1: Molecule, data2: Molecule) => {
const indexA = response.indexOf(data1.id);
const indexB = response.indexOf(data2.id);
if (this.isAscending) {
return indexA - indexB;
}
return indexB - indexA;
});

this.moleculesToDisplay = this.filterBySmiles(this.molecules, smile);
this.goToRow(0);
this.resultsTable.expandedRowKeys = {};
},
error: (error) => {
console.debug("No similarity match found. Err: ", error);
this.showMessage('info', `No exact matches found for '${smile}'`,
'The molecules are sorted by similarity, but no exact matches found for SMILE: ' + smile, 4000);
this.moleculesToDisplay = [];
}
});

return this.moleculesToDisplay;
}



searchStructure() {
this.similaritySort(this.marvinJsSmiles);
this.showMarvinJsEditor = false;
Expand Down Expand Up @@ -580,6 +578,8 @@ export class ResultsComponent {
countActiveFilters++;
}
this.countActiveFilters = countActiveFilters;

this.similaritySort(this.similaritySortSMILE);
}


Expand Down

0 comments on commit 3cb4256

Please sign in to comment.