generated from Quantum-Accelerators/template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
37 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,43 @@ | ||
#This script extracts the smiles from the file named as drugs.txt | ||
import os | ||
def getSmilesFromFile(fileName): | ||
INF = open(fileName,'r').readlines() | ||
tmp1 = [i.strip().split() for i in INF][1:] | ||
molNames, smiles = [], [] | ||
for i in tmp1: | ||
if (i[-1] == 'TRUE') or (i[-1] == 'FALSE'): | ||
from typing import List, Tuple | ||
|
||
|
||
def get_smiles_from_file(file_name: str) -> Tuple[List[str], List[str]]: | ||
""" | ||
Extracts smiles from the given file. | ||
Args: | ||
file_name (str): The name of the file containing smiles. | ||
Returns: | ||
Tuple[List[str], List[str]]: A tuple containing lists of molecule names and smiles. | ||
""" | ||
with open(file_name, 'r') as file: | ||
lines = file.readlines()[1:] | ||
|
||
mol_names, smiles = [], [] | ||
|
||
for line in lines: | ||
elements = line.strip().split() | ||
if elements[-1] in {'TRUE', 'FALSE'}: | ||
continue | ||
else: | ||
if len(i) > 3: | ||
molNames.append('_'.join([j.replace('/','-') for j in i[:-2]])) | ||
smiles.append(i[-1]) | ||
elif len(i) == 3: | ||
molNames.append(i[0]) | ||
smiles.append(i[2]) | ||
if len(elements) > 3: | ||
mol_names.append('_'.join([element.replace('/', '-') for element in elements[:-2]])) | ||
smiles.append(elements[-1]) | ||
elif len(elements) == 3: | ||
mol_names.append(elements[0]) | ||
smiles.append(elements[2]) | ||
else: | ||
print('something wrong with ', i, '.') | ||
|
||
#Writing the names of molecules in a file inside a separate directory | ||
#for future calculations | ||
if not os.path.exists('../../moleculeLists'): | ||
os.mkdir('../../moleculeLists') | ||
os.chdir('../../moleculeLists') | ||
with open("fileList.txt", 'w') as file: | ||
file.write('\n'.join(molNames)) | ||
print(f'Something wrong with {elements}.') | ||
|
||
return [molNames, smiles] | ||
# Writing the names of molecules in a file inside a separate directory for future calculations | ||
molecule_lists_dir = '../../moleculeLists' | ||
if not os.path.exists(molecule_lists_dir): | ||
os.mkdir(molecule_lists_dir) | ||
os.chdir(molecule_lists_dir) | ||
|
||
with open("fileList.txt", 'w') as file: | ||
file.write('\n'.join(mol_names)) | ||
|
||
return mol_names, smiles |