|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# This script can be used for any purpose without limitation subject to the |
| 4 | +# conditions at http://www.ccdc.cam.ac.uk/Community/Pages/Licences/v2.aspx |
| 5 | +# |
| 6 | +# This permission notice and the following statement of attribution must be |
| 7 | +# included in all copies or substantial portions of this script. |
| 8 | +# |
| 9 | + |
| 10 | +from ccdc.io import MoleculeReader |
| 11 | +import os |
| 12 | +import csv |
| 13 | +import argparse |
| 14 | +from utilities import file_list, string_scrubber, read_experimental_csv |
| 15 | + |
| 16 | + |
| 17 | +def read_mol_file(directory, file): |
| 18 | + '''Returns: identifier, smiles''' |
| 19 | + mol_reader = MoleculeReader(os.path.join(directory, file)) |
| 20 | + mol = mol_reader[0] |
| 21 | + |
| 22 | + return mol.identifier, mol.heaviest_component.smiles |
| 23 | + |
| 24 | + |
| 25 | +def main(): |
| 26 | + parser = argparse.ArgumentParser(__doc__) |
| 27 | + parser.add_argument( |
| 28 | + "--input_dir", |
| 29 | + type=str, |
| 30 | + required=True, |
| 31 | + help="Directory containing API folders." |
| 32 | + ) |
| 33 | + parser.add_argument( |
| 34 | + "--output_filename", |
| 35 | + type=str, |
| 36 | + required=True, |
| 37 | + help="Filename of formatted .csv file containing SMILES." |
| 38 | + ) |
| 39 | + parser.add_argument( |
| 40 | + "--experimental_csv", |
| 41 | + type=str, |
| 42 | + required=False, |
| 43 | + help="Filename of formatted .csv file containing identifier names and experimental bool." |
| 44 | + ) |
| 45 | + parser.add_argument( |
| 46 | + '--clean_id', |
| 47 | + action='store_true', |
| 48 | + help='Removes special characters from ids that may be problematic.' |
| 49 | + ) |
| 50 | + |
| 51 | + args = parser.parse_args() |
| 52 | + |
| 53 | + output_path = os.path.join(args.input_dir, args.output_filename) |
| 54 | + with open(output_path, 'w', newline='', encoding="utf-8") as output_file: |
| 55 | + csvwriter = csv.writer(output_file, delimiter=',', quotechar='|') |
| 56 | + csvwriter.writerow(['identifier', 'n_components', |
| 57 | + 'component_a', 'component_b', 'neutral_a', 'neutral_b']) |
| 58 | + |
| 59 | + if args.experimental_csv: |
| 60 | + experimental_dict = read_experimental_csv(args.experimental_csv) |
| 61 | + |
| 62 | + # API group directories contain one or more API files and a directory of coformers |
| 63 | + API_groups = [name for name in os.listdir( |
| 64 | + args.input_dir) if os.path.isdir(os.path.join(args.input_dir, name))] |
| 65 | + |
| 66 | + exp_replaced = combo_count = 0 |
| 67 | + |
| 68 | + with open(output_path, 'a+', newline='', encoding="utf-8") as output_file: |
| 69 | + |
| 70 | + for API_group in API_groups: |
| 71 | + csvwriter = csv.writer(output_file, delimiter=',', quotechar='|') |
| 72 | + API_group_path = os.path.join(args.input_dir, API_group) |
| 73 | + |
| 74 | + for API_file in file_list(API_group_path): |
| 75 | + api_id, api_smiles = read_mol_file( |
| 76 | + API_group_path, API_file) |
| 77 | + print(api_id) |
| 78 | + coformer_dir_path = os.path.join(API_group_path, 'coformers') |
| 79 | + |
| 80 | + for coformer_file in file_list(coformer_dir_path): |
| 81 | + coformer_id, coformer_smiles = read_mol_file( |
| 82 | + coformer_dir_path, coformer_file) |
| 83 | + |
| 84 | + combo_count += 1 |
| 85 | + exp_bool = "?" |
| 86 | + # Try to look up the experimental boolean in dictionary, if provided |
| 87 | + if args.experimental_csv: |
| 88 | + for (x, y) in [(api_id, coformer_id), (coformer_id, api_id)]: |
| 89 | + if (x, y) in list(experimental_dict.keys()): |
| 90 | + exp_bool = experimental_dict[(x, y)] |
| 91 | + exp_replaced += 1 |
| 92 | + |
| 93 | + # Clean the ids if the option is turned on |
| 94 | + if args.clean_id: |
| 95 | + api_id = string_scrubber(api_id) |
| 96 | + coformer_id = string_scrubber(coformer_id) |
| 97 | + |
| 98 | + n_components = 2 |
| 99 | + if api_smiles == coformer_smiles: |
| 100 | + n_components = 1 |
| 101 | + |
| 102 | + combo_id = ".".join([api_id, coformer_id, str(exp_bool)]) |
| 103 | + csvwriter.writerow([f'"{combo_id}"', n_components, api_smiles, coformer_smiles, "", ""]) |
| 104 | + |
| 105 | + if args.experimental_csv: |
| 106 | + print(f"Found experimental labels for {exp_replaced} out of {combo_count} combinations") |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == '__main__': |
| 110 | + main() |
0 commit comments