Mercurial > repos > recetox > rem_complex
comparison rem_complex.py @ 0:a0e07a0bc047 draft
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/rem_complex commit 833f7671d1e1b713d52ba5c7e59d28be38b92b1e
| author | recetox |
|---|---|
| date | Mon, 27 Nov 2023 09:04:04 +0000 |
| parents | |
| children | 567327a97ad2 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:a0e07a0bc047 |
|---|---|
| 1 import argparse | |
| 2 | |
| 3 import pandas as pd | |
| 4 from openbabel import openbabel, pybel | |
| 5 openbabel.obErrorLog.SetOutputLevel(1) # 0: suppress warnings; 1: warnings | |
| 6 | |
| 7 | |
| 8 def parse_arguments() -> argparse.Namespace: | |
| 9 parser = argparse.ArgumentParser() | |
| 10 parser.add_argument('-iformat', '--input_format', help='Input file format') | |
| 11 parser.add_argument('-i', '--input_filename', type=str, required=True, help='Input file name') | |
| 12 parser.add_argument('-o', '--output_filename', type=str, required=True, help='Outout file name') | |
| 13 args = parser.parse_args() | |
| 14 return args | |
| 15 | |
| 16 | |
| 17 def filter_csv_molecules(file_name: str, output_file_name: str) -> None: | |
| 18 """Removes molecules with '.' in SMILES string from csv file. | |
| 19 | |
| 20 Args: | |
| 21 file_name (str): Path to csv file that contains metadata. | |
| 22 output_file_name (str): Path to destination file, in csv format. | |
| 23 """ | |
| 24 df = pd.read_csv(file_name) | |
| 25 mask = df['smiles'].str.contains(".", na=False, regex=False) | |
| 26 mask = mask.apply(lambda x: not x) | |
| 27 df[mask].to_csv(output_file_name, index=False) | |
| 28 | |
| 29 | |
| 30 def filter_other_format_molecules(file_name: str, output_file_name: str, input_format: str) -> None: | |
| 31 """Removes molecules with '.' in SMILES string from smi or inchi files. | |
| 32 | |
| 33 Args: | |
| 34 file_name (str): Path to smi or inchi files. | |
| 35 output_file_name (str): Path to destination files, in smi or inchi formats. | |
| 36 input_format (str): Input file format. | |
| 37 """ | |
| 38 molecules = list(pybel.readfile(input_format, file_name)) | |
| 39 filtered_molecules = [mol for mol in molecules if "." not in mol.write('smi').strip()] | |
| 40 | |
| 41 with open(output_file_name, 'w') as f: | |
| 42 for mol in filtered_molecules: | |
| 43 f.write(mol.write(input_format)) | |
| 44 | |
| 45 | |
| 46 def filter_complex_molecules(file_name: str, output_file_name: str, input_format: str) -> None: | |
| 47 """Removes molecular complexes depending on the input format. | |
| 48 | |
| 49 Args: | |
| 50 file_name (str): Path to csv, smi or inchi files | |
| 51 output_file_name (str): Path to destination files, in csv. smi or inchi formats. | |
| 52 input_format (str): Input file formats. | |
| 53 """ | |
| 54 if input_format == 'csv': | |
| 55 filter_csv_molecules(file_name, output_file_name) | |
| 56 else: | |
| 57 filter_other_format_molecules(file_name, output_file_name, input_format) | |
| 58 | |
| 59 | |
| 60 if __name__ == "__main__": | |
| 61 args = parse_arguments() | |
| 62 filter_complex_molecules(args.input_filename, args.output_filename, args.input_format) |
