%matplotlib inline
import matplotlib.pyplot as plt
import mdtraj as md
traj = md.load("5550217/kras.xtc", top="5550217/kras.pdb")
topology = traj.topology

from contact_map import ContactFrequency

traj_contacts = ContactFrequency(traj)

"""
Explanation: Exporting data to other tools
End of explanation
"""


traj_contacts.residue_contacts.sparse_matrix

"""
Explanation: Exporting contact data
Contact Map Explorer makes it easy for you to get your contact data into other formats, so that you can use the tools you're already familiar with to perform your analysis. Note that the residues and atoms of these report as integers, so what you have is the index of the object in the MDTraj topology,
SciPy sparse matrix
End of explanation
"""


traj_contacts.residue_contacts.df

"""
Explanation: Pandas dataframe
End of explanation
"""


import networkx as nx
graph = traj_contacts.residue_contacts.to_networkx()
nx.draw(graph)

"""
Explanation: NetworkX graph
This requires separately installing NetworkX, which does not come with Contact Map Explorer in a default installation.
End of explanation
"""


counter = traj_contacts.residue_contacts.counter
dict(list(counter.items())[:5])  # to illustrate a few

"""
Explanation: Direct access to the Counter
For ContactFrequency and ContactDifference you can directly access the internal collections.Counter object that stores the contact frequency. This Counter is a mapping of a frozenset of the MDTraj indices (ints) to the frequency.
End of explanation
"""


traj_contacts.save_to_file("contacts.p")

"""
Explanation: Python pickle file
The approaches listed above give you a way to export the contact matrix. But when you do that, you lose the information connecting residue contacts to atom contacts. Sometimes you just want to export your data so you can load it up later to continue your analysis. In this case, the save_to_file method, which uses Python's pickle module, is the best tool.
End of explanation
"""


ContactFrequency.from_file("contacts.p")

"""
Explanation: This is also allows you to reload your data. Because the other approaches can't preserve all the information we store, we cannot recreate our objects from, for example, a SciPy sparse matrix.
End of explanation
"""


json_str = traj_contacts.to_json()

ContactFrequency.from_json(json_str)

"""
Explanation: JSON string
You can also save all the information, including information connecting residue contacts to atom contacts, as a JSON string. This is useful if you need to transfer a ContactFrequency to another machine during a parallelized analysis. It can also be written to disk, though the pickle format is likely to be more efficient.
End of explanation
"""


