Fix script for strings beyond first whitespace
Browse files- .gitattributes +1 -0
- README.md +7 -1
- selfies_converter.py +17 -8
.gitattributes
CHANGED
|
@@ -58,3 +58,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 58 |
*.webm filter=lfs diff=lfs merge=lfs -text
|
| 59 |
*.smi filter=lfs diff=lfs merge=lfs -text
|
| 60 |
*.csv filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 58 |
*.webm filter=lfs diff=lfs merge=lfs -text
|
| 59 |
*.smi filter=lfs diff=lfs merge=lfs -text
|
| 60 |
*.csv filter=lfs diff=lfs merge=lfs -text
|
| 61 |
+
*.selfies filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -57,9 +57,15 @@ find . -name "*.smi" | xargs -I {} -P$(nproc) sh -c 'python selfies_converter.p
|
|
| 57 |
|
| 58 |
Note: Be cautious when using the command that removes original files. Ensure you have backups before running it.
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
## Output
|
| 61 |
|
| 62 |
Both scripts will create output files in the same directory as the input files:
|
| 63 |
|
| 64 |
- The file splitter creates `<original_name>.part01.smi`, `<original_name>.part02.smi`, etc.
|
| 65 |
-
- The SELFIES converter creates `<original_name>.selfies` for the converted file and `<original_name>.alph` for the alphabet file.
|
|
|
|
| 57 |
|
| 58 |
Note: Be cautious when using the command that removes original files. Ensure you have backups before running it.
|
| 59 |
|
| 60 |
+
With gz:
|
| 61 |
+
```bash
|
| 62 |
+
find . -name "*.smi.gz" | xargs -P$(nproc) -I {} sh -c 'f="{}"; g="${f%.gz}"; gzip -dc "$f" > "$g" && python selfies_converter.py "$g" && rm "$g" && rm "$f"'
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
|
| 66 |
## Output
|
| 67 |
|
| 68 |
Both scripts will create output files in the same directory as the input files:
|
| 69 |
|
| 70 |
- The file splitter creates `<original_name>.part01.smi`, `<original_name>.part02.smi`, etc.
|
| 71 |
+
- The SELFIES converter creates `<original_name>.selfies` for the converted file and `<original_name>.alph` for the alphabet file.
|
selfies_converter.py
CHANGED
|
@@ -11,6 +11,7 @@ def convert_file(input_file, verbose=False, batch_size=10000):
|
|
| 11 |
alphabet = set()
|
| 12 |
total_lines = 0
|
| 13 |
processed_lines = 0
|
|
|
|
| 14 |
|
| 15 |
# Count total lines if verbose
|
| 16 |
if verbose:
|
|
@@ -23,9 +24,11 @@ def convert_file(input_file, verbose=False, batch_size=10000):
|
|
| 23 |
|
| 24 |
batch = []
|
| 25 |
for line in fin:
|
| 26 |
-
|
|
|
|
|
|
|
| 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))
|
|
@@ -33,7 +36,7 @@ def convert_file(input_file, verbose=False, batch_size=10000):
|
|
| 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))
|
|
@@ -47,17 +50,23 @@ def convert_file(input_file, verbose=False, batch_size=10000):
|
|
| 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 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
| 59 |
except sf.EncoderError:
|
| 60 |
-
|
|
|
|
| 61 |
|
| 62 |
def main():
|
| 63 |
parser = argparse.ArgumentParser(description="SELFIES Converter")
|
|
@@ -69,4 +78,4 @@ def main():
|
|
| 69 |
convert_file(args.input_file, args.verbose, args.batch_size)
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
| 72 |
-
main()
|
|
|
|
| 11 |
alphabet = set()
|
| 12 |
total_lines = 0
|
| 13 |
processed_lines = 0
|
| 14 |
+
error_count = 0
|
| 15 |
|
| 16 |
# Count total lines if verbose
|
| 17 |
if verbose:
|
|
|
|
| 24 |
|
| 25 |
batch = []
|
| 26 |
for line in fin:
|
| 27 |
+
# Truncate line at first whitespace
|
| 28 |
+
smiles = line.split()[0]
|
| 29 |
+
batch.append(smiles)
|
| 30 |
if len(batch) == batch_size:
|
| 31 |
+
error_count += process_batch(batch, fout, alphabet)
|
| 32 |
processed_lines += len(batch)
|
| 33 |
if verbose:
|
| 34 |
pbar.update(len(batch))
|
|
|
|
| 36 |
|
| 37 |
# Process remaining lines
|
| 38 |
if batch:
|
| 39 |
+
error_count += process_batch(batch, fout, alphabet)
|
| 40 |
processed_lines += len(batch)
|
| 41 |
if verbose:
|
| 42 |
pbar.update(len(batch))
|
|
|
|
| 50 |
|
| 51 |
if verbose:
|
| 52 |
print(f"Conversion complete. Processed {processed_lines} lines.")
|
| 53 |
+
print(f"Encountered {error_count} errors during conversion.")
|
| 54 |
print(f"SELFIES saved to {selfies_file}")
|
| 55 |
print(f"Alphabet saved to {alphabet_file}")
|
| 56 |
|
| 57 |
def process_batch(batch, fout, alphabet):
|
| 58 |
+
error_count = 0
|
| 59 |
for smiles in batch:
|
| 60 |
try:
|
| 61 |
+
selfies = sf.encoder(smiles, strict=False)
|
| 62 |
+
if selfies: # Check if the SELFIES string is not empty
|
| 63 |
+
fout.write(f"{selfies}\n")
|
| 64 |
+
alphabet.update(sf.split_selfies(selfies))
|
| 65 |
+
else:
|
| 66 |
+
error_count += 1
|
| 67 |
except sf.EncoderError:
|
| 68 |
+
error_count += 1
|
| 69 |
+
return error_count
|
| 70 |
|
| 71 |
def main():
|
| 72 |
parser = argparse.ArgumentParser(description="SELFIES Converter")
|
|
|
|
| 78 |
convert_file(args.input_file, args.verbose, args.batch_size)
|
| 79 |
|
| 80 |
if __name__ == "__main__":
|
| 81 |
+
main()
|