Example #1
0
 def setUp(self):
     """setUp: setup method for all tests"""
     self.true = Pairs([(0,40),(1,39),(2,38),(3,37),(10,20),\
         (11,19),(12,18),(13,17),(26,33),(27,32)])
     self.predicted = Pairs([(0,40),(1,39),(2,38),(3,37),(4,36),\
         (5,35),(10,22),(11,20),(14,29),(15,28)])
     self.seq = ['>seq1\n','agguugaaggggauccgauccacuccccggcuggucaaccu']
Example #2
0
def adjust_pairs_from_mapping(pairs, mapping):
    """Returns new Pairs object with numbers adjusted according to map

    pairs: list of tuples or Pairs object
    mapping: dictionary containing mapping of positions from
        one state to the other (e.g. ungapped to gapped)
         For example:
        {0: 0, 1: 1, 2: 3, 3: 4, 4: 6, 5: 7, 6: 9, 7: 10, 8: 12}

    When the Pairs object corresponds to an ungapped sequence and
        you want to insert gaps, use a mapping from ungapped to gapped.
    When the Pairs object corresponds to a gapped sequence and you
        want to degap it, use a mapping from gapped to ungapped.
    """
    result = Pairs()
    for x, y in pairs:
        if x is None:
            new_x = None
        elif x not in mapping:
            continue
        else:
            new_x = mapping[x]
        if y is None:
            new_y = None
        elif y not in mapping:
            continue
        else:
            new_y = mapping[y]
        result.append((new_x, new_y))

    return result
Example #3
0
def column_parser(lines):
    """Parser column format"""

    record = False
    result = []
    struct = []
    seq = ''
    for line in lines:
        if line.startswith('; ------'): #structure part beginns
            record = True
            continue
        if line.startswith('; ******'): #structure part ends
            record = False
            struct =  adjust_base(struct,-1)
            struct = Pairs(struct).directed()#remove duplicates
            struct.sort()

            result.append([seq,struct])
            struct = []
            seq = ''
            continue
        if record:
            sline = line.split()
            if sline[4] == '.': #skip not paired
                seq = ''.join([seq,sline[1]])
                continue
            seq = ''.join([seq,sline[1]])
            pair = (int(sline[3]),int(sline[4])) #(alignpos,align_bp)
            struct.append(pair)
        
    return result
Example #4
0
def ilm_parser(lines=None,pseudo=True):
    """Ilm format parser

    Takes lines as input and returns a list with Pairs object.
    Pseudo - if True returns pairs with possible pseudoknot
             if False removes pseudoknots       
    """
    pairs = []
    for line in lines:
        if line.startswith('Final') or len(line)==1:#skip these lines
            continue
        line = line.strip('\n')
        line = map(int,line.split(None,2))
        if line[1] == 0:
            continue #Skip this line, not a pair
        else:
            pairs.append(line) 

    pairs = adjust_base(pairs,-1)
    tmp = Pairs(pairs).directed()
    tmp.sort()
    if not pseudo:
        tmp = opt_single_random(tmp)
        tmp.sort()
    result = []
    result.append(tmp)

    return result
Example #5
0
def adjust_base(pairs, offset):
    """Returns new Pairs with values shifted by offset

    pairs: Pairs object or list of tuples
    offset: integer

    Adjusts the base of a pairs object or a list of pairs according to
        the given offset.
    There's no validation in here! It is possible negative values are
        returned -> user responsibility.
    This method treats all pairs as equal. It'll return a pairs object
        of exactly the same length as the input, including pairs containing
        None, and duplicates.

    Example: adjust_base(Pairs([(2,8),(4,None)]), 2) --> [(4,10),(6,None)]
    """
    if not isinstance(offset, int):
        raise PairsAdjustmentError("adjust_base: offset should be integer")
    result = Pairs()
    for x, y in pairs:
        if x is not None:
            new_x = x + offset
        else:
            new_x = x
        if y is not None:
            new_y = y + offset
        else:
            new_y = y
        result.append((new_x, new_y))
    assert len(result) == len(pairs)
    return result
Example #6
0
    def test_compare_pairs(self):
        """compare_pairs: should work on simple case"""
        #all the same
        p1 = Pairs([(3,10),(4,9),(5,8),(20,24)])
        p2 = Pairs([(3,10),(4,9),(5,8),(20,24)])
        self.assertEqual(compare_pairs(p1,p2),1)
        
        #all different
        p1 = Pairs([(3,10),(4,9),(5,8),(20,24)])
        p2 = Pairs([(1,2),(3,4),(5,6)])
        self.assertEqual(compare_pairs(p1,p2),0)

        #one empty
        p1 = Pairs([(3,10),(4,9),(5,8),(20,24)])
        p2 = Pairs([])
        self.assertEqual(compare_pairs(p1,p2),0)

        #partially different
        p1 = Pairs([(1,2),(3,4),(5,6),(7,8)])
        p2 = Pairs([(1,2),(3,4),(9,10),(11,12)])
        self.assertFloatEqual(compare_pairs(p1,p2),.33333333333333333)

        #partially different
        p1 = Pairs([(1,2),(3,4),(5,6)])
        p2 = Pairs([(1,2),(3,4),(9,10)])
        self.assertFloatEqual(compare_pairs(p1,p2),.5)
Example #7
0
def adjust_base(pairs, offset):
    """Returns new Pairs with values shifted by offset

    pairs: Pairs object or list of tuples
    offset: integer

    Adjusts the base of a pairs object or a list of pairs according to
        the given offset.
    There's no validation in here! It is possible negative values are
        returned -> user responsibility.
    This method treats all pairs as equal. It'll return a pairs object
        of exactly the same length as the input, including pairs containing
        None, and duplicates.

    Example: adjust_base(Pairs([(2,8),(4,None)]), 2) --> [(4,10),(6,None)]
    """
    if not isinstance(offset, int):
        raise PairsAdjustmentError("adjust_base: offset should be integer")
    result = Pairs()
    for x, y in pairs:
        if x is not None:
            new_x = x + offset
        else:
            new_x = x
        if y is not None:
            new_y = y + offset
        else:
            new_y = y
        result.append((new_x, new_y))
    assert len(result) == len(pairs)
    return result
