File size: 2,261 Bytes
27dc10f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
import sys

from rdkit import Chem

# --- Configuration ---
input_cxsmiles_file = "2024.07_Enamine_REAL_HAC_25_1B_CXSMILES.cxsmiles"  # <-- CHANGE THIS to your input filename
output_smiles_file = "enamine_smiles_1B.txt"     # <-- CHANGE THIS to your desired output filename
# --- End Configuration ---

# --- Script Start ---

# Check if input file exists
if not os.path.isfile(input_cxsmiles_file):
    print(f"ERROR: Input file not found: {input_cxsmiles_file}")
    sys.exit(1)

print(f"Reading CXSMILES from: {input_cxsmiles_file}")
print(f"Writing standard SMILES to: {output_smiles_file}")
print("-" * 30)

count_success = 0
count_error = 0

# Open input and output files safely
try:
    with open(input_cxsmiles_file, 'r') as infile, open(output_smiles_file, 'w') as outfile:
        # Process each line in the input file
        for i, line in enumerate(infile):
            cxsmiles_line = line.strip() # Remove leading/trailing whitespace

            if not cxsmiles_line: # Skip empty lines
                continue

            try:
                # RDKit's MolFromSmiles often ignores CXSMILES extensions
                # It reads the core structure.
                mol = Chem.MolFromSmiles(cxsmiles_line)

                if mol is not None:
                    # Convert the RDKit molecule back to a standard, canonical SMILES
                    standard_smiles = Chem.MolToSmiles(mol)
                    outfile.write(standard_smiles + '\n')
                    count_success += 1
                else:
                    # RDKit couldn't parse this line
                    print(f"Warning: Could not parse line {i+1}. Input: '{cxsmiles_line}'")
                    count_error += 1

            except Exception as e:
                # Catch any other unexpected errors during RDKit processing
                print(f"Error processing line {i+1}: '{cxsmiles_line}'. Details: {e}")
                count_error += 1

except IOError as e:
    print(f"ERROR: Could not open or write file. Details: {e}")
    sys.exit(1)

print("-" * 30)
print(f"Processing finished.")
print(f"Successfully converted: {count_success} lines.")
print(f"Failed/Skipped:       {count_error} lines.")
print(f"Output written to:    {output_smiles_file}")