Commit a714e91e authored by Piotr Gawron's avatar Piotr Gawron
Browse files

sorting of bed file implemented

parent 692e453a
.idea/
bin
venv/
*.bed
*.txt.gz
import gzip
import ntpath
import os
import sys
import subprocess
import urllib.request
from pathlib import Path
def transform(input_line):
......@@ -22,15 +26,18 @@ def transform(input_line):
for i in range(0, int(block_count)):
block_starts.append(block_starts_tmp[i])
block_sizes.append(str(int(block_ends_tmp[i]) - int(block_starts_tmp[i])))
output = [chromosome, chromosome_start, chromosome_end, name, score, strand, thick_start, thick_end, item_rgb,
output_line = [chromosome, chromosome_start, chromosome_end, name, score, strand, thick_start, thick_end, item_rgb,
block_count, ",".join(block_sizes), ",".join(block_starts)]
return output
return output_line
inputFile = sys.argv[1]
outputFile = ntpath.basename(inputFile).replace(".txt.gz", "") + ".bed"
basename = ntpath.basename(inputFile).replace(".txt.gz", "")
output_file_unsorted = basename + ".bed"
output = open(outputFile, "w")
output = open(output_file_unsorted, "w")
print('Generating bed file: ' + output_file_unsorted)
with gzip.open(inputFile, 'rt') as hIN:
for line in hIN:
......@@ -38,3 +45,25 @@ with gzip.open(inputFile, 'rt') as hIN:
output_line = transform(F)
print("\t".join(output_line), file=output)
output.close()
if not Path('bin').is_dir():
os.mkdir('bin')
def downloadUcscFile(filename):
if not Path('bin/' + filename).is_file():
print('Beginning file download ' + filename + '...')
url = 'http://hgdownload.soe.ucsc.edu/admin/exe/linux.x86_64/' + filename
urllib.request.urlretrieve(url, 'bin/' + filename)
os.chmod('bin/' + filename, 0o755)
downloadUcscFile("sortBed")
output_file_sorted = basename + "-sorted.bed"
print('Sorting bed file: ')
subprocess.call(["bin/sortBed", output_file_unsorted, output_file_sorted])
downloadUcscFile("bedToBigBed")
downloadUcscFile("fetchChromSizes")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment