You are on page 1of 10

class Atom:

def __init__(P,line):
P.X = float(line[31:38])
P.Y = float(line[39:46])
P.Z = float(line[47:54])
P.Line = line
P.Chain = line[21:22]
P.ResidueName = line[17:20]
P.RealAtom = line[13:16]

def Distance(P1,P2):
XDistance = P1.X - P2.X
YDistance = P1.Y - P2.Y
ZDistance = P1.Z - P2.Z
Distance = (XDistance**2+YDistance**2+ZDistance**2)**0.5
return float(Distance)

def OpenFile(Filename):
DrugStrandRaw = open(Filename,"r")
DrugStrand = []
for line in DrugStrandRaw:
NewAtom = Atom(line)
DrugStrand.append(NewAtom)
BabelStrand = NumberH(DrugStrand)
DictionaryCreator(BabelStrand)

def NumberH(DrugStrand):
RawBabelStrand = []
Count = 1
for line in DrugStrand:
if line.RealAtom[0] == "H" and Count<10:
line.Line = line.Line.replace("H ","H"+str(Count)+" ")
RawBabelStrand.append(line.Line)
Count+=1
elif line.RealAtom[0] == "H" and Count>=10:
line.Line = line.Line.replace("H ","H"+str(Count))
RawBabelStrand.append(line.Line)
Count+=1
else:
RawBabelStrand.append(line.Line)
BabelStrand = []
for line in RawBabelStrand:
NewBabel = Atom(line)
BabelStrand.append(NewBabel)
return BabelStrand

def DictionaryCreator(BabelStrand):
global Dict
Dict = {}
for line in BabelStrand:
Dict[line.RealAtom] = []
BondFinder(BabelStrand)

def BondFinder(BabelStrand):
for atom in BabelStrand:
for line in BabelStrand:
if atom.RealAtom[0] != "H" and line.RealAtom[0] != "H":
Dist = Distance(atom,line)
if Dist <= 1.8 and Dist != 0:
Dict[atom.RealAtom].append(line.RealAtom)
elif atom.RealAtom[0] == "H" or line.RealAtom[0] == "H":
Dist = Distance(atom,line)
if Dist <= 1.2 and Dist != 0:
Dict[atom.RealAtom].append(line.RealAtom)

ProduceAroRing(BabelStrand)
FunctionGroupFinder()

def FunctionGroupFinder():
AryHalide = AryHalideFinder()
Phenol = PhenolFinder()
Sulfonamide = SulfonamideFinder()
Amine = AmineFinder()
Amide = AmideFinder()
Ketone = KetoneFinder()
AlkylHalide = AlkylHalideFinder()
Cyano = CyanoFinder()
Imine = ImineFinder()
Ether = EtherFinder()
Thiol = ThiolFinder()
Thioether = ThioetherFinder()
Aldehyde = AldehydeFinder()
Ester = EsterFinder()
CarboxylicAcid = CarboxylicAcidFinder()
Alcohol = AlcoholFinder()
QuadrinaryAmine = QuadrinaryAmineFinder()

#----------------------------------------------------------------------------------
---------------------------------

def ProduceAroRing(BabelStrand):
global RawRings
RawRings = []
global Rings
Rings = []
for i in BabelStrand:
NumberOfBonds(i.RealAtom)
for atom in RawRings:
if (atom[0] == "C" and len(Dict[atom])==3) and (atom not in Rings) and
(atom != "H "):
Rings.append(atom)
if (atom[0] == "N" and len(Dict[atom])==2) and (atom not in Rings) and
(atom != "H "):
Rings.append(atom)
Holder = []
AddToRings = []
for atom in Rings:
for i in Dict[atom]:
if i not in Holder:
Holder.append(i)
elif (i in Holder) and (i not in Rings) and (i != "H "):
AddToRings.append(i)
for atom in AddToRings:
Rings.append(atom)
AllPass = False
while AllPass == False:
RingsTemp = Rings
Rings = []
AllPass = True
AtomPass = True
for atom in RingsTemp:
Count = 0
for neighbor in Dict[atom]:
if (neighbor in RingsTemp):
Count+=1
if Count == 3 or Count == 2:
AtomPass = True
Rings.append(atom)
else:
AtomPass = False
AllPass = False
if len(Rings) > 0:
FG.write("Aromatic Rings : True\n\n")
else:
FG.write("Aromatic Rings : False\n\n")

