16359a2028779494d3a6529ea4d31d54ec9f9874 mmaddren Thu Apr 21 14:28:58 2011 -0700 small changes to cv Validation diff --git python/ucscgenomics/rafile/RaFile.py python/ucscgenomics/rafile/RaFile.py index 7f54d3b..784e457 100644 --- python/ucscgenomics/rafile/RaFile.py +++ python/ucscgenomics/rafile/RaFile.py @@ -1,70 +1,77 @@ import sys import re -import OrderedDict +from OrderedDict import * -class RaFile(OrderedDict.OrderedDict): +class RaFile(OrderedDict): """ Stores an Ra file in a set of entries, one for each stanza in the file. """ - def __init__(self, entryType): - self.__entryType = entryType - OrderedDict.OrderedDict.__init__(self) + def __init__(self, filePath=''): + OrderedDict.__init__(self) + if filePath != '': + self.read(filePath) def read(self, filePath): """ Reads an rafile stanza by stanza, and internalizes it. """ file = open(filePath, 'r') - entry = self.__entryType() + #entry = self.__entryType() stanza = list() keyValue = '' for line in file: line = line.strip() if len(stanza) == 0 and (line.startswith('#') or line == ''): self._OrderedDict__ordering.append(line) continue if line != '': stanza.append(line) elif len(stanza) > 0: if keyValue == '': - keyValue, name = entry.readStanza(stanza) + keyValue, name, entry = self.readStanza(stanza) else: - testKey, name = entry.readStanza(stanza) + testKey, name, entry = self.readStanza(stanza) if keyValue != testKey: raise KeyError('Inconsistent Key ' + testKey) if name in self: raise KeyError('Duplicate Key ' + name) self[name] = entry - entry = self.__entryType() + #entry = self.__entryType() stanza = list() if len(stanza) > 0: raise IOError('File is not newline terminated') file.close() + def readStanza(self, stanza): + entry = RaEntry() + val1, val2 = entry.readStanza(stanza) + return val1, val2, entry + + def iter(self): pass def iterkeys(self): for item in self._OrderedDict__ordering: if not(item.startswith('#') or item == ''): yield item def itervalues(self): for item in self._OrderedDict__ordering: if not (item.startswith('#') or item == ''): yield self[item] @@ -75,38 +82,38 @@ yield item, self[item] else: yield [item] def __str__(self): str = '' for item in self.iteritems(): if len(item) == 1: str += item[0].__str__() + '\n' else: str += item[1].__str__() + '\n' return str -class RaEntry(OrderedDict.OrderedDict): +class RaEntry(OrderedDict): """ Holds an individual entry in the RaFile. """ def __init__(self): self._name = '' - OrderedDict.OrderedDict.__init__(self) + OrderedDict.__init__(self) @property def name(self): return self._name def readStanza(self, stanza): """ Populates this entry from a single stanza """ for line in stanza: self.__readLine(line) return self.__readName(stanza[0]) @@ -121,31 +128,35 @@ if len(line.split(' ', 1)) != 2: raise ValueError() names = map(str.strip, line.split(' ', 1)) self._name = names[1] return names def __readLine(self, line): """ Reads a single line from the stanza, extracting the key-value pair """ if line.startswith('#') or line == '': self._OrderedDict__ordering.append(line) else: - raKey, raVal = map(str, line.split(' ', 1)) + raKey = line.split(' ', 1)[0] + raVal = '' + if (len(line.split(' ', 1)) == 2): + raVal = line.split(' ', 1)[1] + #raKey, raVal = map(str, line.split(' ', 1)) self[raKey] = raVal def iter(self): pass def iterkeys(self): for item in self._OrderedDict__ordering: if not (item.startswith('#') or item == ''): yield item def itervalues(self): for item in self._OrderedDict__ordering: