Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved writing for LigninBuilder #3

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions ligning/monomer.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ def create(self) -> nxgraph:
self.G = G_m

# Initialize a big graph for monomer
bigG = nx.Graph()
bigG = nx.DiGraph()
bigG.add_node(self.mi, mtype = self.type, color = default_color[self.type])
self.bigG = bigG

return G_m
return G_m
70 changes: 65 additions & 5 deletions ligning/polymer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Build lignin polymers
"""
import networkx as nx

import os
from typing import Optional, TypeVar, Union, Tuple, List
from copy import copy
import warnings
Expand Down Expand Up @@ -131,7 +131,6 @@ def find_available_C2_in_monomer(self, C1_index_in_polymer: int, ring: Optional[

if ring:
C2_indices_in_monmer = [ci for ci in C2_indices_in_monmer if ci != 1]
#print(C2_indices_in_monmer)

return C2_indices_in_monmer

Expand Down Expand Up @@ -422,7 +421,10 @@ def update_units(
M2_index = self.G.nodes[C2_index_in_polymer]['mi']

# Add the edges to monomer nodes
self.bigG.add_edges_from([(M1_index, M2_index)], btype=linkage_new_name)
if linkage_index[0] <= linkage_index[1]:
self.bigG.add_edges_from([(M1_index, M2_index)], btype=linkage_new_name)
else:
self.bigG.add_edges_from([(M2_index, M1_index)], btype=linkage_new_name)


def try_adding_new(
Expand Down Expand Up @@ -463,10 +465,8 @@ def try_adding_new(
if monomer_new is not None:
monomer_new.create()
PG_temp.G = ut.join_two(PG_temp.G, monomer_new.G)

# Add the linkage
new_linkage_flag = PG_temp.connect_C1_C2(linkage_index, C1_index_in_polymer, C2_index_in_polymer)

# If a new linakge does form
if new_linkage_flag:
# Update the polymer graph
Expand Down Expand Up @@ -891,6 +891,66 @@ def add_specific_ring(

return new_linkage_flag

def write_LigninBuilder(self,
name: Optional[str]='test',
segname: Optional[str]='L',
save_path: Optional[str]=os.getcwd()) -> None:
"""Convert this polymer to a Tcl script that VMD and LigninBuilder can convert to a 3D structure

Parameters
----------
name : Optional[str], optional
name of the file to be written without the .tcl extension, by default 'test', creating a 'test.tcl' file
segname : Optional[str], optional
segment name created. Default is "L"
save_path : Optional[str], optional
path to save the figure, by default os.getcwd()
"""
H = self.bigG
if not os.path.exists(save_path):
os.mkdir(save_path)
filename = os.path.join(save_path, name+'.tcl')
with open(filename, 'w') as fout:
fout.write("package require psfgen\ntopology toppar/top_all36_cgenff.rtf\ntopology toppar/top_lignin.top\n"
"topology toppar/top_spirodienone.top\n")
# Write monomers
fout.write("resetpsf\nsegment %s {\n" % segname)
for i in range(len(H.nodes)):
if H.nodes[i]['mtype'] == 'G':
monomer = "GUAI"
elif H.nodes[i]['mtype'] == 'C':
monomer = "CAT"
elif H.nodes[i]['mtype'] == 'H':
monomer = "PHP"
elif H.nodes[i]['mtype'] == 'S':
monomer = "SYR"
else:
raise ValueError("Monomer type not allowed. Must input C, H, G or S.")
fout.write("residue %d %s\n" % (i+1, monomer))
fout.write("}\n")
#Write linkages
for ei in list(H.edges):
#ei will be the tuple describing the nodes that are connected.
#The order is important! The first node is the residue with the lower numbered carbon involved in the linkage.
#Based on the patches used by LigninBuilder, sometimes the "first" residue in the patch is the first residue in the graph, and sometimes its the second.
if H.edges[ei]['btype'] == '4-O-5':
fout.write("patch 4O5 %s:%s %s:%s\n" % (segname, ei[0]+1, segname, ei[1]+1))
elif H.edges[ei]['btype'] == 'alpha-O-4':
fout.write("patch AO4 %s:%s %s:%s\n" % (segname, ei[1]+1, segname, ei[0]+1))
elif H.edges[ei]['btype'] == 'beta-O-4':
fout.write("patch BO4 %s:%s %s:%s\n" % (segname, ei[1]+1, segname, ei[0]+1))
elif H.edges[ei]['btype'] == '5-5':
fout.write("patch 55 %s:%s %s:%s\n" % (segname, ei[0]+1, segname, ei[1]+1))
elif H.edges[ei]['btype'] == 'beta-5':
if H.nodes[ei[0]]['mtype'] == 'H':
fout.write("patch B5P %s:%s %s:%s\n" % (segname, ei[1]+1, segname, ei[0]+1))
else:
fout.write("patch B5%s %s:%s %s:%s\n" % (H.nodes[ei[0]]['mtype'], segname, ei[1]+1, segname, ei[0]+1))
elif H.edges[ei]['btype'] == 'beta-beta':
fout.write("patch BB %s:%s %s:%s\n" % (segname, ei[0]+1, segname, ei[1]+1))
elif H.edges[ei]['btype'] == 'beta-1':
fout.write("patch B1 %s:%s %s:%s\n" % (segname, ei[1]+1, segname, ei[0]+1))
fout.write("writepsf %s.psf\n" % segname)



5 changes: 1 addition & 4 deletions ligning/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import matplotlib
if platform.system() == 'Linux':
matplotlib.use('Agg')
print('switched')
#print('switched')
import matplotlib.pyplot as plt
# if platform.system() == 'Linux':
# plt.switch_backend('agg')
Expand Down Expand Up @@ -93,7 +93,6 @@ def draw_big_graph(G: nxgraph) -> None:
# big graph is marked by hexagons
draw_graph(G, node_labels, node_shape = 'h', node_size=1000)


def draw_atomic_graph(G: nxgraph) -> None:
"""Plot the atomic graph

Expand Down Expand Up @@ -163,8 +162,6 @@ def graph_to_mol(
Chem.Draw.MolToFile(mol, filename, size=(500, 500))

return mol



def smiles_to_formula(smiles: str) -> str:
"""Convert the smiles to the chemical formula in C, H and O
Expand Down