def NumberOfBonds(RealAtom):
StartingAtom = RealAtom
Atom = StartingAtom
Stem = StartingAtom
Jumps = 1
WaitJumps = 0
JumpedAtoms = []
AtomsToJumpTo = []
FindJumps(Atom,Jumps,JumpedAtoms,AtomsToJumpTo,StartingAtom,StartingAtom)

def FindJumps(Atom,Jumps,JumpedAtoms,AtomsToJumpTo,Stem,StartingAtom):
if Atom not in JumpedAtoms:
JumpedAtoms.append(Atom)
for neighbor in Dict[Atom]:
if (neighbor != Atom) and (neighbor != Stem) and (neighbor[:1] != "H"):
if (neighbor not in AtomsToJumpTo):
AtomsToJumpTo.append((neighbor,Atom))
if neighbor == StartingAtom:
RawRings.append(StartingAtom)
while (len(AtomsToJumpTo) > 0):
nextAtom,Stem = AtomsToJumpTo.pop()
if (nextAtom not in JumpedAtoms) and (nextAtom != Stem):
FindJumps(nextAtom,Jumps,JumpedAtoms,AtomsToJumpTo,Stem,StartingAtom)

#----------------------------------------------------------------------------------
---------------------------------

def AryHalideFinder():
AryHalide = False
for atom in Rings:
F = 0
Cl = 0
B = 0
I = 0
for key in Dict[atom]:
F+=key[0].count("F")
Cl+=key[0].count("L")
B+=key[0].count("B")
I+=key[0].count("I")
if Cl == 1 or F == 1 or B == 1 or I == 1:
AryHalide = True
if AryHalide == True:
#print("Ary Halide")
FG.write("Ary Halide\n")
return True
else:
return False

def PhenolFinder():
Phenol = False
for atom in Rings:
O = 0
for key in Dict[atom]:
if key[0].count("O") > 0:
O+=key[0].count("O")
Oxygen = key
if O == 1:
for particle in Dict[key]:
if particle[0].count("H")==1:
Phenol = True
if Phenol == True:
#print("Phenol")
FG.write("Phenol\n")
return True
else:
return False

def SulfonamideFinder():
Sulfonamide = False
for key in Dict:
if key[0].count("S") >= 1:
C = 0
O = 0
N = 0
for atom in Dict[key]:
C+=atom[0].count("C")
O+=atom[0].count("O")
if atom[0].count("N") == 1:
N+=atom[0].count("N")
Nitrogen = atom
if C == 1 and O == 2 and N == 1:
S = 0
C = 0
H = 0
for neighbor in Dict[Nitrogen]:
S+=neighbor[0].count("S")
C+=neighbor[0].count("C")
H+=neighbor[0].count("H")
if (C+H==2 and S==1):
Sulfonamide = True
if Sulfonamide == True:
#print("Sulfonamide")
FG.write("Sulfonamide\n")
return True
else:
return False
def AmineFinder():
Amine = False
for key in Dict:
if key.count("N") >= 1:
C = 0
H = 0
for atom in Dict[key]:
C+=atom[0].count("C")
H+=atom[0].count("H")
if C>0 and C+H == 3:
Amine = True
if Amine == True:
#print("Amine")
FG.write("Amine\n")
return True
else:
return False

def AmideFinder():
Amide = False
for key in Dict:
if key[0].count("C") >= 1:
C = 0
O = 0
N = 0
Any = 0
for atom in Dict[key]:
C+=atom[0].count("C")
O+=atom[0].count("O")
N+=atom[0].count("N")
if atom[0].count("C")+atom[0].count("O")+atom[0].count("N")==0:
Any+=1
if C == 1 and O == 1 and N == 1 and Any == 0:
Amide = True
#print (key,":",Dict[key])
if Amide == True:
#print("Amide")
FG.write("Amide\n")
return True
else:
return False

def KetoneFinder():
Ketone = False
for key in Dict:
if key[0].count("C") >= 1:
C = 0
O = 0
for atom in Dict[key]:
C+=atom[0].count("C")
if atom[0].count("O") > 0:
O+=atom[0].count("O")
Oxygen = atom
if C == 2 and O == 1:
if len(Dict[Oxygen])==1:
Ketone = True
if Ketone == True:
#print("Ketone")
FG.write("Ketone\n")
return True
else:
return False

def AlkylHalideFinder():
AlkylHalide = False
for key in Dict:
if key not in Rings:
if key[0].count("C") >= 1:
Any = 0
F = 0
Br = 0
Cl = 0
I = 0
for atom in Dict[key]:
F+=atom[0].count("F")
Br+=atom[0].count("R")
I+=atom[0].count("I")
Cl+=atom[0].count("L")
if (atom[0].count("F")+atom[0].count("R")+atom[0].count("I")
+atom[0].count("L")==0):
Any+=1
if (Any >=1) and (F >= 1 or Br >= 1 or Cl >= 1 or I >= 1) and
(len(Dict[key])==4):
AlkylHalide = True
if AlkylHalide == True:
#print("Alkyl Halide")
FG.write("Alkyl Halide\n")
return True
else:
return False

def CyanoFinder():
Cyano = False
for key in Dict:
if key[0].count("C") >= 1:
C = 0
N = 0
H = 0
for atom in Dict[key]:
if atom[0].count("N") > 0:
if len(Dict[atom]) == 1:
N+=atom[0].count("N")
C+=atom[0].count("C")
H+=atom[0].count("H")
if (N==1 and C+H==1):
Cyano = True
if Cyano == True:
#print("Cyano")
FG.write("Cyano\n")
return True
else:
return False

def ImineFinder():
Imine = False
for key in Dict:
if key[0].count("N") >= 1:
C = 0
H = 0
Any = 0
for atom in Dict[key]:
C+=atom[0].count("C")
H+=atom[0].count("H")
if atom[0].count("C")+atom[0].count("H")==0:
Any+=1
#print("\t",key,":",atom)
if (C==1 and H==1 and Any==0) or (C==2 and H==0 and Any==0):
Imine = True
if Imine == True:
#print("Imine")
FG.write("Imine\n")
return True
else:
return False

def EtherFinder():
Ether = False
for key in Dict:
if key[0].count("O") >= 1:
C = 0
for atom in Dict[key]:
C+=atom[0].count("C")
if C == 2:
Ether = True
if Ether == True:
#print("Ether")
FG.write("Ether\n")
return True
else:
return False

def ThiolFinder():
Thiol = False
for key in Dict:
if key[0].count("S") >= 1:
C = 0
H = 0
Any = 0
for atom in Dict[key]:
C+=atom[0].count("C")
H+=atom[0].count("H")
if atom[0].count("C") == 0 and atom[0].count("H") == 0:
Any+=1
if C == 1 and H == 1 and Any == 0:
Thiol = True
if Thiol == True:
#print("Thiol")
FG.write("Thiol\n")
return True
else:
return False

def ThioetherFinder():
Thioether = False
for key in Dict:
if key[0].count("S") >= 1:
C = 0
for atom in Dict[key]:
C+=atom[0].count("C")
if C == 2:
Thioether = True
if Thioether == True:
#print("Thioether")
FG.write("Thioether\n")
return True
else:
return False

def AldehydeFinder():
Aldehyde = False
for key in Dict:
if key[0].count("C") >= 1:
C = 0
O = 0
H = 0
for atom in Dict[key]:
C+=atom[0].count("C")
if atom[0].count("O") > 0:
O+=atom[0].count("O")
Oxygen = atom
H+=atom[0].count("H")
if (O == 1 and H == 2) or (O==1 and H==1 and C==1):
if len(Dict[Oxygen])==1:
Aldehyde = True
if Aldehyde == True:
#print("Aldehyde")
FG.write("Aldehyde\n")
return True
else:
return False

def EsterFinder():
Ester = False
global EsterCarbon
EsterCarbon = []
Oxygen = []
for key in Dict:
if key[0].count("C") >= 1:
C = 0
O = 0
H = 0
for atom in Dict[key]:
C+=atom[0].count("C")
if atom[0].count("O") > 0:
O+=atom[0].count("O")
Oxygen.append(key)
if C == 1 and O == 2 :
OOne = len(Dict[Oxygen[0]])
OTwo = len(Dict[Oxygen[1]])
if (OOne == 0 and OTwo == 1):
C = 0
H = 0
for i in Dict[Oxygen[0]]:
C+=i[0].count("C")
if (C==2):
Ester = True
EsterCarbon.append(key)
if (OOne == 1 and OTwo == 0):
C = 0
H = 0
for i in Dict[Oxygen[1]]:
C+=i[0].count("C")
if (C==2):
Ester = True
EsterCarbon.append(key)
if Ester == True:
#print("Ester")
FG.write("Ester\n")
return True
else:
return False

def CarboxylicAcidFinder():
CarboxylicAcid = False
global CarboxylicAcidCarbon
CarboxylicAcidCarbon = []
Oxygen = []
for key in Dict:
if key not in EsterCarbon:
if key[0].count("C") >= 1:
C = 0
O = 0
H = 0
for atom in Dict[key]:
C+=atom[0].count("C")
H+=atom[0].count("H")
if atom[0].count("O") > 0:
O+=atom[0].count("O")
Oxygen.append(key)
if (C == 1 and O == 2 and H==0) or (H==1 and O==2 and C==0):
OOne = len(Dict[Oxygen[0]])
OTwo = len(Dict[Oxygen[1]])
if (OOne == 0 and OTwo == 1):
H = 0
for i in Dict[Oxygen[0]]:
H+=i[0].count("H")
if H == 1:
CarboxylicAcid = True
CarboxylicAcidCarbon.append(key)
if (OOne == 1 and OTwo == 0):
C = 0
for i in Dict[Oxygen[1]]:
H+=i[0].count("H")
if H == 1:
CarboxylicAcid = True
CarboxylicAcidCarbon.append(key)
if CarboxylicAcid == True:
#print("Carboxylic Acid")
FG.write("Carboxylic Acid\n")
return True
else:
return False

def AlcoholFinder():
Alcohol = False
for key in Dict:
if key[0].count("O") >= 1:
C = 0
H = 0
for atom in Dict[key]:
if (atom.count("C") > 0) and (atom not in Rings) and (atom not in
EsterCarbon) and (atom not in CarboxylicAcidCarbon):
C+=atom[0].count("C")
H+=atom[0].count("H")
if C == 1 and H == 1:
Alcohol = True
if Alcohol == True:
#print("Alcohol")
FG.write("Alcohol\n")
return True
else:
return False

def QuadrinaryAmineFinder():
QuadrinaryAmine = False
for key in Dict:
if key.count("N") >= 1:
C = 0
for atom in Dict[key]:
C+=atom[0].count("C")
if C == 4:
QuadrinaryAmine = True
if QuadrinaryAmine == True:
#print("Quadrinary Amine")
FG.write("Quadrinary Amine\n")
return True
else:
return False

#----------------------------------------------------------------------------------
---------------------------------

def Start(filename):
global FG
FG = open("Alignment.txt","a")

FG.write("\n---------------------------------------------------------------------\n
\n")
OpenFile(filename)
FG.close()

#Sitagliptin contains: ArylHalide, AlkylHalide, Amine, Amide

You might also like