Commits

James Taylor committed 5f2d592

Allow bed_merge_overlapping to take input from both stdin and files

Import and other fixes for python 2.3 compatibility

Comments (0)

Files changed (6)

bed_merge_overlapping.py

 import psyco_full
 import sys
 from bx.bitset import BinnedBitSet
+from itertools import *
 
 bed_filenames = sys.argv[1:]
+if bed_filenames:
+    input = chain( * imap( open, bed_filenames ) )
+else:
+    input = sys.stdin
 
 last_chrom = None
 last_bitset = None
 bitsets = dict() 
 
-for fname in bed_filenames:
-    for line in open( fname ):
+for line in input:
         if line.startswith("#") or line.startswith("track"): continue
         fields = line.split()
         if fields[0] != last_chrom:
-from align.core import *
+from bx.align.core import *
-from align import *
+from bx.align import *
 
 import itertools
 import interval_index_file
         self.indexes = [ Indexed( maf_file, maf_file + ".index" ) for maf_file in maf_filenames ]
     def get( self, src, start, end ):
         blocks = []
-        for index in self.indexes: blocks += index.get( src, start, end )
+        for index in self.indexes: blocks.extend( index.get( src, start, end ) )
         return blocks
     
 class Indexed( object ):
 
     def get( self, src, start, end ):
         intersections = self.indexes.find( src, start, end )
-        return itertools.imap( self.get_maf_at_offset, [ val for start, end, val in intersections ] )
+        return map( self.get_maf_at_offset, [ val for start, end, val in intersections ] )
 
     def get_maf_at_offset( self, offset ):
         if self.f:
 import sys
 
 import sys, random
-from bx import align.maf
+import bx.align.maf
 from optparse import OptionParser
 
 def main():
     ( options, args ) = parser.parse_args()
 
     length = options.length
-    maf_reader = align.maf.Reader( sys.stdin )
-    maf_writer = align.maf.Writer( sys.stdout )
+    maf_reader = bx.align.maf.Reader( sys.stdin )
+    maf_writer = bx.align.maf.Writer( sys.stdout )
 
     for m in maf_reader:
         for chopped in chop( m, length ):

maf_extract_ranges_indexed.py

 
 import cookbook.doc_optparse
 
-from bx import align.maf
+import bx.align.maf
 from bx import misc
 import os
 import sys
         cookbook.doc_optparse.exit()
 
     # Open indexed access to mafs
-    indexes = [ align.maf.Indexed( maf_file, maf_file + ".index" ) for maf_file in maf_files ]
+    index = bx.align.maf.MultiIndexed( maf_files )
 
     # Start MAF on stdout
 
     if dir is None: 
-        out = align.maf.Writer( sys.stdout )
+        out = bx.align.maf.Writer( sys.stdout )
 
     # Iterate over input ranges 
 
             if do_strand: strand = fields[3]
         if prefix: src = prefix + src
         # Find overlap with reference component
-        blocks = []
-        for index in indexes: blocks += index.get( src, start, end )
-        print >>sys.stderr, src, start, end, len( blocks )
+        blocks = index.get( src, start, end )
         # Open file if needed
         if dir:
-            out = align.maf.Writer( open( os.path.join( dir, "%s:%09d-%09d.maf" % ( src, start, end ) ), 'w' ) )
+            out = bx.align.maf.Writer( open( os.path.join( dir, "%s:%09d-%09d.maf" % ( src, start, end ) ), 'w' ) )
         # Write each intersecting block
         if chop:
             for block in blocks: 

maf_thread_for_species.py

 
 import psyco_full
 
-from bx import align.maf
+import bx.align.maf
 import copy
 import sys
 
     except:
         doc_optparse.exit()
 
-    maf_reader = align.maf.Reader( sys.stdin )
-    maf_writer = align.maf.Writer( sys.stdout )
+    maf_reader = bx.align.maf.Reader( sys.stdin )
+    maf_writer = bx.align.maf.Writer( sys.stdout )
 
     if fuse: maf_writer = MafFuser( maf_writer )