Skip to content

Commit

Permalink
Fix Warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Old-Shatterhand committed Oct 16, 2024
1 parent 1e7a969 commit 46d6a97
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 31 deletions.
19 changes: 7 additions & 12 deletions glyles/glycans/mono/enum_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def enumerate_carbon(monomer):

# then iterate over all those carbons and enumerate them beginning at C1
for c_id in range(1, next_c_id):
c_index = int(np.where(monomer.x[:, 1] == c_id)[0])
c_index = np.where(monomer.x[:, 1] == c_id)[0].item()
candidates = np.where(np.array(monomer.adjacency[c_index, :] != 0) & (monomer.x[:, 1] == 0))[0]
if candidates.size != 0:
for candidate in list(candidates):
Expand Down Expand Up @@ -100,14 +100,9 @@ def enumerate_c_atoms(monomer, c_atoms, ringo):
start, end = longest_c_chain[0], longest_c_chain[-1]

# check conditions
"""start_o_conn, end_o_conn = \
np.argwhere((monomer.adjacency[start, :] == 1) & np.in1d(monomer.x[:, 0], [7, 8]) & (monomer.x[:, 2] != 1) &
(monomer.x[:, 3] == 1)).squeeze().size > 0, \
np.argwhere((monomer.adjacency[end, :] == 1) & np.in1d(monomer.x[:, 0], [7, 8]) & (monomer.x[:, 2] != 1) &
(monomer.x[:, 3] == 1)).squeeze().size > 0"""
start_o_conn = np.argwhere(np.array(monomer.adjacency[start, :] == 1) & np.in1d(monomer.x[:, 0], [7, 8]) &
start_o_conn = np.argwhere(np.array(monomer.adjacency[start, :] == 1) & np.isin(monomer.x[:, 0], [7, 8]) &
(monomer.x[:, 2] != 1)).squeeze().size > 0
end_o_conn = np.argwhere(np.array(monomer.adjacency[end, :] == 1) & np.in1d(monomer.x[:, 0], [7, 8]) &
end_o_conn = np.argwhere(np.array(monomer.adjacency[end, :] == 1) & np.isin(monomer.x[:, 0], [7, 8]) &
(monomer.x[:, 2] != 1)).squeeze().size > 0

# decide on c1
Expand Down Expand Up @@ -173,14 +168,14 @@ def equidistant(monomer, start, end):
c_end_candidates = np.where(np.array(monomer.adjacency[end, :] == 1) & (monomer.x[:, 0] == 6) & (monomer.x[:, 2] & 0b1))[0]

if c_start_candidates.size == 1 and c_end_candidates.size == 1:
start_ring_c = int(c_start_candidates)
end_ring_c = int(c_end_candidates)
start_ring_c = c_start_candidates.item()
end_ring_c = c_end_candidates.item()

# check if those ring carbons have an attached oxygen
start_ring_c_o_candidates = np.where(np.array(monomer.adjacency[start_ring_c, :] == 1) &
np.in1d(monomer.x[:, 0], [7, 8]) & (monomer.x[:, 2] != 1))[0]
np.isin(monomer.x[:, 0], [7, 8]) & (monomer.x[:, 2] != 1))[0]
end_ring_c_o_candidates = np.where(np.array(monomer.adjacency[end_ring_c, :] == 1) &
np.in1d(monomer.x[:, 0], [7, 8]) & (monomer.x[:, 2] != 1))[0]
np.isin(monomer.x[:, 0], [7, 8]) & (monomer.x[:, 2] != 1))[0]

if start_ring_c_o_candidates.size == 1 and end_ring_c_o_candidates.size == 1:
raise UnreachableError("C1 atom cannot be detected")
Expand Down
4 changes: 2 additions & 2 deletions glyles/glycans/mono/monomer.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,9 @@ def to_smiles(self, ring_index, root_idx=None, root_id=None):
assert root_idx is not None or root_id is not None, "Either Index or ID has to be provided"
if root_id is None:
if np.where(self.x[:, 1] == root_idx)[0].size != 0:
root_id = int(np.where(self.x[:, 1] == root_idx)[0])
root_id = np.where(self.x[:, 1] == root_idx)[0].item()
else:
root_id = int(np.where(self.x[:, 1] == 1)[0])
root_id = np.where(self.x[:, 1] == 1)[0].item()

root_id = self.__check_root_id(root_id)