Example #8
0
def adjust_pairs_from_mapping(pairs, mapping):
    """Returns new Pairs object with numbers adjusted according to map

    pairs: list of tuples or Pairs object
    mapping: dictionary containing mapping of positions from
        one state to the other (e.g. ungapped to gapped)
         For example:
        {0: 0, 1: 1, 2: 3, 3: 4, 4: 6, 5: 7, 6: 9, 7: 10, 8: 12}

    When the Pairs object corresponds to an ungapped sequence and
        you want to insert gaps, use a mapping from ungapped to gapped.
    When the Pairs object corresponds to a gapped sequence and you
        want to degap it, use a mapping from gapped to ungapped.
    """
    result = Pairs()
    for x,y in pairs:
        if x is None:
            new_x = None
        elif x not in mapping:
            continue
        else:
            new_x = mapping[x]
        if y is None:
            new_y = None
        elif y not in mapping:
            continue
        else:
            new_y = mapping[y]
        result.append((new_x, new_y))

    return result
Example #9
0
    def test_sensitivity_dupl(self):
        """sensitivity: should handle duplicates, pseudo, None"""
        ref = Pairs([(1,6),(2,5),(3,10),(7,None),(None,None),(5,2),(4,9)])
        pred = Pairs([(6,1),(10,11),(3,12)])
        self.assertFloatEqual(sensitivity(ref, pred), 0.25)

        pred = Pairs([(6,1),(10,11),(3,12),(20,None),(None,None),(1,6)])
        self.assertFloatEqual(sensitivity(ref, pred), 0.25)
Example #10
0
    def test_adjust_base_None(self):
        """adjust_base: should keep Nones or duplicates, ignore conflicts"""
        pairs = Pairs([(2,8),(3,7),(6,None),(None,None),(2,10)])
        expected = Pairs([(1,7),(2,6),(5,None),(None, None),(1,9)])
        self.assertEqual(adjust_base(pairs,-1), expected)

        p = Pairs([(1,2),(2,1),(1,2),(2,None)])
        self.assertEqual(adjust_base(p, 1), [(2,3),(3,2),(2,3),(3,None)])
Example #11
0
 def test_symmetric(self):
     """Pairs symmetric() should add (down,up) for each (up,down)"""
     self.assertEqual(self.Empty.symmetric(), [])
     self.assertEqualItems(self.OneTuple.symmetric(), [(2, 1), (1, 2)])
     self.assertEqualItems(
         Pairs([(1, 2), (1, 2)]).symmetric(), [(1, 2), (2, 1)])
     self.assertEqualItems(Pairs([(1,2),(3,4)]).symmetric(),\
     [(1,2),(2,1),(3,4),(4,3)])
     self.assertEqualItems(Pairs([(1, None)]).symmetric(), [])
Example #12
0
 def test_ungapped_to_gapped(self):
     """ungapped_to_gapped: Sequence, ModelSequence, old_cogent, string
     """
     p = Pairs([(0, 6), (1, 5), (3, 9)])
     exp = Pairs([(0, 5), (1, 4), (3, 7)])
     f = ungapped_to_gapped
     self.assertEqual(f(self.rna1, exp)[1], p)
     self.assertEqual(f(self.m1, exp)[1], p)
     self.assertEqual(f(self.s1, exp)[1], p)
Example #13
0
 def test_delete_gaps_from_pairs_weird(self):
     """delete_gaps_from_pairs: should ignore conflicts etc"""
     r = delete_gaps_from_pairs
     gap_list = [0, 1, 4, 5, 7, 9]
     p = Pairs([(2, 6), (3, 8)])
     self.assertEqualItems(r(p, gap_list), [(0, 2), (1, 3)])
     p = Pairs([(2, 6), (3, 8), (3, None), (6, 2), (3, 8), (None, None)])
     self.assertEqualItems(r(p, gap_list),\
         [(0,2),(1,3),(1,None),(2,0),(1,3),(None, None)])
Example #14
0
 def test_sensitivity_empty(self):
     """sensitivity: should work on emtpy Pairs"""
     # both empty
     self.assertFloatEqual(sensitivity(Pairs(), Pairs()), 1)
     pred = Pairs([(6, 1), (10, 11), (3, 12), (13, 20), (14, 19), (15, 18)])
     # prediction emtpy
     self.assertFloatEqual(sensitivity(Pairs(), pred), 0)
     # reference empty
     self.assertFloatEqual(sensitivity(pred, Pairs()), 0)
Example #15
0
 def test_selectivity_general(self):
     """selectivity: should work in general"""
     ref = Pairs([(1, 6), (2, 5), (10, 13)])
     pred = Pairs([(6, 1), (3, 4), (10, 12)])
     # one good prediction
     self.assertFloatEqual(selectivity(ref, pred), 0.5)
     # over-prediction not penalized
     pred = Pairs([(6, 1), (10, 11), (3, 12), (13, 20), (14, 19), (15, 18)])
     self.assertFloatEqual(selectivity(ref, pred), 0.25)
Example #16
0
 def test_selectivity_empty(self):
     """selectivity: should handle empty reference/predicted structure"""
     # both empty
     self.assertFloatEqual(selectivity(Pairs(), Pairs()), 1)
     pred = Pairs([(6, 1), (10, 11), (3, 12), (13, 20), (14, 19), (15, 18)])
     # prediction emtpy
     self.assertFloatEqual(selectivity(Pairs(), pred), 0)
     # reference empty
     self.assertFloatEqual(selectivity(pred, Pairs()), 0)
Example #17
0
 def test_directed(self):
     """Pairs directed() should change all pairs so that a<b in (a,b)"""
     self.assertEqual(self.Empty.directed(), [])
     res = self.Undirected.directed()
     res.sort()
     self.assertEqual(res, Pairs([(1, 2), (1, 7), (3, 8), (4, 6)]))
     res = self.UndirectedNone.directed()
     self.assertEqual(res, Pairs([]))
     res = self.UndirectedDouble.directed()
     self.assertEqual(res, Pairs([(1, 2)]))
