batched loading
Browse files- selfies_converter.py +44 -18
selfies_converter.py
CHANGED
|
@@ -3,44 +3,70 @@ import os
|
|
| 3 |
import selfies as sf
|
| 4 |
from tqdm import tqdm
|
| 5 |
|
| 6 |
-
def convert_file(input_file, verbose=False):
|
| 7 |
base_name = os.path.splitext(input_file)[0]
|
| 8 |
selfies_file = f"{base_name}.selfies"
|
| 9 |
alphabet_file = f"{base_name}.alph"
|
| 10 |
|
| 11 |
alphabet = set()
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
with open(input_file, 'r') as fin, open(selfies_file, 'w') as fout:
|
| 14 |
-
lines = fin.readlines()
|
| 15 |
if verbose:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
except sf.EncoderError as e:
|
| 25 |
if verbose:
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
with open(alphabet_file, 'w') as f:
|
| 30 |
for symbol in sorted(alphabet):
|
| 31 |
f.write(f"{symbol}\n")
|
| 32 |
|
| 33 |
if verbose:
|
| 34 |
-
print(f"Conversion complete.
|
|
|
|
| 35 |
print(f"Alphabet saved to {alphabet_file}")
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
def main():
|
| 38 |
parser = argparse.ArgumentParser(description="SELFIES Converter")
|
| 39 |
parser.add_argument("input_file", help="Input SMILES file (.smi)")
|
| 40 |
parser.add_argument("-v", "--verbose", action="store_true", help="Increase output verbosity")
|
|
|
|
| 41 |
args = parser.parse_args()
|
| 42 |
|
| 43 |
-
convert_file(args.input_file, args.verbose)
|
| 44 |
|
| 45 |
if __name__ == "__main__":
|
| 46 |
-
main()
|
|
|
|
| 3 |
import selfies as sf
|
| 4 |
from tqdm import tqdm
|
| 5 |
|
| 6 |
+
def convert_file(input_file, verbose=False, batch_size=10000):
|
| 7 |
base_name = os.path.splitext(input_file)[0]
|
| 8 |
selfies_file = f"{base_name}.selfies"
|
| 9 |
alphabet_file = f"{base_name}.alph"
|
| 10 |
|
| 11 |
alphabet = set()
|
| 12 |
+
total_lines = 0
|
| 13 |
+
processed_lines = 0
|
| 14 |
+
|
| 15 |
+
# Count total lines if verbose
|
| 16 |
+
if verbose:
|
| 17 |
+
with open(input_file, 'r') as fin:
|
| 18 |
+
total_lines = sum(1 for _ in fin)
|
| 19 |
+
|
| 20 |
with open(input_file, 'r') as fin, open(selfies_file, 'w') as fout:
|
|
|
|
| 21 |
if verbose:
|
| 22 |
+
pbar = tqdm(total=total_lines, desc=f"Converting {input_file}")
|
| 23 |
+
|
| 24 |
+
batch = []
|
| 25 |
+
for line in fin:
|
| 26 |
+
batch.append(line.strip())
|
| 27 |
+
if len(batch) == batch_size:
|
| 28 |
+
process_batch(batch, fout, alphabet)
|
| 29 |
+
processed_lines += len(batch)
|
|
|
|
| 30 |
if verbose:
|
| 31 |
+
pbar.update(len(batch))
|
| 32 |
+
batch = []
|
| 33 |
+
|
| 34 |
+
# Process remaining lines
|
| 35 |
+
if batch:
|
| 36 |
+
process_batch(batch, fout, alphabet)
|
| 37 |
+
processed_lines += len(batch)
|
| 38 |
+
if verbose:
|
| 39 |
+
pbar.update(len(batch))
|
| 40 |
+
|
| 41 |
+
if verbose:
|
| 42 |
+
pbar.close()
|
| 43 |
+
|
| 44 |
with open(alphabet_file, 'w') as f:
|
| 45 |
for symbol in sorted(alphabet):
|
| 46 |
f.write(f"{symbol}\n")
|
| 47 |
|
| 48 |
if verbose:
|
| 49 |
+
print(f"Conversion complete. Processed {processed_lines} lines.")
|
| 50 |
+
print(f"SELFIES saved to {selfies_file}")
|
| 51 |
print(f"Alphabet saved to {alphabet_file}")
|
| 52 |
|
| 53 |
+
def process_batch(batch, fout, alphabet):
|
| 54 |
+
for smiles in batch:
|
| 55 |
+
try:
|
| 56 |
+
selfies = sf.encoder(smiles)
|
| 57 |
+
fout.write(f"{selfies}\n")
|
| 58 |
+
alphabet.update(sf.split_selfies(selfies))
|
| 59 |
+
except sf.EncoderError:
|
| 60 |
+
continue
|
| 61 |
+
|
| 62 |
def main():
|
| 63 |
parser = argparse.ArgumentParser(description="SELFIES Converter")
|
| 64 |
parser.add_argument("input_file", help="Input SMILES file (.smi)")
|
| 65 |
parser.add_argument("-v", "--verbose", action="store_true", help="Increase output verbosity")
|
| 66 |
+
parser.add_argument("-b", "--batch-size", type=int, default=10000, help="Batch size for processing")
|
| 67 |
args = parser.parse_args()
|
| 68 |
|
| 69 |
+
convert_file(args.input_file, args.verbose, args.batch_size)
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
| 72 |
+
main()
|