Expand Down
26 changes: 13 additions & 13 deletions glyles/glycans/mono/reactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"Lac": "OC(=O)C(O)C",
"Lin": "OC(=O)CCCCCCC/C=C\\C/C=C\\CCCCC",
"Mal": "O[C@H](C(=O)O)CC(=O)O",
"Ole": "OC(=O)CCCCCCC/C=C\CCCCCCCC",
"Ole": "OC(=O)CCCCCCC/C=C\\CCCCCCCC",
"Ph": "c2ccccc2",
"Phyt": "OCCC(C)CCCC(C)CCCC(C)CCCC(C)C",
"Pyr": "OC(=O)C(=O)C",
Expand Down Expand Up @@ -290,12 +290,12 @@ def react(self, names, types):
if n == "A" or n == "-uronic":
# if there's no ring take carbon with the highest number
if sum(self.monomer.x[:, 2] & 0b1) == 0:
c_id = int(max(self.monomer.x[self.monomer.x[:, 0] == 6, 1]))
c_id = np.max(self.monomer.x[self.monomer.x[:, 0] == 6, 1]).item()

# else take the last carbon in the ring
else:
c_id = int(max(self.monomer.x[(self.monomer.x[:, 0] == 6) & (self.monomer.x[:, 2] & 0b1), 1]))
c_id = int(np.where(self.monomer.x[:, 1] == c_id)[0])
c_id = np.max(self.monomer.x[(self.monomer.x[:, 0] == 6) & (self.monomer.x[:, 2] & 0b1), 1]).item()
c_id = np.where(self.monomer.x[:, 1] == c_id)[0].item()

# if the selected carbon has a tail raging away from the monomer, iterate all the way down
children = np.where(np.array(self.monomer.adjacency[c_id, :] == 1) & (self.monomer.x[:, 0] == 6) &
Expand Down Expand Up @@ -352,20 +352,20 @@ def react(self, names, types):
# add a functional group connected with an oxygen or nitrogen
elif len(n) > 4 and n[1] == n[3] == "-" and n[2] in "ON":
elem = "" if functional_groups[n[4:-1]][0] == n[2] else n[2]
full &= self.set_fg(C if int(self.monomer.x[self.monomer.find_oxygen(int(n[0])), 0]) == 6 else O, int(n[0]), elem, n[4:-1])
full &= self.set_fg(C if self.monomer.x[self.monomer.find_oxygen(int(n[0])), 0].item() == 6 else O, int(n[0]), elem, n[4:-1])

# connect a functional group with nitrogen, oxygen, or phosphate in between
elif n[1] in "NOP" and n[1:] not in n_conflict + o_conflict + p_conflict:
bridge, fg = extract_bridge(n)
if len(bridge) > 0 and functional_groups[fg][0] == bridge[-1]:
bridge = bridge[:-1]
full &= self.set_fg(C if int(self.monomer.x[self.monomer.find_oxygen(int(n[0])), 0]) == 6 else O, int(n[0]), bridge, fg)
full &= self.set_fg(C if self.monomer.x[self.monomer.find_oxygen(int(n[0])), 0].item() == 6 else O, int(n[0]), bridge, fg)

# add a poly carbon group
elif (n[1] == "C" and n[2:4].isnumeric()) or \
(n[1:3] in "aCiC" and n[3:5].isnumeric()) or \
(n[1:4] == "aiC" and n[4:6].isnumeric()):
full &= self.set_fg(C if int(self.monomer.x[self.monomer.find_oxygen(int(n[0])), 0]) == 6 else O, int(n[0]), "", n)
full &= self.set_fg(C if self.monomer.x[self.monomer.find_oxygen(int(n[0])), 0].item() == 6 else O, int(n[0]), "", n)

# add a group connected directly to the C-Atom
elif n[1] == "C" and n[1:] not in c_conflict:
Expand All @@ -376,7 +376,7 @@ def react(self, names, types):
else:
elem = self.monomer.structure.GetAtomWithIdx(self.monomer.find_oxygen(int(n[0]))).GetSymbol() \
if n[1:] in preserve_elem else ""
attach_to = C if int(self.monomer.x[self.monomer.find_oxygen(int(n[0])), 0]) == 6 else O
attach_to = C if self.monomer.x[self.monomer.find_oxygen(int(n[0])), 0].item() == 6 else O
if elem == "C":
full &= self.set_fg(attach_to, int(n[0]), "", n[1:])
else:
Expand All @@ -390,14 +390,14 @@ def react(self, names, types):

# attach the monomer to the second carbon in the ring, it might be C2 or C3 (for 2-ketoses)
if fg == "Me":
full &= self.set_fg(C if int(self.monomer.x[self.monomer.find_oxygen(self.ring_c), 0]) == 6 else O, self.ring_c, bridge, fg)
full &= self.set_fg(C if self.monomer.x[self.monomer.find_oxygen(self.ring_c), 0].item() == 6 else O, self.ring_c, bridge, fg)
else:
full &= self.set_fg(C if int(self.monomer.x[self.monomer.find_oxygen(self.ring_c + 1), 0]) == 6 else O, self.ring_c + 1, bridge, fg)
full &= self.set_fg(C if self.monomer.x[self.monomer.find_oxygen(self.ring_c + 1), 0].item() == 6 else O, self.ring_c + 1, bridge, fg)

# functional groups might be directly connected to the carbon of the ring
elif n[0] == "C" and n not in c_conflict:
if "=" in n or n[1:].isdigit():
self.set_fg(C if int(self.monomer.x[self.monomer.find_oxygen(self.ring_c), 0]) == 6 else O, self.ring_c, "O", self.parse_poly_carbon(n))
self.set_fg(C if self.monomer.x[self.monomer.find_oxygen(self.ring_c), 0].item() == 6 else O, self.ring_c, "O", self.parse_poly_carbon(n))
else: # add a group connected directly to the C-Atom
bridge, fg = extract_bridge(n)
full &= self.set_fg(C, self.ring_c, bridge, fg)
Expand All @@ -407,7 +407,7 @@ def react(self, names, types):
elem = self.monomer.structure.GetAtomWithIdx(self.monomer.find_oxygen(int(n[0]))).GetSymbol() \
if n[1:] in preserve_elem else ""
elem = "" if functional_groups[n][0] == elem else elem
full &= self.set_fg(C if int(self.monomer.x[self.monomer.find_oxygen(self.ring_c), 0]) == 6 else O, self.ring_c, elem, n)
full &= self.set_fg(C if self.monomer.x[self.monomer.find_oxygen(self.ring_c), 0].item() == 6 else O, self.ring_c, elem, n)

# after storing all functional groups in a list, iterate over them and put them into the monomers structure
self.assemble_chains()
Expand Down Expand Up @@ -478,7 +478,7 @@ def check_for_anhydro(self, names, types):
nums = [int(x) for x in re.findall(r'\d+', n)]
if len(nums) != 2:
raise ValueError("Anhydro functional groups should have exactly two numbers: X,Y-Anhydro-...")
y_c = int(np.where(self.monomer.x[:, 1] == nums[1])[0])
y_c = np.where(self.monomer.x[:, 1] == nums[1])[0].item()
x_o = self.monomer.find_oxygen(nums[0])
y_o = self.monomer.find_oxygen(nums[1])

Expand Down
2 changes: 1 addition & 1 deletion glyles/glycans/mono/reactor_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def check_for_resizing(monomer, names, types):

# find oxygen of c6 to delete it and find c6 to replace it
# TODO: Check if this is actually valid for all cases
c_id = int(np.where(monomer.x[:, 1] == int(max(monomer.x[monomer.x[:, 0] == 6, 1])))[0])
c_id = np.where(monomer.x[:, 1] == np.max(monomer.x[monomer.x[:, 0] == 6, 1]).item())[0].item()
ox_id = monomer.find_oxygen(position=c_id)

# if the carbon to be extended is part of the ring, put the extension into a side_chain
Expand Down
4 changes: 2 additions & 2 deletions glyles/glycans/poly/merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ def merge(self, t, root_orientation="n", start=100, smiles_only: bool = True):

# return the string that can be computed from connecting the monomers as marked above
if np.where(t.nodes[0]["type"].get_features()[:, 1] == start)[0].size != 0:
position = int(np.argwhere(t.nodes[0]["type"].get_features()[:, 1] == start).squeeze())
position = np.argwhere(t.nodes[0]["type"].get_features()[:, 1] == start).squeeze().item()
else:
position = int(np.argwhere(t.nodes[0]["type"].get_features()[:, 1] == 1).squeeze())
position = np.argwhere(t.nodes[0]["type"].get_features()[:, 1] == 1).squeeze().item()
smiles, mask = self.merge_int(t, 0, position, 0)
if smiles_only:
return smiles
Expand Down
2 changes: 1 addition & 1 deletion tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ def test_parse_ternary_branching_2(self, mode):
check_child(g, id_child_1, id_child_23, "Gal", "(a1-6)", 0, Lactole.PYRANOSE)

def test_parsing_error(self, caplog):
iupac = "Alt(a1-2)[Glc(a1-4)][Gal(a1-6)]Gul(a1-4)M*#$s'\d ;«]as;an" # Invalid IUPAC string!
iupac = "Alt(a1-2)[Glc(a1-4)][Gal(a1-6)]Gul(a1-4)M*#$s'\\d ;«]as;an" # Invalid IUPAC string!
g = Glycan(iupac).get_tree()
assert g is None
assert "A parsing error occurred" in caplog.records[0].msg

0 comments on commit 46d6a97

Please sign in to comment.