Example #18
0
 def test_get_counts_pseudo(self):
     """get_counts: should work when pseudo in ref -> classification off"""
     # pairs that would normally be compatible, are now contradicting
     ref = Pairs([(0, 8), (1, 7), (4, 10)])
     pred = Pairs([(0, 8), (3, 6), (4, 10)])
     seq = 'GACUGUGUCAU'
     exp = {'TP':2,'TN':13-2-1, 'FN':1,'FP':1,\
         'FP_INCONS':0, 'FP_CONTRA':1, 'FP_COMP':0}
     self.assertEqual(get_counts(ref, pred, split_fp=True,\
         sequences=[seq], min_dist=4), exp)
Example #19
0
def pairs_union(one, other):
    """Returns the intersection of one and other

    one: list of tuples or Pairs object
    other: list of tuples or Pairs object

    one and other should map onto a sequence of the same length.
    """
    pairs1 = frozenset(Pairs(one).directed())  #removes duplicates
    pairs2 = frozenset(Pairs(other).directed())
    return Pairs(pairs1 | pairs2)
Example #20
0
 def test_all_metrics_pseudo(self):
     """all_metrics: pseudoknot in ref, check against compare_ct.pm"""
     ref = Pairs([(0, 8), (1, 7), (4, 10)])
     pred = Pairs([(0, 8), (3, 6), (4, 10)])
     seq = 'GACUGUGUCAU'
     exp = {'SENSITIVITY':0.6666667, 'SELECTIVITY':0.6666667,\
         'AC':0.6666667, 'CC':0.57575758, 'MCC':0.57575758}
     obs = all_metrics(ref, pred, seqs=[seq], min_dist=4)
     self.assertEqualItems(obs.keys(), exp.keys())
     for k in exp:
         self.assertFloatEqual(obs[k], exp[k])
Example #21
0
def get_counts(ref, predicted, split_fp=False, sequences=None, min_dist=4):
    """Return TP, TN, FPcont, FPconf FPcomp, FN counts"""

    result = dict.fromkeys(['TP','TN','FN','FP',\
        'FP_INCONS','FP_CONTRA','FP_COMP'],0)

    ref_set = frozenset(Pairs(ref).directed())
    pred_set = frozenset(Pairs(predicted).directed())

    ref_dict = dict(ref.symmetric())
    pred_dict = dict(predicted.symmetric())

    tp_pairs = ref_set.intersection(pred_set)
    fn_pairs = ref_set.difference(pred_set)
    fp_pairs = pred_set.difference(ref_set)
    result['TP'] = len(tp_pairs)
    result['FN'] = len(fn_pairs)
    result['FP'] = len(fp_pairs)
    if split_fp:
        fp_incons = []
        fp_contra = []
        fp_comp = []
        for x,y in fp_pairs:
            if x in ref_dict or y in ref_dict:
                #print "Conflicting: %d - %d"%(x,y)
                fp_incons.append((x,y))
            else:
                five_prime = x
                three_prime = y
                contr_found = False
                for idx in range(x,y+1):
                    if idx in ref_dict and\
                        (ref_dict[idx] < five_prime or\
                            ref_dict[idx] > three_prime):
                        #print "Contradicting: %d - %d"%(x,y)
                        contr_found = True
                        fp_contra.append((x,y))
                        break
                if not contr_found:
                    #print "Comatible: %d - %d"%(x,y)
                    fp_comp.append((x,y))


        result['FP_INCONS'] = len(fp_incons)
        result['FP_CONTRA'] = len(fp_contra)
        result['FP_COMP'] = len(fp_comp)
        assert result['FP_INCONS'] + result['FP_CONTRA'] + result['FP_COMP'] ==\
            result['FP']
    if sequences:
        num_possible_pairs = get_all_pairs(sequences, min_dist)
        result['TN'] = num_possible_pairs - result['TP'] -\
            result['FP_INCONS'] - result['FP_CONTRA']

    return result
Example #22
0
    def test_pairs_intersection_duplicates(self):
        """pairs_intersection: should work on flipped pairs and duplicates
        """
        p1 = Pairs([(3,10),(4,9),(5,8),(20,24)])
        p2 = Pairs([(10,3),(4,9),(5,8),(9,4),(4,9),(23,30)])
        self.assertEqualItems(pairs_intersection(p1,p2),[(3,10),(4,9),(5,8)])

        # Conflicts, duplicates, None, pseudoknots 
        p1 = Pairs([(3,10),(4,9),(5,8),(20,24),(22,26),(3,2),(9,4),(6,None)])
        p2 = Pairs([(1,12),(4,9),(5,8)])
        self.assertEqualItems(pairs_intersection(p1,p2),\
            [(4,9),(5,8)])
Example #23
0
 def test_insert_gaps_in_pairs(self):
     """insert_gaps_in_pairs: should work with normal and conflicts"""
     p = Pairs([(0, 3), (1, 2), (1, 4), (3, None)])
     gaps = [0, 1, 4, 5, 7]
     self.assertEqual(insert_gaps_in_pairs(p, gaps),\
         [(2,8),(3,6),(3,9),(8,None)])
     p = Pairs([(0, 6), (1, 5), (2, None), (3, 7), (0, 1), (5, 1)])
     gaps = [0, 2, 6, 9]
     self.assertEqual(insert_gaps_in_pairs(p, gaps),\
         [(1,10),(3,8),(4,None),(5,11),(1,3),(8,3)])
     gaps = [2, 3, 4, 9]
     self.assertEqual(insert_gaps_in_pairs(p, gaps),\
         [(0,10),(1,8),(5,None),(6,11),(0,1),(8,1)])
Example #24
0
 def test_compare_random_to_correct(self):
     """comapre_random_to_correct: should return correct fraction
     """
     p1 = Pairs([(1, 8), (2, 7), (3, 6), (4, 5)])
     p2 = Pairs([(1, 8)])
     p3 = Pairs([(1, 8), (2, 7), (4, 5)])
     p4 = Pairs([(1, 8), (2, 7), (9, 10), (11, 12)])
     self.assertFloatEqual(compare_random_to_correct(p2, p1), 1)
     self.assertFloatEqual(compare_random_to_correct(p3, p1), 1)
     self.assertFloatEqual(compare_random_to_correct(p4, p1), 0.5)
     self.assertFloatEqual(compare_random_to_correct([], p1), 0)
     self.assertFloatEqual(compare_random_to_correct(p2, []), 0)
     self.assertFloatEqual(compare_random_to_correct([], []), 1)
