Skip to content

Commit

Permalink
Merge branch '1.1.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelglenister committed Feb 5, 2020
2 parents d1e16d2 + 978b6a4 commit 7131f44
Show file tree
Hide file tree
Showing 22 changed files with 443 additions and 776 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ script:
- ./test_L.sh
- ./test_CM.sh
- ./test_DCC.sh
- ./test_PRS.sh
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<img src="https://travis-ci.org/RUBi-ZA/MD-TASK.svg?branch=master" align="right">

# MD-TASK

Tool suite for analysing molecular dynamics trajectories using network analysis and PRS.
A suite of Python scripts that have been developed to analyze molecular dynamics trajectories. Detailed documentation on how to install and use MD-TASK can be found on our [ReadTheDocs](http://md-task.readthedocs.io/en/latest/index.html) site.

## Introduction
### Project Status
| Branch | Build Status | Code Style | Coverage |
|--------|---------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------|
| Master (1.0.x) | [![Build Status](https://travis-ci.org/RUBi-ZA/MD-TASK.svg?branch=master)](https://travis-ci.org/RUBi-ZA/MD-TASK) | [![Codacy Badge](https://api.codacy.com/project/badge/Grade/f81d8d8016094467a836dd3ef1c42908)](https://www.codacy.com/app/davidbrownza/MD-TASK?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=RUBi-ZA/MD-TASK&amp;utm_campaign=Badge_Grade) | n/a |
| 1.1.x | [![Build Status](https://travis-ci.org/RUBi-ZA/MD-TASK.svg?branch=1.1.x)](https://travis-ci.org/RUBi-ZA/MD-TASK) | [![Codacy Badge](https://api.codacy.com/project/badge/Grade/f81d8d8016094467a836dd3ef1c42908?branch=1.1.x)](https://www.codacy.com/app/davidbrownza/MD-TASK?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=RUBi-ZA/MD-TASK&amp;utm_campaign=Badge_Grade) | n/a |
| 1.0.x | [![Build Status](https://travis-ci.org/RUBi-ZA/MD-TASK.svg?branch=1.0.x)](https://travis-ci.org/RUBi-ZA/MD-TASK) | [![Codacy Badge](https://api.codacy.com/project/badge/Grade/f81d8d8016094467a836dd3ef1c42908?branch=1.0.x)](https://www.codacy.com/app/davidbrownza/MD-TASK?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=RUBi-ZA/MD-TASK&amp;utm_campaign=Badge_Grade) | n/a |

MD-TASK consists of a suite of Python scripts that have been developed to analyze molecular dynamics trajectories. Detailed documentation can be found on our [ReadTheDocs](http://md-task.readthedocs.io/en/latest/index.html) site.

Expand Down
49 changes: 7 additions & 42 deletions avg_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@

import numpy as np

from datetime import datetime
from lib.cli import CLI
from lib.utils import Logger

from lib.utils import *

import os, sys, argparse, matplotlib
import sys, argparse, matplotlib

matplotlib.use('Agg')
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -58,7 +57,6 @@ def plot_graph(network, err=None, start_x=1, color="black", ecolor="red", title=


def plot_graph_split(split_pos, avg_matrix, std, initial_x_1, initial_x_2, title_1, title_2, x_label, y_label, ylim=None):
num_nodes = avg_matrix.shape[0]
pos = int(split_pos)

avg_matrix_1,avg_matrix_2 = split_array(avg_matrix, pos)
Expand All @@ -81,10 +79,9 @@ def plot_graph_split(split_pos, avg_matrix, std, initial_x_1, initial_x_2, title

def main(args):
prefix = args.prefix
start_index = args.initial_x

if not args.data_type in ["BC", "delta-BC", "L", "delta-L"]:
log("Unrecognized data type. Exiting...\n")
log.error("Unrecognized data type. Exiting...\n")
sys.exit(1)

# average BC
Expand Down Expand Up @@ -114,7 +111,7 @@ def main(args):
np.savetxt("%s_avg.dat" % prefix, avg_matrix)

if args.generate_plots:
log("Generating graph: %s_avg.png\n" % prefix)
log.info("Generating graph: %s_avg.png\n" % prefix)

ylim = None
if args.y_max is not None and args.y_min is not None:
Expand All @@ -136,26 +133,13 @@ def main(args):
plt.close()


silent = False
stream = sys.stdout

def log(message):
global silent
global stream

if not silent:
stream.write(message)

log = Logger()

if __name__ == "__main__":

#parse cmd arguments
parser = argparse.ArgumentParser()

#standard arguments for logging
parser.add_argument("--silent", help="Normalizes the values (Delta L/L)", action='store_true', default=False)
parser.add_argument("--log-file", help="Output log file (default: standard output)", default=None)

#custom arguments
parser.add_argument("--data", help="The .dat files that will be averaged", nargs="*")
parser.add_argument("--data-type", help="Type of data - BC/delta-BC/L/delta-L")
Expand Down Expand Up @@ -183,23 +167,4 @@ def log(message):

args = parser.parse_args()

#set up logging
silent = args.silent

if args.log_file:
stream = open(args.log_file, 'w')

start = datetime.now()
log("Started at: %s\n" % str(start))

#run script
main(args)

end = datetime.now()
time_taken = format_seconds((end - start).seconds)

log("Completed at: %s\n" % str(end))
log("- Total time: %s\n" % str(time_taken))

#close logging stream
stream.close()
CLI(parser, main, log)
75 changes: 12 additions & 63 deletions calc_correlation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,22 @@
# Author: Caroline Ross
# Date: 17-11-2016



from datetime import datetime

from matplotlib import cm

import mdtraj as md

import numpy as np

from lib.utils import *
from lib.cli import CLI
from lib.utils import Logger
from lib.trajectory import load_trajectory

import os, sys, argparse, traceback, math, matplotlib
import argparse, math, matplotlib

matplotlib.use('Agg')
import matplotlib.pyplot as plt



def parse_traj(traj, topology=None, step=1, selected_atoms=["CA"], lazy_load=False):
if lazy_load:
traj = MDIterator(traj, top=topology, stride=step)
else:
traj = md.load(traj, top=topology)[::step]
traj = load_trajectory(traj, topology, step, lazy_load)[0]

residues = {}

Expand Down Expand Up @@ -92,11 +84,10 @@ def correlate(residues):
return correlation



def plot_map(correlation, title, output_prefix):
M = np.array(correlation)

fig, ax = plt.subplots()
ax = plt.subplots()[1]
colors = [('white')] + [(cm.jet(i)) for i in xrange(40,250)]

new_map = matplotlib.colors.LinearSegmentedColormap.from_list('new_map', colors, N=300)
Expand Down Expand Up @@ -124,7 +115,7 @@ def plot_map(correlation, title, output_prefix):
plt.xlabel('Residue Index', fontsize=12)
plt.ylabel("Residue Index", fontsize=12)

cbar = plt.colorbar(heatmap, orientation="vertical")
plt.colorbar(heatmap, orientation="vertical")
plt.savefig('%s.png' % output_prefix, dpi=300)
plt.close('all')

Expand All @@ -143,40 +134,24 @@ def print_correlation(correlation, output_prefix):


def main(args):
log("Preparing a trajectory matrix...\n")
log.info("Preparing a trajectory matrix...\n")
traj_matrix = parse_traj(args.trajectory, args.topology, args.step, lazy_load=args.lazy_load)

log("Correlating...\n")
log.info("Correlating...\n")
correlation = correlate(traj_matrix)

log("Plotting heat map...\n")
log.info("Plotting heat map...\n")
plot_map(correlation, args.title, args.prefix)
print_correlation(correlation, args.prefix)



silent = False
stream = sys.stdout

def log(message):
global silent
global stream

if not silent:
stream.write(message)
stream.flush()


log = Logger()

if __name__ == "__main__":

#parse cmd arguments
parser = argparse.ArgumentParser()

#standard arguments for logging
parser.add_argument("--silent", help="Turn off logging", action='store_true', default=False)
parser.add_argument("--log-file", help="Output log file (default: standard output)", default=None)

#custom arguments
parser.add_argument("--trajectory", help="Trajectory file")
parser.add_argument("--topology", help="Referencce PDB file (must contain the same number of atoms as the trajectory)")
Expand All @@ -186,30 +161,4 @@ def log(message):
parser.add_argument("--title", help="Title for heatmap", default="Protein")
parser.add_argument("--prefix", help="Prefix for output files", default="correlation")

args = parser.parse_args()

#set up logging
silent = args.silent

if args.log_file:
stream = open(args.log_file, 'w')

start = datetime.now()
log("Started at: %s\n" % str(start))

#run script
main(args)

end = datetime.now()
time_taken = format_seconds((end - start).seconds)

log("Completed at: %s\n" % str(end))
log("- Total time: %s\n" % str(time_taken))

#close logging stream
stream.close()





CLI(parser, main, log)
96 changes: 96 additions & 0 deletions calc_delta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env python
#
# Calculate the change in the valuses (BC or L) of each residue in a protein over the
# course of an MD simulation
#
# Script distributed under GNU GPL 3.0
#
# Author: David Brown
# Date: 17-11-2016

from natsort import natsorted

from lib.cli import CLI
from lib.utils import Logger
from lib.strategies import normalization

import numpy as np

import sys, argparse, matplotlib

matplotlib.use('Agg')
import matplotlib.pyplot as plt


def calc_delta(reference_file, alternative_files, normalizer, generate_plots=False):
reference = np.loadtxt(reference_file)
num_nodes = reference.shape[0]

label = normalizer.get_label()

alternatives = natsorted(alternative_files)

log.info("Calculating %s for %d networks...\n" % (label, len(alternatives)))

for i, alternative in enumerate(alternatives):
log.info("Calculating %s (%d/%d)\r" % (label, i + 1, len(alternatives)))

title = ".".join(alternative.split(".")[:-1])
alternative = np.loadtxt(alternative)

difference = alternative - reference
difference = normalizer.normalize(difference, reference)
prefix = "%s_%s_delta_%s" % (title, normalizer.get_prefix(), normalizer.matrix_type)

np.savetxt("%s.dat" % prefix, difference)

if generate_plots:
node_axis = range(1, num_nodes + 1)
plt.plot(node_axis, difference)
plt.axhline(0, color='black')
plt.title("%s %s" % (title, label), fontsize=18)
plt.xlabel('Residue Numbers', fontsize=16)
plt.ylabel(label, fontsize=16)
plt.savefig("%s.png" % prefix, dpi=300, bbox_inches="tight")
plt.close()

log.info("\n")


def get_normalizer(matrix_type, mode):
type_map = {
"L": "standard",
"BC": "plusone"
}

if matrix_type not in type_map:
log.info("ERROR: --matrix-type must be specified and must be either 'BC' or 'L'")
sys.exit(1)

normalization_mode = mode if mode else type_map[matrix_type]

return getattr(normalization, normalization_mode, "standard")(matrix_type)


def main(args):
if args.normalize:
normalizer = get_normalizer(args.matrix_type, args.normalization_mode)
else:
normalizer = normalization.none(args.matrix_type)

calc_delta(args.reference, args.alternatives, normalizer, args.generate_plots)


log = Logger()

if __name__ == "__main__":
parser = argparse.ArgumentParser()

parser.add_argument("--matrix-type", help="The type of values in the matrices i.e. BC or L", default=None)
parser.add_argument("--reference", help="The reference matrix (.dat)")
parser.add_argument("--alternatives", help="The alternative matrices (.dat)", nargs="*")
parser.add_argument("--normalize", help="Normalizes the values", action='store_true', default=False)
parser.add_argument('--normalization-mode', help="Method used to normalize (default for L = standard, default for BC = plusone)", default=None)
parser.add_argument("--generate-plots", help="Plot results - without setting this flag, no graph will be generated", action='store_true', default=False)

CLI(parser, main, log)

0 comments on commit 7131f44

Please sign in to comment.