Skip to content

Commit 0b6d601

Browse files
committed
Merge branch 'sync_from_bench'
Conflicts: .gitmodules README.md Snakefile config.yaml py/freddie_cluster.py py/freddie_isoforms.py py/freddie_segment.py py/freddie_split.py
2 parents 53fbaaf + 4682693 commit 0b6d601

4 files changed

Lines changed: 439 additions & 140 deletions

File tree

py/freddie_cluster.py

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
from multiprocessing import Pool
44

5-
from math import ceil
5+
from math import ceil,floor
66
import argparse
77
import re
88
import glob
@@ -67,6 +67,11 @@ def parse_args():
6767
type=int,
6868
default=3,
6969
help="Minimum isoform size in terms of number supporting reads. Default {}".format(3))
70+
parser.add_argument("-mi",
71+
"--max-ilp",
72+
type=int,
73+
default=1500,
74+
help="Maximum number of unique reads allowed for an ILP instance. ILP instances with more reads will have their input broken into evenly sized problems, each with less than the max. Default {}".format(1500))
7075
parser.add_argument("-to",
7176
"--timeout",
7277
type=int,
@@ -99,6 +104,12 @@ def parse_args():
99104

100105
return args
101106

107+
def split_list_evenly(l, m):
108+
p = ceil(len(l)/m)
109+
s = ceil(len(l)/p)
110+
for idx in range(0, p*s, s):
111+
yield l[idx:idx+s]
112+
102113

103114
def read_segment(segment_tsv):
104115
tints = dict()
@@ -136,9 +147,9 @@ def read_segment(segment_tsv):
136147
for p in poly_gap_prog.findall(re_dict['gaps'])},
137148
)
138149
read_rep_key = [d for d in re_dict['data'].replace('2', '0')]
139-
read_rep_key += ['.{}'.format(g[2] if int(g[2]) > 3 else 0)
150+
read_rep_key += ['.{}'.format(g[2] if int(g[2]) > 10 else 0)
140151
for g in internal_gap_prog.findall(re_dict['gaps'])]
141-
read_rep_key += ['.{}{}'.format(p[0][0], p[2] if int(p[2]) > 3 else 0)
152+
read_rep_key += ['.{}{}'.format(p[0][0], p[2] if int(p[2]) > 10 else 0)
142153
for p in poly_gap_prog.findall(re_dict['gaps'])]
143154
read_rep_key = ''.join(read_rep_key)
144155
tind_id = read['tint']
@@ -179,7 +190,7 @@ def garbage_cost_exons(I):
179190
# TO DO: think about better ways to define the cost to assign to the garbagte isoform
180191

181192

182-
def partition_reads(tint):
193+
def partition_reads(tint, maximum_ilp_size):
183194
reads = tint['reads']
184195
read_reps = tint['read_reps']
185196
I = tint['ilp_data']['I']
@@ -229,18 +240,18 @@ def partition_reads(tint):
229240
for c in components.connected_components(G):
230241
rids = list()
231242
incomp = list()
232-
c = list(c)
233-
for idx, i in enumerate(c):
234-
rids.extend(unique_data[i][1])
235-
# for j in c[idx+1:]:
236-
# i,j = min(i,j),max(i,j)
237-
# assert i<j
238-
# if G.has_edge(i,j):
239-
# continue
240-
# for rid_1 in unique_data[i][1]:
241-
# for rid_2 in unique_data[j][1]:
242-
# incomp.append((rid_1,rid_2))
243-
tint['partitions'].append((rids, incomp))
243+
for c in split_list_evenly(list(c), maximum_ilp_size):
244+
for idx, i in enumerate(c):
245+
rids.extend(unique_data[i][1])
246+
for j in c[idx+1:]:
247+
i,j = min(i,j),max(i,j)
248+
assert i<j
249+
if G.has_edge(i,j):
250+
continue
251+
for rid_1 in unique_data[i][1]:
252+
for rid_2 in unique_data[j][1]:
253+
incomp.append((rid_1,rid_2))
254+
tint['partitions'].append((rids, incomp))
244255

245256

246257
def preprocess_ilp(tint, ilp_settings):
@@ -684,7 +695,7 @@ def output_isoforms(tint, out_file):
684695
output.append(str((reads[ridx]['partition'])))
685696
output.append(str((reads[ridx]['poly_tail_category'])))
686697
output.append('*')
687-
output.append('*')
698+
output.append(''.join(map(str, reads[ridx]['data'])))
688699
exon_strs = [str(x) for x in reads[ridx]['data']]
689700
for (j1, j2), l in reads[ridx]['gaps'].items():
690701
exon_strs[j1] += '({})'.format(l)
@@ -702,6 +713,7 @@ def cluster_tint(cluster_args):
702713
tint_id,
703714
ilp_settings,
704715
min_isoform_size,
716+
max_ilp,
705717
logs_dir,
706718
) = cluster_args
707719
tints = read_segment(
@@ -715,7 +727,7 @@ def cluster_tint(cluster_args):
715727
timeout_log = open(
716728
'{}/{}/timeout.log'.format(logs_dir, tint['id']), 'w+')
717729
preprocess_ilp(tint, ilp_settings)
718-
partition_reads(tint)
730+
partition_reads(tint, max_ilp)
719731
# print('# Paritions ({}) sizes: {}\n'.format(
720732
# len(tint['partitions']), [len(p) for p in tint['partitions']]))
721733
tint['isoforms'] = list()
@@ -811,16 +823,17 @@ def main():
811823
tint_id,
812824
ilp_settings,
813825
args.min_isoform_size,
826+
args.max_ilp,
814827
'{}/{}'.format(args.logs_dir, contig) if args.logs_dir != None else None,
815828
])
816829
if args.threads > 1:
817830
p = Pool(args.threads)
818-
for idx, tint_id in enumerate(p.imap_unordered(cluster_tint, cluster_args, chunksize=5)):
819-
print('[freddie_cluster] Done with {}/{} tints ({:.1%})'.format(idx,
831+
for idx, tint_id in enumerate(p.imap_unordered(cluster_tint, cluster_args, chunksize=1)):
832+
print('[freddie_cluster] Done with {}/{} tints ({:.1%})'.format(idx+1,
820833
len(cluster_args), idx/len(cluster_args)))
821834
else:
822835
for idx, tint_id in enumerate(map(cluster_tint, cluster_args)):
823-
print('[freddie_cluster] Done with {}/{} tints ({:.1%})'.format(idx,
836+
print('[freddie_cluster] Done with {}/{} tints ({:.1%})'.format(idx+1,
824837
len(cluster_args), idx/len(cluster_args)))
825838

826839

py/freddie_isoforms.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def run_consensus(consensus_args):
5656
majority_threshold,
5757
correction_window,
5858
) = consensus_args
59+
print(f'Building isoforms for contig {contig}')
5960
segments, reads, isoforms = read_cluster(cluster_tsv)
6061
isoforms_cons(isoforms, segments, reads)
6162
read_split(split_tsv, reads)
@@ -203,16 +204,31 @@ def isoforms_cons(isoforms, segments, reads):
203204
for isoform_key, isoform in isoforms.items():
204205
chrom, tint, _, _ = isoform_key
205206
cons = [0 for _ in segments[(chrom, tint)]]
207+
cov = [0 for _ in segments[(chrom, tint)]]
206208
M = len(segments[(chrom, tint)])
207209
N = len(isoform['rids'])
208210
tails = {'N': 0, 'S': 0, 'E': 0}
209211
for rid in isoform['rids']:
210212
read = reads[rid]
211213
assert len(read['data']) == M, (M, isoform_key, read)
212-
for j in range(M):
213-
cons[j] += read['data'][j] == '1'
214-
tails[read['tail']] += 1
215-
cons = [x/N > 0.3 for x in cons]
214+
if not '1' in read['data']:
215+
continue
216+
if read['tail'] == 'S':
217+
first = 0
218+
else:
219+
first = read['data'].index('1')
220+
if read['tail'] == 'S':
221+
last = M - 1
222+
else:
223+
last = M - 1 - read['data'][::-1].index('1')
224+
assert 0<=first<=last<M,f'first {first}, last {last}, M {M}'
225+
for j in range(first,last+1):
226+
X = read['data'][j] == '1'
227+
cons[j] += X
228+
cov[j] += 1
229+
tails[read['tail']] += 1
230+
231+
cons = [x/c > 0.5 if x >= 3 else False for x,c in zip(cons,cov) ]
216232
if not True in cons:
217233
continue
218234
if tails['S'] > tails['E']:

0 commit comments

Comments
 (0)