Example #25
0
    def test_adjust_pairs_from_mapping_confl(self):
        """adjust_pairs_from_mapping: should handle conflicts, pseudo, dupl
        """
        f = adjust_pairs_from_mapping
        p = Pairs([(0,6),(1,5),(2,None),(None,None),(1,4),(3,7),(6,0)])
        m = {0:1,1:3,2:6,3:7,4:8,5:10,6:11,7:12}
        exp = Pairs([(1,11),(3,10),(6,None),(None,None),(3,8),(7,12),(11,1)])
        self.assertEqual(f(p, m), exp)

        p = Pairs([(1,11),(3,10),(7,12),(6,None),(None,None),(5,8)])
        m = {1: 0, 3: 1, 6: 2, 7: 3, 8: 4, 10: 5, 11: 6, 12: 7}
        exp = Pairs([(0,6),(1,5),(3,7),(2,None),(None,None)])
        self.assertEqual(f(p,m), exp)
Example #26
0
def ct_parser(lines=None):
    """Ct format parser

    Takes lines from a ct file as input
    
    Returns a list containing sequence,structure and if available the energy.
    [[seq1,[struct1],energy1],[seq2,[struct2],energy2],...]
    """

    count = 0
    length = ''
    energy = None
    seq = ''
    struct = []
    result = []

    for line in lines:
        count+=1
        sline = line.split(None,6) #sline = split line
        if count==1 or new_struct(line):#first line or new struct line.
            if count > 1:
                struct = adjust_base(struct,-1)
                struct = Pairs(struct).directed()
                struct.sort()
                if energy is not None:
                    result.append([seq,struct,energy])
                    energy = None
                else:
                    result.append([seq,pairs])
                struct = []
                seq = ''
            #checks if energy for predicted struct is given
            if sline.__contains__('dG') or sline.__contains__('ENERGY'):
                energy = atof(sline[3])
            if sline.__contains__('Structure'):
                energy = atof(sline[2])
        else:
            seq = ''.join([seq,sline[1]])
            if not int(sline[4]) == 0:#unpaired base
                pair = ( int(sline[0]),int(sline[4]) )
                struct.append(pair) 
    #structs are one(1) based, adjust to zero based
    struct = adjust_base(struct,-1)
    struct = Pairs(struct).directed()
    struct.sort()

    if energy is not None:
        result.append([seq,struct,energy])
    else:
        result.append([seq,struct])
    return result 
Example #27
0
 def test_delete_gaps_from_pairs(self):
     """delete_gaps_from_pairs: should work on standard input"""
     r = delete_gaps_from_pairs
     # empty list
     p = Pairs([])
     self.assertEqual(r(p, [1, 2, 3]), [])
     # normal list
     p1 = Pairs([(2, 8), (3, 6)])
     gap_list = [0, 1, 4, 5, 7, 9]
     self.assertEqualItems(r(p1, gap_list), [(0, 3), (1, 2)])
     p2 = Pairs([(2, 8), (3, 6), (4, 9)])
     self.assertEqualItems(r(p2, gap_list), [(0, 3), (1, 2)])
     p3 = Pairs([(2, 8), (3, 6), (4, 10)])
     self.assertEqualItems(r(p3, gap_list), [(0, 3), (1, 2)])
Example #28
0
    def test_mismatches(self):
        """Pairs mismatches() should return #pairs that can't be formed"""
        # with plain string
        self.assertEqual(Pairs([(0, 1)]).mismatches('AC', {}), 1)
        self.assertEqual(
            Pairs([(0, 1)]).mismatches('AC', {('A', 'C'): None}), 0)
        self.assertEqual(
            Pairs([(0, 1)]).mismatches('AC', {('A', 'G'): None}), 1)
        self.assertEqual(Pairs([(0,1),(2,3),(3,1)]).\
        mismatches('ACGU',{('A','U'):None}),3)

        # using sequence with alphabet
        sequence = Rna('ACGUA')
        self.assertEqual(
            Pairs([(0, 1), (0, 4), (0, 3)]).mismatches(sequence), 2)
Example #29
0
    def test_pairs_intersection(self):
        """pairs_intersection: should work on simple case
        """
        p1 = Pairs([(3, 10), (4, 9), (5, 8), (20, 24)])
        p2 = Pairs([(1, 12), (4, 9), (5, 8)])
        self.assertEqualItems(pairs_intersection(p1, p2), [(4, 9), (5, 8)])

        #works when one is empty
        p1 = Pairs([(3, 10), (4, 9), (5, 8), (20, 24)])
        p2 = Pairs([])
        self.assertEqualItems(pairs_intersection(p1, p2), [])

        #works also on lists (not Pairs)
        p1 = [(3, 10), (4, 9), (5, 8), (20, 24)]
        p2 = [(1, 12), (4, 9), (5, 8)]
        self.assertEqualItems(pairs_intersection(p1, p2), [(4, 9), (5, 8)])
Example #30
0
def compare_random_to_correct(one, other):
    """Returns fraction of bp in one that is in other (correct)
    
    one: list of tuples or Pairs object
    other: list of tuples or Pairs object

    Note: the second structure is the one compared against (the correct
        structure)
    """
    if not one and not other:
        return 1.0
    if not one or not other:
        return 0.0
    pairs1 = frozenset(Pairs(one).directed())  #removes duplicates
    pairs2 = frozenset(Pairs(other).directed())
    return len(pairs1 & pairs2) / len(pairs1)
Example #31
0
    def test_adjust_base(self):
        """adjust_base: should work for pairs object or list of pairs"""
        p = Pairs()
        self.assertEqual(adjust_base(p, 10), [])

        pairs = [(1, 21), (2, 15), (3, 13), (4, 11), (5, 10), (6, 9)]
        offset = -1
        expected = [(0, 20), (1, 14), (2, 12), (3, 10), (4, 9), (5, 8)]
        obs_pairs = adjust_base(pairs, offset)
        self.assertEqual(obs_pairs, expected)

        pairs = Pairs([(0, 10), (1, 9)])
        self.assertEqual(adjust_base(pairs, -1), Pairs([(-1, 9), (0, 8)]))
        self.assertEqual(adjust_base(pairs, 5), Pairs([(5, 15), (6, 14)]))

        self.assertRaises(PairsAdjustmentError, adjust_base, pairs, 3.5)
