Exemple #1
0
 def test_make_trans(self):
     """make_trans should return trans table mapping chars to default"""
     a = make_trans()
     self.assertEqual('abc123'.translate(a), 'abc123')
     a = make_trans('a', 'x')
     self.assertEqual('abc123'.translate(a), 'xbc123')
     a = make_trans('ac', 'xa')
     self.assertEqual('abc123'.translate(a), 'xba123')
     a = make_trans('ac', 'xa', '.')
     self.assertEqual('abc123'.translate(a), 'x.a...')
     self.assertRaises(ValueError, make_trans, 'ac', 'xa', 'av')
Exemple #2
0
 def test_make_trans(self):
     """make_trans should return trans table mapping chars to default"""
     a = make_trans()
     self.assertEqual("abc123".translate(a), "abc123")
     a = make_trans("a", "x")
     self.assertEqual("abc123".translate(a), "xbc123")
     a = make_trans("ac", "xa")
     self.assertEqual("abc123".translate(a), "xba123")
     a = make_trans("ac", "xa", ".")
     self.assertEqual("abc123".translate(a), "x.a...")
     self.assertRaises(ValueError, make_trans, "ac", "xa", "av")
Exemple #3
0
 def test_make_trans(self):
     """make_trans should return trans table mapping chars to default"""
     a = make_trans()
     self.assertEqual('abc123'.translate(a), 'abc123')
     a = make_trans('a', 'x')
     self.assertEqual('abc123'.translate(a), 'xbc123')
     a = make_trans('ac', 'xa')
     self.assertEqual('abc123'.translate(a), 'xba123')
     a = make_trans('ac', 'xa', '.')
     self.assertEqual('abc123'.translate(a), 'x.a...')
     self.assertRaises(ValueError, make_trans, 'ac', 'xa', 'av')
Exemple #4
0
#!/usr/bin/env python

from cogent.struct.rna2d import ViennaStructure, wuss_to_vienna
from cogent.util.transform import make_trans

__author__ = "Shandy Wikman"
__copyright__ = "Copyright 2007-2012, The Cogent Project"
__contributors__ = ["Shandy Wikman"]
__license__ = "GPL"
__version__ = "1.5.3-dev"
__maintainer__ = "Shandy Wikman"
__email__ = "*****@*****.**"
__status__ = "Development"

to_vienna_table = make_trans('><', '()')


def consan_parser(lines):
    """
    Takes a series of lines as input.

    Returns a list containing alignment and structure
    ex: [{alignment},[structure]]
    """
    seqs = []
    struct = ''
    pairs = []
    alignment = {}
    for line in lines:
        if sequence(line):
            line = line.split()
Exemple #5
0
        """
        mismatches = 0
        if pairs is None:
            try:
                pairs = sequence.Alphabet.Pairs
            except AttributeError:
                pairs = sequence.Pairs
            
        for up, down in self.directed():
            curr = (sequence[up], sequence[down])
            if curr not in pairs:
                mismatches += 1
        return mismatches
    
        
wuss_to_vienna_table = make_trans('<([{>)]} ', '(((()))) ', '.')

def wuss_to_vienna(data):
    """Converts WUSS format string to Vienna format.

    Any pseudoknots or unrecognized chars will convert to unpaired bases.
    Spaces will be preserved.
    """
    return ViennaStructure(data.translate(wuss_to_vienna_table))


class StructureString(str):
    """Base class for ViennaStructure and WussStructure. Immutable.
    
    StructureString holds a structure and a energy. By default energy is 
    set to None. 
Exemple #6
0
        """
        mismatches = 0
        if pairs is None:
            try:
                pairs = sequence.Alphabet.Pairs
            except AttributeError:
                pairs = sequence.Pairs

        for up, down in self.directed():
            curr = (sequence[up], sequence[down])
            if curr not in pairs:
                mismatches += 1
        return mismatches


wuss_to_vienna_table = make_trans('<([{>)]} ', '(((()))) ', '.')


def wuss_to_vienna(data):
    """Converts WUSS format string to Vienna format.

    Any pseudoknots or unrecognized chars will convert to unpaired bases.
    Spaces will be preserved.
    """
    return ViennaStructure(data.translate(wuss_to_vienna_table))


class StructureString(str):
    """Base class for ViennaStructure and WussStructure. Immutable.
    
    StructureString holds a structure and a energy. By default energy is 
