c903336ca8a2b38de169e14f74cb1ec1e0f477eb
mmaddren
  Thu Feb 10 14:42:16 2011 -0800
updated raFile.py
diff --git python/ucscgenomics/rafile/raFile.py python/ucscgenomics/rafile/raFile.py
index 3af110a..d6f3cd7 100644
--- python/ucscgenomics/rafile/raFile.py
+++ python/ucscgenomics/rafile/raFile.py
@@ -1,124 +1,190 @@
-#
-# Holds raDict class which is an internal representation of
-# the data held in the RA file. Essentially just a dict that
-# also has a list to preserve the computationally arbitrary
-# order we want an RA entry to be in.
-#
-
 import sys
 import re
 
 class _OrderedDict(object):
+    """
+    Abstract class containing all shared functionality between the RaFile and
+    RaEntry classes.
+
+    Contains a dictionary to hold each entry, as well as a list to hold the
+    computationally arbitrary ordering we wish to preserve between reads and
+    writes.  
+    """
 
     def __init__(self):
         self._dictionary = dict()
         self._ordering = list()
 
     def add(self, key, value):
+        """
+        Add a key-value pair to the dictionary, and put its key in the list.
+        """
+
         key = key.strip()
 
         if (key in self._dictionary):
             raise KeyError()
             sys.exit(1)
 
         if (key == None or key == ''):
             return
         
         self._dictionary[key] = value
         self._ordering.append(key)
         
     def remove(self, key):
+        """
+        Remove a key-value pair from the dictionary, and the key from the list.
+        """
+
         key = key.strip()
 
         if (key not in self._dictionary):
             raise KeyError()
 
         if (key == None or key == ''):
             return
 
         del self._dictionary[key]
         self._ordering.remove(key)
 
     def getValue(self, key):
+        """
+        Return the value associated with a key in the dictionary.
+        """
+
         if (key not in self._dictionary):
-            raise KeyError()
+            return None
 
         return self._dictionary[key]
 
-class RaFile(_OrderedDict):
+    def getKeyAt(self, index):
+        """
+        Return the key associated with the index in this list
+        """
+
+        if (index > len(self._ordering)):
+            raise IndexError()
+
+        return self._ordering[index]
+
+    def getValueAt(self, index):
+        """
+        Return the value associated with an index in the list.
+        """
+
+        if (index > len(self._ordering)):
+            raise IndexError()
+
+        return self._dictionary[self._ordering[index]]
+
+    def count(self):
+        """
+        Return the length of the list.
+        """
 
-    def addComment(self, comment):
-        if not comment.startswith('#'):
-            raise Exception()
+        return len(self._ordering)
 
-        self._ordering.append(comment)
+
+class RaFile(_OrderedDict):
+    """
+    Stores an Ra file in a set of entries, one for each stanza in the file.
+    """
 
     def read(self, filePath, keyField):
+        """
+        Reads an rafile, separating it by keyField, and internalizes it.
+
+        keyField must be the first field in each entry.
+        """
 
         file = open(filePath, 'r')
         raEntry = RaEntry()
         raKey = None
 
         for line in file:
 
             line = line.strip()
 
-            # remove all commented lines
+            # put all commented lines in the list only
             if (line.startswith('#')):
-                self.addComment(line)
+                self._ordering.append(line)
                 continue
 
             # a blank line indicates we need to move to the next entry
             if (len(line) == 0):
                 raKey = None
                 raEntry = None
                 continue
 
             # check if we're at the first key in a new entry
             if (line.split()[0].strip() == keyField):
                 if len(line.split()) < 2:
                     raise KeyError()
 
                 raKey = line.split(' ', 1)[1].strip()
                 raEntry = RaEntry()
                 raEntry.add(keyField, raKey)
                 self.add(raKey, raEntry)
 
             # otherwise we should be somewhere in the middle of an entry
             elif (raEntry != None):
                 raKey = line.split()[0].strip()
                 raVal = ''
 
                 if len(line.split()) > 1:
                     raVal = line.split(' ', 1)[1].strip()
 
                 raEntry.add(raKey, raVal)
 
             # we only get here if we didn't find the keyField at the beginning
             else:
                 raise KeyError()
 
         file.close()
 
     def write(self):
+        """
+        Write out the entire RaFile.
+        """
+
         print self
 
     def writeEntry(self, key):
+        """
+        Write out a single stanza, specified by key
+        """
+
         print self.getValue(key)
 
+    def __iter__(self):
+        return self
+
     def __str__(self):
         str = ''
         for key in self._ordering:
             if key.startswith('#'):
                 str = str + key + '\n'
             else:
                 str = str + self._dictionary[key].__str__() + '\n'
         return str
 
 class RaEntry(_OrderedDict):
+    """
+    Holds an individual entry in the RaFile.
+    """
+
+    def __init__(self):
+        
+
+    def next(self):
+        
+
+    def __iter__(self):
+        return self
 
     def __str__(self):
         str = ''
         for key in self._ordering:
             str = str + key + ' ' + self._dictionary[key] + '\n'
         return str