Example #32
0
def common(structs):
    """
    Will return a list of sequences and structures with the most common structure 
    first in the list. (rest or list unordered!)
    Don't care which of the sequences for the "winning" sequence that is reported
    since the are not ranked amongst them self.
    """

    frequency = {}
    v = 0
    indx = 0
    result = []
    tmp_list = [
    ]  #lookup the seq for the structures,dont care which winner seq
    key = []
    for block in structs:
        tmp_list.extend(block)
        p = tuple(block[-1])
        if frequency.__contains__(
                p):  #everytime struct p appears count up by 1
            frequency[p] += 1
        else:
            frequency[p] = 1
        nr = frequency[p]
        if nr > v:  #Which struct appears most times
            v = nr
            key = p

    #if winning structure has frequency == 1 all structure apper only once
    if frequency[key] == 1:
        longest = 0
        for block in structs:
            l = len(block[-1])
            if l > longest:  #pick longest sequence as the winner
                key = tuple(block[-1])

    winner = Pairs(key)
    indx = tmp_list.index(winner) - 1
    result.append([tmp_list[indx],
                   winner])  #adds the most common structure first
    del frequency[key]
    for i in frequency.keys():  #rest of structures added
        i = Pairs(i)
        indx = tmp_list.index(i) - 1
        result.append([tmp_list[indx], i])

    return result
Example #33
0
def parse_residues(residue_lines, num_base, unpaired_symbol):
    """Return RnaSequence and Pairs object from residue lines.

    residue_lines -- list of lines or anything that behaves like it. 
        Lines should contain:
        residue_position, residue_identiy, residue_partner.
    num_base -- int, basis of the residue numbering. In bpseq files from
        the CRW website, the numbering starts at 1.
    unpaired_symbol -- string, symbol in the 'partner' column that indicates
        that a base is unpaired. In bpseq files from the CRW website, the
        unpaired_symbol is '0'. This parameter should be a string to allow
        other symbols that can't be casted to an integer to indicate
        unpaired bases.
    
    Checks for double entries both in the sequence and the structure, and
    checks that the structre is valid in the sense that if (up,down) in there,
    that (down,up) is the same.
    """
    #create dictionary/list for sequence and structure
    seq_dict = {}
    pairs = Pairs()

    for line in residue_lines:
        try:
            pos, res, partner = line.strip().split()
            if partner == unpaired_symbol:
                # adjust pos, not partner
                pos = int(pos) - num_base
                partner = None
            else:
                # adjust pos and partner
                pos = int(pos) - num_base
                partner = int(partner) - num_base
            pairs.append((pos, partner))

            #fill seq_dict
            if pos in seq_dict:
                raise BpseqParseError(\
                    "Double entry for residue %s (%s in bpseq file)"\
                    %(str(pos), str(pos+1)))
            else:
                seq_dict[pos] = res

        except ValueError:
            raise BpseqParseError("Failed to parse line: %s" % (line))

    #check for conflicts, remove unpaired bases
    if pairs.hasConflicts():
        raise BpseqParseError("Conflicts in the list of basepairs")
    pairs = pairs.directed()
    pairs.sort()

    # construct sequence from seq_dict
    seq = RnaSequence(construct_sequence(seq_dict))

    return seq, pairs
Example #34
0
 def test_tuples(self):
     """Pairs tuples() should transform the elements of list to tuples"""
     x = Pairs([])
     x.tuples()
     assert x == []
     
     x = Pairs([[1,2],[3,4]])
     x.tuples()
     assert x == [(1,2),(3,4)]
     
     x = Pairs([(1,2),(3,4)])
     x.tuples()
     assert x == [(1,2),(3,4)]
     assert x != [[1,2],[3,4]]
Example #35
0
def compare_pairs(one, other):
    """Returns size of intersection divided by size of union between two Pairs

    Use as a similiraty measure for comparing secondary structures.
    Returns the number of base pairs common to both structures divided by
    the number of base pairs that is in one or the other structure:
    (A AND B)/(A OR B) (intersection/union)

    one: list of tuples or Pairs object
    other: list of tuples or Pairs object
    """
    if one.hasConflicts() or other.hasConflicts():
        raise ValueError("Can't handle conflicts in the structure" "")
    if not one and not other:
        return 1.0
    pairs1 = frozenset(Pairs(one).directed())  #removes duplicates
    pairs2 = frozenset(Pairs(other).directed())
    return len(pairs1 & pairs2) / len(pairs1 | pairs2)
Example #36
0
def compare_pairs_mapping(one, other, one_to_other):
    """Returns intersection/union given a mapping from the first pairs to second

    Use in case the numbering of the two Pairs object don't correspond.
    Sort of aligning two ungapped sequences and comparing their Pairs
    object via a mapping. 
    
    one: list of tuples or Pairs object
    other: list of tuples or Pairs object
    one_to_other: mapping of positions in first pairs object to positions
        in second pairs object.
    For example:
    # pos in first seq, base, pos in second seq
    #1 U 0
    #2 C 1
    #3 G 2
    #4 A 3
    #  A 4
    #5 C 5
    #6 C 6
    #7 U 
    #8 G 7

    mapping = {1:0, 2:1, 3:2, 4:3, 5:5, 6:6, 7:None, 8:7}
    """
    if not one and not other:
        return 1.0
    just_in_first = 0
    just_in_second = 0
    in_both = 0
    pairs1 = Pairs(one).directed() #removes duplicates
    pairs2 = Pairs(other).directed()
    for x,y in pairs1:
        other_match = (one_to_other[x],one_to_other[y])
        if other_match in pairs2:
            in_both += 1
            pairs2.remove(other_match)
        else:
            just_in_first += 1
    just_in_second += len(pairs2)

    return in_both/(just_in_first + in_both + just_in_second)