Exemple #7
0
    pseudo - If True pairs will be returned with pseudoknots
             If False pairs will be returned without pseudoknots
    """
    result = []
    struct = str(lines[1]).strip('\n')
    seq = lines[0].strip()
    tmp_pairs,energy = to_pairs(struct)
    tmp_pairs.sort()
    if not pseudo:
        tmp_pairs = opt_single_random(tmp_pairs)
        tmp_pairs.sort()
    result.append([seq,tmp_pairs,energy])
    return result
          
primary_table = make_trans('{[]}','....')
first_table   = make_trans('({[]})','..()..')
second_table  = make_trans('([{}])','..()..')
    

def to_pairs(struct=None):
    """
    Converts structure string in to a pairs object.
    Starts by checking for pseudoknots if pseudoknotted it translates each 
    steam in to vienna notation and from there makes a pairs object. 
    Each pairs object is then joined to form the final pairs object of 
    the entire structure

    Returns a tuple of the pairs object and the energy
    """
    primary = first = second = struct.split(None,2)[0]
Exemple #8
0
    pseudo - If True pairs will be returned with pseudoknots
             If False pairs will be returned without pseudoknots
    """
    result = []
    struct = str(lines[1]).strip('\n')
    seq = lines[0].strip()
    tmp_pairs, energy = to_pairs(struct)
    tmp_pairs.sort()
    if not pseudo:
        tmp_pairs = opt_single_random(tmp_pairs)
        tmp_pairs.sort()
    result.append([seq, tmp_pairs, energy])
    return result


primary_table = make_trans('{[]}', '....')
first_table = make_trans('({[]})', '..()..')
second_table = make_trans('([{}])', '..()..')


def to_pairs(struct=None):
    """
    Converts structure string in to a pairs object.
    Starts by checking for pseudoknots if pseudoknotted it translates each 
    steam in to vienna notation and from there makes a pairs object. 
    Each pairs object is then joined to form the final pairs object of 
    the entire structure

    Returns a tuple of the pairs object and the energy
    """
    primary = first = second = struct.split(None, 2)[0]
Exemple #9
0
__contributors__ = ["Shandy Wikman"]
__license__ = "GPL"
__version__ = "1.9"
__maintainer__ = "Shandy Wikman"
__email__ = "*****@*****.**"
__status__ = "Development"

def nupack_parser(lines=None,pseudo=True):
    """Parser for NUPACK output format

    pseudo - If True pseudoknot will be keept if False it will be removed
    """
    result = line_parser(lines,pseudo)
    return result
    
curly_to_dots_table = make_trans('{}','..')
bracket_to_dots_table = make_trans('()','..')
curly_to_bracket_table = make_trans('{}','()')

def line_parser(lines=None,pseudo=True):
    """Parser for nupack output format

    Returns list containing: sequence, paris and energy
    ex: [[seq,[struct],energy]]
    """
    record = False
    result = []
    SSEList = []    #Sequence,Structure,Energy
    for line in lines:
        if line.startswith('Error'):#Error no structure found
            result = [Pairs([])] #return empty pairs list
Exemple #10
0
#!/usr/bin/env python

from cogent.struct.rna2d   import ViennaStructure,wuss_to_vienna
from cogent.util.transform import make_trans

__author__ = "Shandy Wikman"
__copyright__ = "Copyright 2007-2011, The Cogent Project"
__contributors__ = ["Shandy Wikman"]
__license__ = "GPL"
__version__ = "1.6.0dev"
__maintainer__ = "Shandy Wikman"
__email__ = "*****@*****.**"
__status__ = "Development"

to_vienna_table = make_trans('><','()')

def consan_parser(lines):
    """
    Takes a series of lines as input.

    Returns a list containing alignment and structure
    ex: [{alignment},[structure]]
    """
    seqs = []
    struct = ''
    pairs = []
    alignment = {}
    for line in lines:
        if sequence(line):
            line = line.split()
            name = line[0].strip()
Exemple #11
0
                    c = 0
                    tmp_seq = sline[-1]
                    seq = "".join([seq, tmp_seq])
            elif c == 0:  # struct line (every other line)
                if line.startswith(name):
                    c = 1
                    tmp_struct = sline[-1]
                    struct = "".join([struct, tmp_struct])
    struct, seq = remove_gaps(struct, seq)
    pairs = convert_to_vienna(struct)
    result.append([seq, pairs])

    return result


cove_to_vienna_table = make_trans("><", "()")


def remove_gaps(struct, seq):
    """Remove gaps function

    Some results comes with gaps that need to be removed
    """
    seq = seq.replace("-", "")
    tmp_struct = struct.split()  # removes gaps
    tmp = ""
    for i in range(len(tmp_struct)):
        tmp = "".join([tmp, tmp_struct[i]])  # put struct parts together
    struct = tmp
    if len(struct) != len(seq):  # check so that struct and seq match in length
        raise ValueError, "Sequence length don't match structure length"