def test_identity(self): i1, i2, i3, i4 = P.closed(0, 1), P.openclosed(0, 1), P.closedopen(0, 1), P.open(0, 1) assert P.from_string(P.to_string(i1), int) == i1 assert P.from_string(P.to_string(i2), int) == i2 assert P.from_string(P.to_string(i3), int) == i3 assert P.from_string(P.to_string(i4), int) == i4
def get_compatible_isoforms_stitcher(mol_list, isoform_dict_json, refskip_dict_json, h): isoform_dict = P.IntervalDict() for i, s in isoform_dict_json.items(): isoform_dict[P.from_string(i, conv=int)] = set(s.split(',')) refskip_dict = P.IntervalDict() for i, s in refskip_dict_json.items(): refskip_dict[P.from_string(i, conv=int)] = set(s.split(',')) compatible_isoforms_trie = dict() new_mol_list = [] for success, m in mol_list: if not success: if type(m) is str: new_mol_list.append((success, m)) else: new_mol_list.append((success, m.to_string())) continue mol = pysam.AlignedRead.fromstring(m, h) i = interval(intervals_extract(mol.get_reference_positions())) refskip_cigar = [ t[0] for t in mol.cigartuples if t[1] > 0 and t[0] in [2, 3] ] blocks = mol.get_blocks() j = [] for n in range(len(blocks) - 1): if refskip_cigar[n] == 3: j.append((blocks[n][1], blocks[n + 1][0])) j = interval(j) set_list = [ s for k, s in isoform_dict.get(i, default={'intronic'}).items() if len(list(P.iterate(k, step=1))) > 4 ] set_refskip_list = [ s for k, s in refskip_dict.get(j, default={'intronic'}).items() if len(list(P.iterate(k, step=1))) > 4 ] if {'intronic'} in set_list: if len(set_list) > 1: del set_list[set_list.index({'intronic'})] if {'intronic'} in set_refskip_list: if len(set_refskip_list) > 1: del set_refskip_list[set_refskip_list.index({'intronic'})] try: if len(set_refskip_list) > 0: mol.set_tag( 'CT', ','.join( list( set.intersection(*set_list).intersection( *set_refskip_list)))) else: mol.set_tag('CT', ','.join(list(set.intersection(*set_list)))) new_mol_list.append((success, mol.to_string())) except: continue return new_mol_list
def test_parameters(self): i1, i2, i3, i4 = '<"0"-"1">', '<!"0"-"1">', '<"0"-"1"!>', '<!"0"-"1"!>' params = { 'conv': lambda s: int(s[1:-1]), 'disj': ' or ', 'sep': '-', 'left_open': '<!', 'left_closed': '<', 'right_open': '!>', 'right_closed': '>', 'pinf': r'\+oo', 'ninf': '-oo', } assert P.from_string(i1, **params) == P.closed(0, 1) assert P.from_string(i2, **params) == P.openclosed(0, 1) assert P.from_string(i3, **params) == P.closedopen(0, 1) assert P.from_string(i4, **params) == P.open(0, 1) assert P.from_string('<!!>', **params) == P.empty() assert P.from_string('<"1">', **params) == P.singleton(1) assert P.from_string('<!-oo-"1">', **params) == P.openclosed(-P.inf, 1) assert P.from_string('<"1"-+oo!>', **params) == P.closedopen(1, P.inf) assert P.from_string('<"0"-"1"> or <"2"-"3">', **params) == P.closed(0, 1) | P.closed(2, 3)
def get_compatible_isoforms_stitcher(mol_list, isoform_dict_json, h): isoform_dict = P.IntervalDict() for i, s in isoform_dict_json.items(): isoform_dict[P.from_string(i, conv=int)] = set(s.split(',')) compatible_isoforms_trie = dict() new_mol_list = [] for success, mol in [(s, pysam.AlignedRead.fromstring(m, h)) for s, m in mol_list]: if not success: if type(mol) == 'str': new_mol_list.append((success, mol)) else: new_mol_list.append((success, mol.to_string())) continue i = interval(intervals_extract(mol.get_reference_positions())) set_list = [ s for k, s in isoform_dict.get(i, default={'intronic'}).items() if len(list(P.iterate(k, step=1))) > 4 ] if {'intronic'} in set_list: if len(set_list) > 1: del set_list[set_list.index({'intronic'})] try: mol.set_tag('CT', ','.join(list(set.intersection(*set_list)))) new_mol_list.append((success, mol.to_string())) except: continue return new_mol_list
def test_conv_is_required(self): with pytest.raises(Exception): P.from_string('[1,2]', None)
def test_unions(self): assert P.from_string('[0,1] | [2,3]', int) == P.closed(0, 1) | P.closed(2, 3)
def test_infinities(self): assert P.from_string('(-inf,1]', int) == P.openclosed(-P.inf, 1) assert P.from_string('[1,+inf)', int) == P.closedopen(1, P.inf)
def test_empty(self): assert P.from_string('()', int) == P.empty()
def test_singleton(self): assert P.from_string('[0]', int) == P.singleton(0)
def test_bounds(self): assert P.from_string('[0,1]', int) == P.closed(0, 1) assert P.from_string('(0,1]', int) == P.openclosed(0, 1) assert P.from_string('[0,1)', int) == P.closedopen(0, 1) assert P.from_string('(0,1)', int) == P.open(0, 1)
def time_interval_from_str(bytes_string): converter = lambda s: datetime.strptime(s, time_format).time() return P.from_string(bytes_string.decode("utf-8"), conv=converter)