コード例 #1
0
ファイル: column.py プロジェクト: chungtseng/pycogent
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
コード例 #2
0
ファイル: pairs_util.py プロジェクト: chungtseng/pycogent
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
コード例 #3
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
コード例 #4
0
ファイル: pairs_util.py プロジェクト: chungtseng/pycogent
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
コード例 #5
0
ファイル: column.py プロジェクト: yatisht/pycogent
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
コード例 #6
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
コード例 #7
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
コード例 #8
0
ファイル: bpseq.py プロジェクト: GavinHuttley/pycogent
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
コード例 #9
0
ファイル: ct.py プロジェクト: miklou/pycogent
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 
コード例 #10
0
ファイル: ct.py プロジェクト: mikerobeson/pycogent
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 
コード例 #11
0
ファイル: pairs_util.py プロジェクト: chungtseng/pycogent
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
コード例 #12
0
ファイル: pairs_util.py プロジェクト: chungtseng/pycogent
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
コード例 #13
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
コード例 #14
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 equal(g, up).any() or equal(g, down).any():
            continue

        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