Commits

James Taylor  committed 56abeb2

Writer for transfac format, parser now handles "BS" lines as produced by
CREAD.

  • Participants
  • Parent commits 9bb94a8

Comments (0)

Files changed (2)

File lib/bx/motif/io/transfac.py

         self.basis = None
         self.comment = None
         self.matrix = None
+        self.attributes = None
+        self.sites = None
+        
+transfac_actions = {
+    "AC": ( "store_single", "accession" ),
+    "ID": ( "store_single", "id" ),
+    "DT": ( "store_single_list", "dates" ),
+    "NA": ( "store_single", "name" ),
+    "DE": ( "store_block", "description" ),
+    "BF": ( "store_single_list", "binding_factors" ),
+    "BA": ( "store_block", "basis" ),
+    "CC": ( "store_block", "comment" ),
+    "P0": ( "store_matrix", "matrix" ),
+    # For CREAD format files
+    "AT": ( "store_single_key_value", "attributes" ),
+    "BS": ( "store_single_list", "sites" )
+}
         
 class TransfacReader( object ):
     """
     Reads motifs in TRANSFAC format.
     """
     
+    parse_actions = transfac_actions
+    
     def __init__( self, input ):
         self.input = iter( input )
         self.input_exhausted = False
         if lines:
             return self.parse_record( lines )
     
-    parse_actions = {
-        "AC": ( "store_single", "accession" ),
-        "ID": ( "store_single", "id" ),
-        "DT": ( "store_single_list", "dates" ),
-        "NA": ( "store_single", "name" ),
-        "DE": ( "store_block", "description" ),
-        "BF": ( "store_single_list", "binding_factors" ),
-        "BA": ( "store_block", "basis" ),
-        "CC": ( "store_block", "comment" ),
-        "P0": ( "store_matrix", "matrix" )
-    }
-    
     def parse_record( self, lines ):
         """
         Parse a TRANSFAC record out of `lines` and return a motif.
                     setattr( motif, key, [] )
                 getattr( motif, key ).append( rest )
                 current_line += 1
+            # Add a single line value to a dictionary
+            if action[0] == "store_single_key_value":
+                key = action[1]
+                k, v = rest.strip().split( '=', 1 )
+                if not getattr( motif, key ):
+                    setattr( motif, key, {} )
+                getattr( motif, key )[k] = v
+                current_line += 1
             # Store a block of text
             if action[0] == "store_block":
                 key = action[1]
                 setattr( motif, action[1], matrix )
         # Only return a motif if we saw at least ID or AC or NA
         if motif.id or motif.accession or motif.name:
-            return motif
+            return motif
+            
+class TransfacWriter( object ):
+    """
+    Writes motifs in TRANSFAC format.
+    """
+
+    actions = transfac_actions
+
+    def __init__( self, output ):
+        self.output = output
+        
+    def write( self, motif ):
+        output = self.output
+        for prefix, actions in self.actions.iteritems():
+            action = actions[0]
+            if action == "store_single":
+                key = actions[1]
+                if getattr( motif, key ) is not None:
+                    print >> output, prefix, "  ", getattr( motif, key )
+                    print >> output, "XX"
+            elif action == "store_single_list":
+                key = actions[1]
+                if getattr( motif, key ) is not None:
+                    value = getattr( motif, key )
+                    for v in value:
+                        print >> output, prefix, "  ", v
+                    print >> output, "XX"
+            elif action == "store_single_key_value":
+                key = actions[1]
+                if getattr( motif, key ) is not None:
+                    value = getattr( motif, key )
+                    for k, v in value.iteritems():
+                        print >> output, prefix, "  ", "%s=%s" % ( k, v )
+                    print >> output, "XX"
+            elif action == "store_block":
+                key = actions[1]
+                if getattr( motif, key ) is not None:
+                    value = getattr( motif, key )
+                    for line in value.split( "\n" ):
+                        print >> output, prefix, "  ", line
+                    print >> output, "XX"
+            elif action == "store_matrix":
+                key = actions[1]
+                if getattr( motif, key ) is not None:
+                    matrix = getattr( motif, key )
+                    print >> output, prefix, "  ", " ".join( [ s.rjust(6) for s in matrix.alphabet ] )
+                    for i in range( matrix.width ):
+                        print >> output, "%02d" % ( i + 1 ), "  ", " ".join( [ str(matrix.values[i,matrix.char_to_index[ord(s)]]).rjust(6) for s in matrix.alphabet ] )
+                    print >> output, "XX"
+        print "//"

File lib/bx/motif/pwm.py

     """
     Base class for position specific matrices. 
     """
-    def __init__( self ):
-        self.alphabet = None
-        self.sorted_alphabet = None
-        self.char_to_index = None
-        self.values = None
+    def __init__( self, alphabet=None, sorted_alphabet=None, 
+                  char_to_index=None, values=None ):
+        self.alphabet = alphabet
+        self.sorted_alphabet = sorted_alphabet
+        self.char_to_index = char_to_index
+        self.values = values
 
     @classmethod
     def from_rows( Class, alphabet, rows ):
         # Array
         values = zeros( ( len( rows) , len( alphabet ) ), float32 )
         for i, row in enumerate( rows ):
+            assert len( row ) == len( alphabet )
             for ch, val in zip( alphabet, row ):
                 values[i, char_to_index[ord(ch)]] = val
         # Matrix