Commits

Davide Cittaro committed 0c34001

Updated bigwig input using new bx python interface

  • Participants
  • Parent commits 8d1cceb

Comments (0)

Files changed (1)

lib/dspomics/ptpileup.py

 import tables
 import pysam
 import bx.wiggle
+import bx.bbi.bigwig_file
 import subprocess
 import numpy as np
 import os
     gfh.close()
   def importProfileFile(self, fname, fmt = 'wig', csize = None):
     fmt = fmt.lower()
-    if fmt == "bdg":
-      # since we use the bx.wiggle module, we can treat bedgraph and wiggle
-      # as they are the same format.
-      fmt = "wig"
-    elif fmt == "bigwig":
-      fmt = "bw"
-    if fmt == "wig":
+    if fmt == "wig" or fmt == "bdg":
       if not csize:
         raise IOError("chromosome sizes must be provided when %s format is specified" % fmt)
       else:
           raise TypeError("A chromosome table should be provided either as dictionary or file")
         for (chrom, size) in csize.items():
           self.addChromosome(chrom, int(size))
-    elif fmt == "bw":
+      for (chrom, position, value) in bx.wiggle.Reader(giterator):
+        self.chromosome(chrom)[position] = value
+        if position % 131072 == 0:
+          self._flush()
+    elif fmt == "bw" or fmt == "bigwig":
       for (chrom, size) in getChromosomeSizesFromBigWig(fname):
         self.addChromosome(chrom, size)
-    
+      fh = open(os.path.expanduser(fname), "rb")
+      bwh = bx.bbi.bigwig_file.BigWigFile(fh)
+      self.chromosome(chrom) = bwh.get_as_array(chrom, 0, csize[chrom])
       giterator = subprocess.Popen(["bigWigToBedGraph", fname, "stdout"], shell = False, stdout = subprocess.PIPE).stdout 
     else:  
       raise TypeError("Format %s not supported, yet" % fmt)
-    for (chrom, position, value) in bx.wiggle.Reader(giterator):
-      self.chromosome(chrom)[position] = value
-      if position % 131072 == 0:
-        self._flush()
 
 
 def getChromosomeSizesFromBigWig(bwname):
   csize = {}
-  bw = open(os.path.expanduser(bwname), "rb")
+  fh = open(os.path.expanduser(bwname), "rb")
   
   # read magic number to guess endianness
   magic = fh.read(4)
       (key, chromId, chromSize) = struct.unpack(endianness + str(keySize) + 'sII', fh.read(keySize + 2 * 4))
       # we have chrom and size
       csize[key.replace('\x00', '')] = chromSize
+    fh.close()  
     return csize  
 #  (key, childOffset) = struct.unpack(endianness + str(keySize) + 'sQ', fh.read(keySize + 8))
 ####