Example #37
0
def parse_residues(residue_lines, num_base, unpaired_symbol):
    """Return RnaSequence and Pairs object from residue lines.

    residue_lines -- list of lines or anything that behaves like it. 
        Lines should contain:
        residue_position, residue_identiy, residue_partner.
    num_base -- int, basis of the residue numbering. In bpseq files from
        the CRW website, the numbering starts at 1.
    unpaired_symbol -- string, symbol in the 'partner' column that indicates
        that a base is unpaired. In bpseq files from the CRW website, the
        unpaired_symbol is '0'. This parameter should be a string to allow
        other symbols that can't be casted to an integer to indicate
        unpaired bases.
    
    Checks for double entries both in the sequence and the structure, and
    checks that the structre is valid in the sense that if (up,down) in there,
    that (down,up) is the same.
    """
    #create dictionary/list for sequence and structure
    seq_dict = {}
    pairs = Pairs()
    
    for line in residue_lines:
        try:
            pos, res, partner = line.strip().split()
            if partner == unpaired_symbol:
                # adjust pos, not partner
                pos = int(pos) - num_base
                partner = None
            else:
                # adjust pos and partner
                pos = int(pos) - num_base
                partner = int(partner) - num_base
            pairs.append((pos,partner))
            
            #fill seq_dict
            if pos in seq_dict:
                raise BpseqParseError(\
                    "Double entry for residue %s (%s in bpseq file)"\
                    %(str(pos), str(pos+1)))
            else:
                seq_dict[pos] = res
        
        except ValueError:
            raise BpseqParseError("Failed to parse line: %s"%(line))
    
    #check for conflicts, remove unpaired bases 
    if pairs.hasConflicts():
        raise BpseqParseError("Conflicts in the list of basepairs")
    pairs = pairs.directed()
    pairs.sort()
    
    # construct sequence from seq_dict
    seq = RnaSequence(construct_sequence(seq_dict))
    
    return seq, pairs
Example #38
0
def ct_parser(lines=None):
    """Ct format parser

    Takes lines from a ct file as input
    
    Returns a list containing sequence,structure and if available the energy.
    [[seq1,[struct1],energy1],[seq2,[struct2],energy2],...]
    """

    count = 0
    length = ''
    energy = None
    seq = ''
    struct = []
    result = []

    for line in lines:
        count+=1
        sline = line.split(None,6) #sline = split line
        if count==1 or new_struct(line):#first line or new struct line.
            if count > 1:
                struct = adjust_base(struct,-1)
                struct = Pairs(struct).directed()
                struct.sort()
                if energy is not None:
                    result.append([seq,struct,energy])
                    energy = None
                else:
                    result.append([seq,pairs])
                struct = []
                seq = ''
            #checks if energy for predicted struct is given
            if sline.__contains__('dG') or sline.__contains__('ENERGY'):
                energy = atof(sline[3])
            if sline.__contains__('Structure'):
                energy = atof(sline[2])
        else:
            seq = ''.join([seq,sline[1]])
            if not int(sline[4]) == 0:#unpaired base
                pair = ( int(sline[0]),int(sline[4]) )
                struct.append(pair) 
    #structs are one(1) based, adjust to zero based
    struct = adjust_base(struct,-1)
    struct = Pairs(struct).directed()
    struct.sort()

    if energy is not None:
        result.append([seq,struct,energy])
    else:
        result.append([seq,struct])
    return result 
Example #39
0
    def test_toPartners(self):
        """Pairs toPartners() should return a Partners object"""
        a = Pairs([(1,5),(3,4),(6,9),(7,8)]) #normal
        b = Pairs([(0,4),(2,6)]) #pseudoknot
        c = Pairs([(1,6),(3,6),(4,5)]) #conflict

        self.assertEqual(a.toPartners(10),[None,5,None,4,3,1,9,8,7,6])
        self.assertEqual(a.toPartners(13,3),\
        [None,None,None,None,8,None,7,6,4,12,11,10,9])
        assert isinstance(a.toPartners(10),Partners)
        self.assertEqual(b.toPartners(7),[4,None,6,None,0,None,2])
        self.assertRaises(ValueError,c.toPartners,7)
        self.assertEqual(c.toPartners(7,strict=False),[None,None,None,6,5,4,3])

        #raises an error when try to insert something at non-existing indices
        self.assertRaises(IndexError,c.toPartners,0)
Example #40
0
def delete_gaps_from_pairs(pairs, gap_list):
    """Returns Pairs object with pairs adjusted to gap_list

    pairs: list of tuples or Pairs object
    gap_list: list or array of gapped positions that should be removed
        from the pairs object

    Base pairs of which one of the partners or both of them are in 
        the gap list are removed. If both of them are not in the gap_list, the
        numbering is adjusted according to the gap_list.
    When at least one of the two pair members is in the gap_list, the
        pair will be removed. The rest of the structure will be left
        intact. Pairs containing None, duplicates, pseudoknots, and 
        conflicts will be maintained and adjusted according to the gap_list.
    """
    if not gap_list:
        result = Pairs()
        result.extend(pairs)
        return result

    g = array(gap_list)
    result = Pairs()
    for up, down in pairs:
        if up in g or down in g:
            continue
        else:
            if up is not None:
                new_up = up - g.searchsorted(up)
            else:
                new_up = up
            if down is not None:
                new_down = down - g.searchsorted(down)
            else:
                new_down = down
            result.append((new_up, new_down))
    return result
Example #41
0
def insert_gaps_in_pairs(pairs, gap_list):
    """Adjusts numbering in pairs according to the gap list.

    pairs: Pairs object
    gap_list: list of integers, gap positions in a sequence

    The main assumptionis that all positions in pairs correspond to
    ungapped positions. If this is not true, the result will be meaningless.
    """
    if not gap_list:
        new = Pairs()
        new.extend(pairs)
        return new

    ungapped = []
    for idx in range(max(gap_list)+2):
        if idx not in gap_list:
            ungapped.append(idx)
    new = Pairs()
    for x,y in pairs:
        if x is not None:
            try:
                new_x = ungapped[x]
            except IndexError:
                new_x = ungapped[-1] + (x-len(ungapped)+1)
        else:
            new_x = x
        if y is not None:
            try:
                new_y = ungapped[y]
            except IndexError:
                new_y = ungapped[-1] + (y-len(ungapped)+1)
        else:
            new_y = y
        new.append((new_x, new_y))
    return new
Example #42
0
class PairsTests(TestCase):
    """Tests for Pairs object"""

    def setUp(self):
        """Pairs SetUp method for all tests"""
        self.Empty = Pairs([])
        self.OneList = Pairs([[1,2]])
        self.OneTuple = Pairs([(1,2)])
        self.MoreLists = Pairs([[2,4],[3,9],[6,36],[7,49]])
        self.MoreTuples = Pairs([(2,4),(3,9),(6,36),(7,49)])
        self.MulNoOverlap = Pairs([(1,10),(2,9),(3,7),(4,12)])
        self.MulOverlap = Pairs([(1,2),(2,3)])
        self.Doubles = Pairs([[1,2],[1,2],[2,3],[1,3]])
        self.Undirected = Pairs([(2,1),(6,4),(1,7),(8,3)])
        self.UndirectedNone = Pairs([(5,None),(None,3)])
        self.UndirectedDouble = Pairs([(2,1),(1,2)])
    
        self.NoPseudo = Pairs([(1,20),(2,19),(3,7),(4,6),(10,15),(11,14)])
        self.NoPseudo2 = Pairs([(1,3),(4,6)])
        #((.(.)).)
        self.p0 = Pairs([(0,6),(1,5),(3,8)])
        #(.((..(.).).))
        self.p1 = Pairs([(0,9),(2,12),(3,10),(5,7)])
        #((.(.(.).)).)
        self.p2 = Pairs([(0,10),(1,9),(3,12),(5,7)])
        #((.((.(.)).).))
        self.p3 = Pairs([(0,9),(1,8),(3,14),(4,13),(6,11)])
        #(.(((.((.))).)).(((.((((..))).)))).)
        self.p4 = Pairs([(0,35),(2,11),(3,10),(4,9),(6,14),(7,13),(16,28),\
            (17,27),(18,26),(20,33),(21,32),(22,31),(23,30)])
        #(.((.).))
        self.p5 = Pairs([(0,5),(2,8),(3,7)])
        self.p6 = Pairs([(0,19),(2,6),(3,5),(8,14),(9,13),(10,12),\
            (16,22),(17,21)])
        self.p7 = Pairs([(0,20),(2,6),(3,5),(8,14),(9,10),(11,16),(12,15),\
            (17,23),(18,22)])

         
    def test_init(self):
        """Pairs should initalize with both lists and tuples"""
        self.assertEqual(self.Empty,[])
        self.assertEqual(self.OneList,[[1,2]])
        self.assertEqual(self.OneTuple,[(1,2)])
        self.assertEqual(self.MulNoOverlap,[(1,10),(2,9),(3,7),(4,12)])
        self.assertEqual(self.MulOverlap,[(1,2),(2,3)])

    def test_toPartners(self):
        """Pairs toPartners() should return a Partners object"""
        a = Pairs([(1,5),(3,4),(6,9),(7,8)]) #normal
        b = Pairs([(0,4),(2,6)]) #pseudoknot
        c = Pairs([(1,6),(3,6),(4,5)]) #conflict

        self.assertEqual(a.toPartners(10),[None,5,None,4,3,1,9,8,7,6])
        self.assertEqual(a.toPartners(13,3),\
        [None,None,None,None,8,None,7,6,4,12,11,10,9])
        assert isinstance(a.toPartners(10),Partners)
        self.assertEqual(b.toPartners(7),[4,None,6,None,0,None,2])
        self.assertRaises(ValueError,c.toPartners,7)
        self.assertEqual(c.toPartners(7,strict=False),[None,None,None,6,5,4,3])

        #raises an error when try to insert something at non-existing indices
        self.assertRaises(IndexError,c.toPartners,0)

    def test_toVienna(self):
        """Pairs toVienna() should return a ViennaStructure if possible"""
        a = Pairs([(1,5),(3,4),(6,9),(7,8)]) #normal
        b = Pairs([(0,4),(2,6)]) #pseudoknot
        c = Pairs([(1,6),(3,6),(4,5)]) #conflict
        d = Pairs([(1,6),(3,None)])
        e = Pairs([(1,9),(8,2),(7,3)]) #not directed
        f = Pairs([(1,6),(2,5),(10,15),(14,11)]) # not directed

        self.assertEqual(a.toVienna(10),'.(.())(())')
        self.assertEqual(a.toVienna(13,offset=3),'....(.())(())')
        
        self.assertRaises(PairError,b.toVienna,7) #pseudoknot NOT accepted
        self.assertRaises(Exception,b.toVienna,7) #old test for exception
        self.assertRaises(ValueError,c.toVienna,7)
        
        #pairs containging None are being skipped
        self.assertEquals(d.toVienna(7),'.(....)')
        
        #raises error when trying to insert at non-existing indices
        self.assertRaises(IndexError,a.toVienna,3)

        self.assertEqual(Pairs().toVienna(3),'...')
        
        #test when parsing in the sequence
        self.assertEqual(a.toVienna('ACGUAGCUAG'),'.(.())(())')
        self.assertEqual(a.toVienna(Rna('AACCGGUUAGCUA'), offset=3),\
            '....(.())(())')
       
        self.assertEqual(e.toVienna(10),'.(((...)))')
        self.assertEqual(f.toVienna(20),'.((..))...((..))....')

    def test_tuples(self):
        """Pairs tuples() should transform the elements of list to tuples"""
        x = Pairs([])
        x.tuples()
        assert x == []
        
        x = Pairs([[1,2],[3,4]])
        x.tuples()
        assert x == [(1,2),(3,4)]
        
        x = Pairs([(1,2),(3,4)])
        x.tuples()
        assert x == [(1,2),(3,4)]
        assert x != [[1,2],[3,4]]

    def test_unique(self):
        """Pairs unique() should remove double occurences of certain tuples"""
        self.assertEqual(self.Empty.unique(),[])
        self.assertEqual(self.MoreTuples.unique(),self.MoreTuples)
        self.assertEqual(self.Doubles.unique(),Pairs([(1,2),(2,3),(1,3)]))

    def test_directed(self):
        """Pairs directed() should change all pairs so that a<b in (a,b)"""
        self.assertEqual(self.Empty.directed(),[])
        res = self.Undirected.directed()
        res.sort()
        self.assertEqual(res,Pairs([(1,2),(1,7),(3,8),(4,6)]))
        res = self.UndirectedNone.directed()
        self.assertEqual(res,Pairs([]))
        res = self.UndirectedDouble.directed()
        self.assertEqual(res,Pairs([(1,2)]))

    def test_symmetric(self):
        """Pairs symmetric() should add (down,up) for each (up,down)"""
        self.assertEqual(self.Empty.symmetric(),[])
        self.assertEqualItems(self.OneTuple.symmetric(),[(2,1),(1,2)])
        self.assertEqualItems(Pairs([(1,2),(1,2)]).symmetric(),[(1,2),(2,1)])
        self.assertEqualItems(Pairs([(1,2),(3,4)]).symmetric(),\
        [(1,2),(2,1),(3,4),(4,3)])
        self.assertEqualItems(Pairs([(1,None)]).symmetric(),[])

    def test_paired(self):
        """Pairs paired() should omit all pairs containing None"""
        self.assertEqual(self.Empty.paired(),[])
        self.assertEqual(Pairs([(1,2),(2,None),(None,3),(None,None)]).paired()\
        ,[(1,2)])

    def test_hasConflicts(self):
        """Pairs hasConflicts() should return True if there are conflicts"""
        assert not self.Empty.hasConflicts()
        assert not Pairs([(1,2),(3,4)]).hasConflicts()
        assert Pairs([(1,2),(2,3)]).hasConflicts()
        assert Pairs([(1,2),(2,None)]).hasConflicts()

    def test_mismatches(self):
        """Pairs mismatches() should return #pairs that can't be formed"""
        # with plain string
        self.assertEqual(Pairs([(0,1)]).mismatches('AC',{}),1)
        self.assertEqual(Pairs([(0,1)]).mismatches('AC',{('A','C'):None}),0)
        self.assertEqual(Pairs([(0,1)]).mismatches('AC',{('A','G'):None}),1)
        self.assertEqual(Pairs([(0,1),(2,3),(3,1)]).\
        mismatches('ACGU',{('A','U'):None}),3)

        # using sequence with alphabet
        sequence = Rna('ACGUA')
        self.assertEqual(Pairs([(0,1),(0,4),(0,3)]).mismatches(sequence),2)

    def test_hasPseudoknots(self):
        """Pairs hasPseudoknots() should return True if there's a pseudoknot"""
                
        assert not self.NoPseudo.hasPseudoknots()
        assert not self.NoPseudo2.hasPseudoknots()
        #add tests for ((.))() etc
        assert self.p0.hasPseudoknots()
        assert self.p1.hasPseudoknots() 
        assert self.p2.hasPseudoknots()
        assert self.p3.hasPseudoknots()
        assert self.p4.hasPseudoknots()
        assert self.p5.hasPseudoknots()
        assert self.p6.hasPseudoknots()
        assert self.p7.hasPseudoknots()
