view allpy/fileio.py @ 520:1785e40b2a2d
blocks3d(): add primary_cliques parameter
if primary_cliques parameter is set,
blocks3d() returns self-overlapping raw cliques (as blocks)
instead of set of self-non-overlapping blocks (default)
it could be used for debugging and testing
author |
boris (kodomo) <bnagaev@gmail.com> |
date |
Sat, 26 Feb 2011 18:05:10 +0300 |
parents |
7ebeae6eea65 |
children |
f5af99997e0e 737b52785e5e |
line source
2 from tempfile import NamedTemporaryFile
7 """ Base class providing alignment/sequence import and export
13 def __init__(self, file):
16 def save_string(self, string, name, description=''):
17 """ Saves given string to file
19 Splits long lines to substrings of length=long_line
20 To prevent this, set long_line=None
24 def get_all_strings(self):
25 """Parse fasta file, remove spaces and newlines from sequence bodies.
27 Return a list of tuples (name, description, sequence_body).
31 def get_string(self, name):
32 """ return tuple (name, description, string) for sequence with name name """
33 for name_test, description, body in self.get_all_strings():
35 return (name_test, description, body)
37 class FastaIo(BaseIo):
38 """ Fasta import and export
41 * long_line - max length of file line while export
42 Splits long lines to substrings of length=long_line
43 To prevent this, set long_line=None
46 def __init__(self, file, long_line=70):
47 BaseIo.__init__(self, file)
48 self.long_line = long_line
50 def save_string(self, string, name, description=''):
52 name += " " + description
53 self.file.write(">%s\n" % name)
55 for i in range(0, len(string) // self.long_line + 1):
56 start = i*self.long_line
57 end = i*self.long_line + self.long_line
58 self.file.write("%s\n" % string[start:end])
60 self.file.write("%s\n" % string)
62 def get_all_strings(self):
63 for part in self.file.read().split("\n>"):
64 header, _, body = part.partition("\n")
65 header = header.lstrip(">").strip()
66 name, _, description = header.partition(" ")
68 description = description.strip()
69 body = util.remove_each(body, " \n\r\t\v")
70 yield (name, description, body)
72 def get_string(self, name):
73 for name_test, description, body in self.get_all_strings():
75 return (name_test, description, body)
78 """ Msf import and export """
80 def __init__(self, file):
81 BaseIo.__init__(self, file)
83 def save_string(self, string, name, description=''):
84 name = name.replace(':', '_') # seqret bug
85 tmp_fasta = NamedTemporaryFile('w', delete=False)
87 os.system("seqret %(msf)s %(fasta)s" % \
88 {'msf': self.file.name, 'fasta': tmp_fasta.name})
89 tmp_fasta = open(tmp_fasta.name, 'a')
90 fasta = FastaIo(tmp_fasta)
91 fasta.save_string(string, name, description)
94 os.system("seqret %(fasta)s msf::%(msf)s" % \
95 {'msf': self.file.name, 'fasta': tmp_fasta.name})
96 os.unlink(tmp_fasta.name)
97 self.file = open(self.file.name)
99 def get_all_strings(self):
100 tmp_fasta = NamedTemporaryFile(delete=False)
101 os.system("seqret %(msf)s %(fasta)s" % \
102 {'msf': self.file.name, 'fasta': tmp_fasta.name})
103 fasta = FastaIo(tmp_fasta)
104 strings = list(fasta.get_all_strings())
105 os.unlink(tmp_fasta.name)