Task tutorial

Flag duplicate structures in a SMILES CSV

Keep every source row, write chematic's canonical SMILES beside it, and flag duplicate candidates without sending the file to a server.

You will make

A row-preserving CSV report with the original SMILES, a local canonical form, and explicit unique, duplicate, or invalid status.

Steps

  1. Create molecules.csv with a smiles header and a few input rows.
  2. Run the standard-library Python script below after installing chematic.
  3. Review molecule-report.csv before removing or merging any rows.
import csv
import chematic

seen = {}
with open("molecules.csv", newline="") as src, open("molecule-report.csv", "w", newline="") as out:
    reader = csv.DictReader(src)
    fields = ["source_row", "smiles", "canonical_smiles", "status"]
    writer = csv.DictWriter(out, fieldnames=fields)
    writer.writeheader()
    for source_row, row in enumerate(reader, start=2):
        smiles = row["smiles"].strip()
        try:
            canonical = chematic.from_smiles(smiles).smiles
            status = f"duplicate_of_row_{seen[canonical]}" if canonical in seen else "unique"
            seen.setdefault(canonical, source_row)
        except ValueError:
            canonical, status = "", "invalid_smiles"
        writer.writerow({"source_row": source_row, "smiles": smiles,
                         "canonical_smiles": canonical, "status": status})

Expected result

For CCO and OCC in separate rows, both rows receive C(C)O as canonical_smiles; the later row is marked duplicate_of_row_2. An invalid SMILES stays in the report as invalid_smiles.

If it does not work

A shared canonical SMILES is a candidate for duplicate review, not proof that records have the same salt form, stereochemistry, provenance, or intended identity. Retain the original row and package version, and validate the standardization policy before deleting data.

Next action

Read the Python workflow guide