deeptools / pyBigWig

A python extension for quick access to bigWig and bigBed files
MIT License
216 stars 48 forks source link

can't create a bigwig file #54

Closed rfarouni closed 6 years ago

rfarouni commented 6 years ago

When I try to create a bigwig file from numpy data, I get an error. Here is the code

import pyBigWig
import numpy as np
bw = pyBigWig.open("delete.bw", "w")
bw.addHeader([("chr1", 100), ("chr2", 100)])
chroms = np.array(["chr1"] * 4 + ["chr2"] * 4)
starts = np.array([0, 10, 40, 50, 60, 70, 80, 90], dtype=np.int64)
ends = np.array([5, 15, 45, 55, 65, 75, 85, 95], dtype=np.int64)
values0 = np.array(np.random.random_sample(8), dtype=np.float64)
bw.addEntries(chroms, starts, ends=ends, values=values0)
bw.close()

and here is the error

Traceback (most recent call last):
  File "/home/rick/anaconda3/envs/tensorflow/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2910, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-e11cdb4af2f6>", line 9, in <module>
    bw.addEntries(chroms, starts, ends=ends, values=values0)
RuntimeError: The entries you tried to add are out of order, precede already added entries, or otherwise use illegal values.
 Please correct this and try again

I checked whether pyBigWig was installed with numpy

pyBigWig.numpy
Out[6]: 1

Now if I add validate false flag, bw.addEntries(chroms, starts, ends=ends, values=values0, validate=False)

I get this warning but the file saves correctly

[bwClose] There was an error while finishing writing a bigWig file! The output is likely truncated.

The version I am using is pybigwig 0.3.9 np113py36_0 bioconda

dpryan79 commented 6 years ago

I wonder if this is a reproducible example of this https://github.com/deeptools/deepTools/issues/641

Thanks for reporting this issue, I'll have a look!

dpryan79 commented 6 years ago

I can reproduce this with python 3.5 but not 2.7, I suspect it's a weird python 3 issue that I never took care of.

rfarouni commented 6 years ago

@dpryan79 Thanks! Can I use the validate=False flag for now if I know the the order of the chromosomes?

dpryan79 commented 6 years ago

This is caused by the code incorrectly handling lists of python3 Unicode strings. Using validate=False will result in largely useless results (you won't be able to query anything), so you'll need for me to push out a fix (hopefully today or tomorrow).

dpryan79 commented 6 years ago

You've provided a nice test case, so version 0.3.10 with this fixed is likely to come out in a few hours.

rfarouni commented 6 years ago

@dpryan79 Thanks!

dpryan79 commented 6 years ago

Version 0.3.10 is available via pip and I'm adding it to conda now.