Saving the resultsΒΆ

If you want to save the results as a meaningful filename for later use, use the BedTool.saveas() method. This does a copy operation on the file pointed to by the BedTool. This method also lets you optionally specify a trackline for directly uploading to the UCSC Genome Browser, instead of opening up the files afterward and manually adding a trackline:

>>> c = a_with_b.saveas('intersection-of-a-and-b.bed', trackline='track name="a and b"')
>>> print(c.fn)
intersection-of-a-and-b.bed


>>> # opening the underlying file shows the track line
>>> print(open(c.fn).read())
track name="a and b"
chr1        155     200     feature2        0       +
chr1        155     200     feature3        0       -
chr1        900     901     feature4        0       +


>>> # printing file-based BedTool objects will not print the track line
>>> print(c)
chr1        155     200     feature2        0       +
chr1        155     200     feature3        0       -
chr1        900     901     feature4        0       +

Note that the BedTool.saveas() method returns a new BedTool object which points to the newly created file on disk. This allows you to insert a BedTool.saveas() call in the middle of a chain of commands (described in another section below).

Alternatively, if you do not want to add a track line, you can use the BedTool.moveto() method which can be much faster, especially on larger files. This does rename operation rather than a copy operation, which means that trying to call BedTool methods on the original will no longer work because the underlying file no longer exists:

>>> d = a_with_b.moveto('another_location.bed')