Skip to content

Commit ec7121e

Browse files
committed
Fix autoflip for real
1 parent f7cb6a9 commit ec7121e

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

cooler/_reader.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ def __init__(self, filepath, chromsizes, bins, map=map, **kwargs):
226226
for chrom in self.contigs:
227227
if chrom not in file_contigs:
228228
warnings.warn(
229-
"Did not find contig '{}' in contact list file.".format(chrom))
229+
"Did not find contig " +
230+
" '{}' in contact list file.".format(chrom))
230231
self.contigs = [ctg for ctg in self.contigs if ctg in file_contigs]
231232

232233
def __getstate__(self):
@@ -328,8 +329,9 @@ def __init__(self, filepath, chromsizes, bins, map=map, **kwargs):
328329
[b.split('|') for b in f.get_blocknames()]))
329330

330331
self.C1 = f.get_chr1_col()
332+
self.C2 = f.get_chr2_col()
331333
self.P1 = f.get_startpos1_col()
332-
self.P2 = f.get_startpos2_col()
334+
self.P2 = f.get_startpos2_col()
333335

334336
for chrom in self.contigs:
335337
if chrom not in file_contigs:
@@ -366,25 +368,34 @@ def aggregate(self, chrom1):
366368
chrom_abspos = self.chrom_abspos
367369
start_abspos = self.start_abspos
368370
C1 = self.C1
371+
C2 = self.C2
369372
P1 = self.P1
370373
P2 = self.P2
371374
these_bins = self.bins_grouped.get_group(chrom1)
372375

373-
rows = []
374-
375376
f = pypairix.open(filepath, 'r')
376-
accumulator = Counter()
377-
i = self.contigs.index(chrom1)
378-
remaining = self.contigs[i:]
377+
cid1 = self.contigs.index(chrom1)
378+
remaining = self.contigs[cid1:]
379379

380+
accumulator = Counter()
381+
rows = []
380382
for bin1_id, bin1 in these_bins.iterrows():
381383
chrom1 = bin1.chrom
382-
for cid2, chrom2 in enumerate(remaining, i):
384+
385+
for cid2, chrom2 in enumerate(remaining, cid1):
383386
chrom2_size = chromsizes[chrom2]
387+
388+
trans = chrom1 != chrom2
389+
384390
for line in f.query2D(
385391
chrom1, bin1.start, bin1.end,
386392
chrom2, 0, chrom2_size, 1):
387-
pos2 = int(line[P2]) if line[C1] == chrom1 else int(line[P1])
393+
394+
if trans and line[C2] == chrom1:
395+
pos2 = int(line[P1])
396+
else:
397+
pos2 = int(line[P2])
398+
388399
if binsize is None:
389400
lo, hi = chrom_binoffset[cid2], chrom_binoffset[cid2+1]
390401
bin2_id = lo + np.searchsorted(
@@ -393,9 +404,12 @@ def aggregate(self, chrom1):
393404
side='right') - 1
394405
else:
395406
bin2_id = chrom_binoffset[cid2] + (pos2 // binsize)
407+
396408
accumulator[bin2_id] += 1
409+
397410
if not accumulator:
398411
continue
412+
399413
rows.append(
400414
pandas.DataFrame({
401415
'bin1_id': bin1_id,
@@ -404,6 +418,7 @@ def aggregate(self, chrom1):
404418
columns=['bin1_id', 'bin2_id', 'count'])
405419
.sort_values('bin2_id')
406420
)
421+
407422
accumulator.clear()
408423

409424
logger.info(chrom1)

0 commit comments

Comments
 (0)