def testWriteStockholm(self): stock = StringIO() with MSAFile(stock, 'w', format='stockholm') as out: for seq in MSAFile(pathDatafile('msa_Cys_knot.sth'), split=False): out.write(seq) stock.seek(0) stock_list = list(MSAFile(stock, format='stockholm')) self.assertListEqual(STOCK_LIST, stock_list)
def testWriteSelex(self): selex = StringIO() with MSAFile(selex, 'w', format='selex') as out: for seq in MSAFile(pathDatafile('msa_Cys_knot.slx')): out.write(seq) selex.seek(0) selex_list = list(MSAFile(selex, format='selex')) self.assertListEqual(SELEX_LIST, selex_list)
def testWriteFasta(self): fasta = StringIO() with MSAFile(fasta, 'w', format='fasta') as out: for seq in MSAFile(pathDatafile('msa_Cys_knot.fasta')): out.write(seq) fasta.seek(0) fasta_list = list(MSAFile(fasta, format='fasta')) self.assertListEqual(FASTA_LIST, fasta_list)
def evol_merge(*msa, **kwargs): import prody from prody import parseMSA, mergeMSA, LOGGER, writeMSA, MSAFile from prody.sequence.msafile import MSAEXTMAP from os.path import splitext if len(msa) < 2: raise ValueError('multiple msa filenames must be specified') msaobj = [] try: msaobj = [parseMSA(fn) for fn in msa] except: raise IOError('failed to parse {0}'.format(fn)) msafile = MSAFile(msa[0]) format = kwargs.get('format') or msafile.format outname = kwargs.get('outname') or (msafile.getTitle() + '_merged' + MSAEXTMAP[msafile.format]) writeMSA(outname, mergeMSA(*msaobj), **kwargs) LOGGER.info('Merged MSA is saved as: {0}'.format(outname))
def evol_filter(msa, *word, **kwargs): import prody from prody import MSAFile, writeMSA, LOGGER from os.path import splitext outname = kwargs.get('outname') if outname is None: outname, ext = splitext(msa) if ext.lower() == '.gz': outname, _ = splitext(msa) outname += '_filtered' + ext single = len(word) == 1 if single: word = word[0] if kwargs.get('startswith', False): if single: filter = lambda label, seq, word=word: label.startswith(word) elif kwargs.get('endswith', False): if single: filter = lambda label, seq, word=word: label.endswith(word) elif kwargs.get('contains', False): if single: filter = lambda label, seq, word=word: word in label elif kwargs.get('equals', False): if single: filter = lambda label, seq, word=word: word == label else: filter = lambda label, seq, word=set(word): label in word else: raise TypeError('one of startswith, endswith, contains, or equals ' 'must be specified') msa = MSAFile(msa, filter=filter, filter_full=kwargs.get('filter_full', False)) LOGGER.info('Filtered MSA is written in file: ' + writeMSA(outname, msa, **kwargs))
def testFasta(self): filename = writeMSA(join(TEMPDIR, 'test.fasta.gz'), FASTA) fasta = list(MSAFile(pathDatafile(filename))) self.assertListEqual(list(FASTA), list(fasta)) if os.path.isfile(filename): os.remove(filename)
except ImportError: from io import StringIO from numpy import array, log, zeros, char from numpy.testing import assert_array_equal, dec from prody.tests.datafiles import * from prody.tests import TEMPDIR from prody import MSA, MSAFile, parseMSA, LOGGER, writeMSA LOGGER.verbosity = None FASTA = parseMSA(pathDatafile('msa_Cys_knot.fasta')) SELEX = parseMSA(pathDatafile('msa_Cys_knot.slx')) STOCK = parseMSA(pathDatafile('msa_Cys_knot.sth')) FASTA_LIST = list(MSAFile(pathDatafile('msa_Cys_knot.fasta'))) SELEX_LIST = list(MSAFile(pathDatafile('msa_Cys_knot.sth'))) STOCK_LIST = list(MSAFile(pathDatafile('msa_Cys_knot.sth'))) class TestMSAFile(TestCase): def testMSAFile(self): self.assertListEqual(FASTA_LIST, SELEX_LIST) self.assertListEqual(FASTA_LIST, STOCK_LIST) def testWriteFasta(self): fasta = StringIO() with MSAFile(fasta, 'w', format='fasta') as out: for seq in MSAFile(pathDatafile('msa_Cys_knot.fasta')):