James Taylor avatar James Taylor committed 3cc3983

Merging David's changes from universe version of this file.

Comments (0)

Files changed (1)

 def parse_header( line ):
     return dict( [ field.split( '=' ) for field in line.split()[1:] ] )
 
+def IntervalReader( f ):
+
+    current_chrom = None
+    current_pos = None
+    current_step = None
+
+    # always for wiggle data
+    strand = '+'
+
+    mode = "bed"
+
+    for line in f:
+        if line.startswith( "track" ) or line.startswith( "#" ) or line.isspace():
+            continue
+        elif line.startswith( "variableStep" ):
+            header = parse_header( line )
+            current_chrom = header['chrom']
+            current_pos = None
+            current_step = None
+            if 'span' in header: current_span = int( header['span'] )
+            else: current_span = 1
+            mode = "variableStep"
+        elif line.startswith( "fixedStep" ):
+            header = parse_header( line )
+            current_chrom = header['chrom']
+            current_pos = int( header['start'] )
+            current_step = int( header['step'] )
+            if 'span' in header: current_span = int( header['span'] )
+            else: current_span = 1
+            mode = "fixedStep"
+        elif mode == "bed":
+            fields = line.split()
+            if len( fields ) > 3:
+                chrom = fields[0]
+                start = int( fields[1] )
+                end = int( fields[2] )
+                val = float( fields[3] )
+                yield chrom, start, end, strand, val
+            else:
+                yield fields[0], fields[1], fields[2], strand, fields[3]
+        elif mode == "variableStep": 
+            fields = line.split()
+            pos = int( fields[0] )
+            val = float( fields[1] )
+            yield current_chrom, pos, pos+current_span, val
+        elif mode == "fixedStep":
+            val = float( line.split()[0] )
+            pos = current_pos
+            yield current_chrom, pos, pos+current_span, strand, val
+        else:
+            raise "Unexpected input line: %s" % line.strip()
+
 def Reader( f ):
 
     current_chrom = None
             mode = "fixedStep"
         elif mode == "bed":
             fields = line.split()
-            chrom = fields[0]
-            start = int( fields[1] )
-            end = int( fields[2] )
-            val = float( fields[3] )
-            for i in range( start, end ):
-                yield chrom, i, val
+            if len( fields ) > 3:
+                chrom = fields[0]
+                start = int( fields[1] )
+                end = int( fields[2] )
+                val = float( fields[3] )
+                for i in range( start, end ):
+                    yield chrom, i, val
+            else:
+                yield fields[0], fields[1], fields[2]
         elif mode == "variableStep": 
             fields = line.split()
             pos = int( fields[0] )
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.