Skip to content

Commit

Permalink
Cleaned up getSmilesFromFile.py.
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaranu committed May 1, 2024
1 parent b2b96eb commit ea32a72
Showing 1 changed file with 37 additions and 23 deletions.
60 changes: 37 additions & 23 deletions src/MolecularDockingKit/getSmilesFromFile.py
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

0 comments on commit ea32a72

Please sign in to comment.