allpy
changeset 825:4f896db3531d
allpy.fileio. markup filetype now allows either - or _ to be used in headers interchangeably [closes #89]
author | Daniil Alexeyevsky <dendik@kodomo.fbb.msu.ru> |
---|---|
date | Fri, 15 Jul 2011 18:01:31 +0400 |
parents | 41f7241c2aeb |
children | 18119191a4c8 |
files | allpy/fileio.py allpy/markups.py |
diffstat | 2 files changed, 21 insertions(+), 12 deletions(-) [+] |
line diff
1.1 --- a/allpy/fileio.py Fri Jul 15 17:13:01 2011 +0400 1.2 +++ b/allpy/fileio.py Fri Jul 15 18:01:31 2011 +0400 1.3 @@ -102,7 +102,8 @@ 1.4 record type. Header is a sequence of lines, each in format `key: value`. 1.5 Content, if present, is separated from header with an empty line. 1.6 1.7 - Type names and header key names are case-insensitive. 1.8 + Type names and header key names are case-insensitive and '-' and '_' in 1.9 + them are equivalent. 1.10 1.11 Known record types now are: 1.12 1.13 @@ -112,14 +113,14 @@ 1.14 1.15 Example:: 1.16 1.17 - sequence_markup 1.18 - sequence_name: cyb5_mouse 1.19 - sequence_description: 1.20 + sequence-markup 1.21 + sequence-name: cyb5_mouse 1.22 + sequence-description: 1.23 name: pdb_residue_number 1.24 type: SequencePDBResidueNumberMarkup 1.25 markup: -,12,121,122,123,124,13,14,15,-,-,16 1.26 1.27 - alignment_markup 1.28 + alignment-markup 1.29 name: geometrical_core 1.30 type: AlignmentGeometricalCoreMarkup 1.31 markup: -,-,-,-,+,+,+,-,-,-,+,+,-,-,-,- 1.32 @@ -161,9 +162,10 @@ 1.33 def write_record(self, record): 1.34 """Write record to file. Add new line before every but first record.""" 1.35 self.write_empty_line() 1.36 - self.file.write('%s\n' % record['type']) 1.37 + self.file.write('%s\n' % self.normalize('write', record['type'])) 1.38 del record['type'] 1.39 for key, value in record.items(): 1.40 + key = self.normalize('write', key) 1.41 self.file.write('%s: %s\n' % (key, value)) 1.42 1.43 def write_empty_line(self): 1.44 @@ -207,14 +209,14 @@ 1.45 1.46 def read_record(self, alignment, type): 1.47 """Read record headers and record payload.""" 1.48 - type = type.strip().lower() 1.49 + type = self.normalize('read', type) 1.50 record = {'type': type} 1.51 for line in self.file: 1.52 if line.strip() == "": 1.53 self.read_payload(alignment, record, type) 1.54 return record 1.55 key, value = line.split(':', 1) 1.56 - key = key.strip().lower() 1.57 + key = self.normalize('read', key) 1.58 value = value.strip() 1.59 record[key] = value 1.60 return record 1.61 @@ -225,6 +227,13 @@ 1.62 io = File(self.file, record.get('format', 'fasta'), gaps=self.gaps) 1.63 io.read_alignment(alignment) 1.64 1.65 + @staticmethod 1.66 + def normalize(for_what, string): 1.67 + if for_what == 'read': 1.68 + return string.strip().replace('-', '_').lower() 1.69 + if for_what == 'write': 1.70 + return string.strip().replace('_', '-').capitalize() 1.71 + 1.72 class EmbossFile(AlignmentFile): 1.73 """Parser & writer for file formats supported by EMBOSS.""" 1.74
2.1 --- a/allpy/markups.py Fri Jul 15 17:13:01 2011 +0400 2.2 +++ b/allpy/markups.py Fri Jul 15 18:01:31 2011 +0400 2.3 @@ -26,7 +26,7 @@ 2.4 2.5 @classmethod 2.6 def from_record(cls, container, record, name=None): 2.7 - assert record['io-class'] == 'IntMarkup' 2.8 + assert record['io_class'] == 'IntMarkup' 2.9 result = container.add_markup(name, markup_class=cls) 2.10 separator = record.get('separator', ',') 2.11 values = record['markup'].split(separator) 2.12 @@ -42,7 +42,7 @@ 2.13 return "" 2.14 return str(value) 2.15 values = [fmt(self.get(key)) for key in self.sorted_keys()] 2.16 - return {'markup': ','.join(values), 'io-class': 'IntMarkup'} 2.17 + return {'markup': ','.join(values), 'io_class': 'IntMarkup'} 2.18 2.19 class SequenceNumberMarkup(base.SequenceMarkup): 2.20 2.21 @@ -89,7 +89,7 @@ 2.22 2.23 @classmethod 2.24 def from_record(cls, container, record, name=None): 2.25 - assert record['io-class'] == 'SequenceCaseMarkup' 2.26 + assert record['io_class'] == 'SequenceCaseMarkup' 2.27 result = container.add_markup(name, markup_class=cls) 2.28 markup = record['markup'] 2.29 assert markup[0] == markup[-1] == "'" 2.30 @@ -111,7 +111,7 @@ 2.31 markup += monomer.code1.upper() 2.32 elif case == 'lower': 2.33 markup += monomer.code1.lower() 2.34 - return {'markup': "'%s'" % markup, 'io-class': 'SequenceCaseMarkup'} 2.35 + return {'markup': "'%s'" % markup, 'io_class': 'SequenceCaseMarkup'} 2.36 2.37 class SequencePdbResiMarkup(base.SequenceMarkup, IntMarkupMixin): 2.38 name = 'pdb_resi'