Example #43
0
 def setUp(self):
     """Pairs SetUp method for all tests"""
     self.Empty = Pairs([])
     self.OneList = Pairs([[1,2]])
     self.OneTuple = Pairs([(1,2)])
     self.MoreLists = Pairs([[2,4],[3,9],[6,36],[7,49]])
     self.MoreTuples = Pairs([(2,4),(3,9),(6,36),(7,49)])
     self.MulNoOverlap = Pairs([(1,10),(2,9),(3,7),(4,12)])
     self.MulOverlap = Pairs([(1,2),(2,3)])
     self.Doubles = Pairs([[1,2],[1,2],[2,3],[1,3]])
     self.Undirected = Pairs([(2,1),(6,4),(1,7),(8,3)])
     self.UndirectedNone = Pairs([(5,None),(None,3)])
     self.UndirectedDouble = Pairs([(2,1),(1,2)])
 
     self.NoPseudo = Pairs([(1,20),(2,19),(3,7),(4,6),(10,15),(11,14)])
     self.NoPseudo2 = Pairs([(1,3),(4,6)])
     #((.(.)).)
     self.p0 = Pairs([(0,6),(1,5),(3,8)])
     #(.((..(.).).))
     self.p1 = Pairs([(0,9),(2,12),(3,10),(5,7)])
     #((.(.(.).)).)
     self.p2 = Pairs([(0,10),(1,9),(3,12),(5,7)])
     #((.((.(.)).).))
     self.p3 = Pairs([(0,9),(1,8),(3,14),(4,13),(6,11)])
     #(.(((.((.))).)).(((.((((..))).)))).)
     self.p4 = Pairs([(0,35),(2,11),(3,10),(4,9),(6,14),(7,13),(16,28),\
         (17,27),(18,26),(20,33),(21,32),(22,31),(23,30)])
     #(.((.).))
     self.p5 = Pairs([(0,5),(2,8),(3,7)])
     self.p6 = Pairs([(0,19),(2,6),(3,5),(8,14),(9,13),(10,12),\
         (16,22),(17,21)])
     self.p7 = Pairs([(0,20),(2,6),(3,5),(8,14),(9,10),(11,16),(12,15),\
         (17,23),(18,22)])
Example #44
0
    def test_toVienna(self):
        """Pairs toVienna() should return a ViennaStructure if possible"""
        a = Pairs([(1,5),(3,4),(6,9),(7,8)]) #normal
        b = Pairs([(0,4),(2,6)]) #pseudoknot
        c = Pairs([(1,6),(3,6),(4,5)]) #conflict
        d = Pairs([(1,6),(3,None)])
        e = Pairs([(1,9),(8,2),(7,3)]) #not directed
        f = Pairs([(1,6),(2,5),(10,15),(14,11)]) # not directed

        self.assertEqual(a.toVienna(10),'.(.())(())')
        self.assertEqual(a.toVienna(13,offset=3),'....(.())(())')
        
        self.assertRaises(PairError,b.toVienna,7) #pseudoknot NOT accepted
        self.assertRaises(Exception,b.toVienna,7) #old test for exception
        self.assertRaises(ValueError,c.toVienna,7)
        
        #pairs containging None are being skipped
        self.assertEquals(d.toVienna(7),'.(....)')
        
        #raises error when trying to insert at non-existing indices
        self.assertRaises(IndexError,a.toVienna,3)

        self.assertEqual(Pairs().toVienna(3),'...')
        
        #test when parsing in the sequence
        self.assertEqual(a.toVienna('ACGUAGCUAG'),'.(.())(())')
        self.assertEqual(a.toVienna(Rna('AACCGGUUAGCUA'), offset=3),\
            '....(.())(())')
       
        self.assertEqual(e.toVienna(10),'.(((...)))')
        self.assertEqual(f.toVienna(20),'.((..))...((..))....')