def fetch_cluster_pairs(data, samfile, chrom, rstart, rend):
<EOL>rdict = {}<EOL>clust = []<EOL>iterreg = samfile.fetch(chrom, rstart, rend)<EOL>for read in iterreg:<EOL><INDENT>if read.qname not in rdict:<EOL><INDENT>rdict[read.qname] = [read]<EOL><DEDENT>else:<EOL><INDENT>rdict[read.qname].append(read)<EOL><DEDENT><DEDENT>sfunc = lambda x: int(x.split("<STR_LIT>")[<NUM_LIT:1>].split("<STR_LIT:;>")[<NUM_LIT:0>])<EOL>rkeys = sorted(rdict.keys(), key=sfunc, reverse=True)<EOL>try:<EOL><INDENT>read1, read2 = rdict[rkeys[<NUM_LIT:0>]]<EOL><DEDENT>except ValueError:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>poss = read1.get_reference_positions() + read2.get_reference_positions()<EOL>seed_r1start = min(poss)<EOL>seed_r2end = max(poss)<EOL>reads_overlap = False<EOL>if read1.is_reverse:<EOL><INDENT>if read2.aend > read1.get_blocks()[<NUM_LIT:0>][<NUM_LIT:0>]:<EOL><INDENT>reads_overlap = True<EOL>seq = read2.seq + "<STR_LIT>" + revcomp(read1.seq)<EOL><DEDENT>else:<EOL><INDENT>seq = read2.seq + "<STR_LIT>" + read1.seq<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if read1.aend > read2.get_blocks()[<NUM_LIT:0>][<NUM_LIT:0>]:<EOL><INDENT>reads_overlap = True<EOL>seq = read1.seq + "<STR_LIT>" + revcomp(read2.seq)<EOL><DEDENT>else:<EOL><INDENT>seq = read1.seq + "<STR_LIT>" + read2.seq<EOL><DEDENT><DEDENT>size = sfunc(rkeys[<NUM_LIT:0>])<EOL>clust.append("<STR_LIT>".format(chrom, seed_r1start, seed_r2end, size, seq))<EOL>if len(rkeys) > <NUM_LIT:1>:<EOL><INDENT>for key in rkeys[<NUM_LIT:1>:]:<EOL><INDENT>skip = False<EOL>try:<EOL><INDENT>read1, read2 = rdict[key]<EOL><DEDENT>except ValueError:<EOL><INDENT>read1 = rdict[key][<NUM_LIT:0>]<EOL>read2 = read1<EOL>skip = True<EOL><DEDENT>poss = read1.get_reference_positions() + read2.get_reference_positions()<EOL>minpos = min(poss)<EOL>maxpos = max(poss)<EOL>if read1.has_tag("<STR_LIT>") or read2.has_tag("<STR_LIT>"):<EOL><INDENT>skip = True<EOL><DEDENT>if (abs(minpos - seed_r1start) < <NUM_LIT:50>) and(abs(maxpos - seed_r2end) < <NUM_LIT:50>) and(not skip):<EOL><INDENT>if read1.is_reverse:<EOL><INDENT>if read2.aend > read1.get_blocks()[<NUM_LIT:0>][<NUM_LIT:0>]:<EOL><INDENT>reads_overlap = True<EOL>seq = read2.seq + "<STR_LIT>" + revcomp(read1.seq)<EOL><DEDENT>else:<EOL><INDENT>seq = read2.seq + "<STR_LIT>" + read1.seq<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if read1.aend > read2.get_blocks()[<NUM_LIT:0>][<NUM_LIT:0>]:<EOL><INDENT>reads_overlap = True<EOL>seq = read1.seq + "<STR_LIT>" + revcomp(read2.seq)<EOL><DEDENT>else:<EOL><INDENT>seq = read1.seq + "<STR_LIT>" + read2.seq<EOL><DEDENT><DEDENT>size = sfunc(key)<EOL>clust.append("<STR_LIT>".format(chrom, minpos, maxpos, size, seq))<EOL><DEDENT>else:<EOL><INDENT>pass<EOL><DEDENT><DEDENT><DEDENT>if reads_overlap:<EOL><INDENT>if data._hackersonly["<STR_LIT>"]:<EOL><INDENT>clust = merge_after_pysam(data, clust)<EOL><DEDENT><DEDENT>return clust<EOL>
Builds a paired cluster from the refmapped data.