-
Notifications
You must be signed in to change notification settings - Fork 0
/
homstrad-matt-fragbag
executable file
·186 lines (168 loc) · 7.47 KB
/
homstrad-matt-fragbag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python2.7
from collections import defaultdict
import csv
import glob
import os.path as path
import numpy as np
import pybcb as bcb
import pybcb.flags as flags
flags.use_all('frag-lib',
'cpu', 'tmp-dir', 'results-dir',
'ignore-cache', 'no-cache')
flags.use('homstrad-dir', (
lambda: flags.add(dest='homstrad_dir', type=str,
help='The directory containing HOMSTRAD with PDB data.'),
flags.verify_path,
))
flags.use('seq-align', (
lambda: flags.add('--seq-align', type=str, default='bent',
choices=['bent', 'unbent', 'homstrad'],
help='The sequence alignment to use to find pairwise '
'corresponding fragments.'),
None,
))
flags.use('struct-align', (
lambda: flags.add('--struct-align', type=str, default='bent',
choices=['bent', 'unbent', 'homstrad', 'none'],
help='The structure alignment to use to find pairwise '
'corresponding fragments. If "none" is used, then '
'fragments are compared in sequence space.'),
None,
))
flags.use('disagreements', (
lambda: flags.add('--disagreements', action='store_true',
help='When set, the fragment disagreements will be '
'written in CSV format to the results directory.'),
None,
))
flags.init()
bcb.set_exp_dir(flags.config.tmp_dir)
bcb.set_results_dir(flags.config.results_dir)
bcb.make()
flib_name = path.basename(flags.config.frag_lib)
align_combo = '%s-%s' % (flags.config.seq_align, flags.config.struct_align)
align_dir = path.join(flib_name, align_combo)
bcb.makedirs(bcb.ejoin('matt'))
bcb.makedirs(bcb.ejoin(align_dir))
bcb.makedirs(bcb.rjoin(flib_name))
if flags.config.disagreements:
bcb.makedirs(bcb.rjoin(align_dir))
homstrad_pdbs = glob.glob(path.join(flags.config.homstrad_dir, '*', '*.pdb'))
# Outline of experiment:
#
# 1. Run Matt bent alignment on the first two proteins in a Homstrad family.
#
# 2. In addition to the Homstrad sequence alignment (ali file), Matt produces
# two more sequence alignments: unbent and bent. Depending upon experiment
# parameters, use sequence alignment to find all pairwise contiguous
# fragments with size equal to the fragment size of the chosen fragment
# library. For each pair, compute the best fragment corresponding to each
# set of aligned alpha-carbon ATOM records.
#
# 3. Organize data by only saving sections with unequivalent best fragments.
# Also, provide a coverage statistic for each family and for all families:
# #fragments same / #all fragments.
# Finally, provide a co-occurrence matrix with frequency counts for each
# pair of fragments that show up in a labeling.
# Step 1 - Run matt on first two entries in each Homstrad family if we're
# using bent or unbent in either alignment.
if flags.config.seq_align in ('bent', 'unbent') \
or flags.config.struct_align in ('bent', 'unbent'):
for hpdb in homstrad_pdbs:
family = path.basename(path.dirname(hpdb))
bcb.makedirs(bcb.ejoin('matt', family))
prefix = bcb.ejoin('matt', family, family)
files = [
'%s.fasta' % prefix,
'%s.pdb' % prefix,
'%s_bent.fasta' % prefix,
'%s_bent.pdb' % prefix,
]
bcb.cached_cmd(files,
'matt', '-b', '-f', 'fasta,pdb',
'-o', prefix,
'%s:A' % hpdb,
'%s:B' % hpdb)
# Step 2 - Find best fragment for each contiguous pairwise fragment.
# The output for each Homstrad family is a csv file with the following columns:
# start1, end1, start2, end2, frag1, frag2, frag_rmsd
# The start and end columns are in terms of the residue number in the PDB file.
# The frag columns are just fragment numbers into the given fragment library.
# Finally, frag_rmsd is the RMSD between the two fragments given.
for hpdb in homstrad_pdbs:
family = path.basename(path.dirname(hpdb))
eoutcsv = bcb.ejoin(align_dir, '%s.tsv' % family)
routcsv = bcb.rjoin(align_dir, '%s.tsv' % family)
if flags.config.seq_align == 'unbent':
seq_align = bcb.ejoin('matt', family, '%s.fasta' % family)
elif flags.config.seq_align == 'bent':
seq_align = bcb.ejoin('matt', family, '%s_bent.fasta' % family)
elif flags.config.seq_align == 'homstrad':
seq_align = path.join(path.dirname(hpdb), '%s.ali' % family)
if flags.config.struct_align == 'unbent':
struct_align = bcb.ejoin('matt', family, '%s.pdb' % family)
elif flags.config.struct_align == 'bent':
struct_align = bcb.ejoin('matt', family, '%s_bent.pdb' % family)
elif flags.config.struct_align == 'homstrad':
struct_align = hpdb
elif flags.config.struct_align == 'none':
struct_align = None
if struct_align is None:
bcb.cached_cmd([eoutcsv],
'best-pairwise-seqfrag', '--all-fragments',
flags.config.frag_lib, seq_align, eoutcsv)
if flags.config.disagreements:
bcb.cached_cmd([routcsv],
'best-pairwise-seqfrag',
flags.config.frag_lib, seq_align, routcsv)
else:
bcb.cached_cmd([eoutcsv],
'best-pairwise-frag', '--all-fragments',
flags.config.frag_lib, seq_align, struct_align, eoutcsv)
if flags.config.disagreements:
bcb.cached_cmd([routcsv],
'best-pairwise-frag',
flags.config.frag_lib, seq_align,
struct_align, routcsv)
# Step 3 - Process the CSV data in the experiment directory to produce coverage
# statistics for each family and for all families.
fcoverage = bcb.rjoin(flib_name, '%s.tsv' % align_combo)
fmatrix = bcb.rjoin(flib_name, '%s-matrix.tsv' % align_combo)
def compute_coverage():
matrix = {}
def add_pair(f1, f2):
f1, f2 = sorted((int(f1), int(f2)))
if f1 not in matrix:
matrix[f1] = defaultdict(int)
matrix[f1][f2] += 1
coverage = {}
allMatch, allRows = 0, 0
for bestFrags in glob.glob(bcb.ejoin(align_dir, '*.tsv')):
matchRows, totalRows = 0, 0
for row in csv.DictReader(open(bestFrags), delimiter='\t'):
totalRows += 1
if row['frag1'] == row['frag2']:
matchRows += 1
add_pair(row['frag1'], row['frag2'])
family = path.splitext(path.basename(bestFrags))[0]
if totalRows == 0:
coverage[family] = 1.0
else:
coverage[family] = float(matchRows) / float(totalRows)
allMatch += matchRows
allRows += totalRows
with open(fcoverage, 'w+') as w:
print >> w, 'family\tcoverage'
print >> w, 'all\t%f' % (float(allMatch) / float(allRows))
print >> w, 'median\t%f' % np.median(coverage.values())
print >> w, 'mean\t%f' % np.mean(coverage.values())
print >> w, 'std\t%f' % np.std(coverage.values())
for family in sorted(coverage, key=lambda k: coverage[k]):
print >> w, '%s\t%s' % (family, coverage[family])
with open(fmatrix, 'w+') as w:
frags = sorted(matrix.keys())
print >> w, '#\t%s' % ('\t'.join(map(str, frags)))
for f1 in frags:
freqs = [matrix[f1][f2] for f2 in frags]
print >> w, '%d\t%s' % (f1, '\t'.join(map(str, freqs)))
bcb.cached([fcoverage, fmatrix], compute_coverage)