Skip to content

Instantly share code, notes, and snippets.

@mdshw5
Last active August 29, 2015 14:05
Show Gist options
  • Save mdshw5/4514ee43fba7ebfb9105 to your computer and use it in GitHub Desktop.
Save mdshw5/4514ee43fba7ebfb9105 to your computer and use it in GitHub Desktop.
convert FREEC ratio.txt file to bedGraph format
"""
Chromosome Start Ratio MedianRatio CopyNumber
1 1 -1 -1 5
1 9854 4.28786 2.74942 5
1 19707 3.38082 2.74942 5
1 29560 2.56094 2.74942 5
1 39413 2.87198 2.74942 5
1 49266 2.47 2.74942 5
1 59119 2.62686 2.74942 5
1 68972 2.0109 1.81246 4
1 78825 1.51216 1.81246 4
"""
import argparse
with args.txt as infile, args.bedgraph as outfile:
header = next(infile)
prev_rname = None
prev_start = None
prev_cn = cn
for line in infile:
rname, start, ratio, med, cn = line.split()
if rname == prev_rname:
outfile.write("chr{0}\t{1}\t{2}\t{3}\n".format(rname, start, prev_start, prev_cn))
prev_start = start
prev_cn = cn
elif rname != prev_rname:
prev_rname = rname
prev_start = start
prev_cn = cn
outfile.write("chr{0}\t{1}\t{2}\t{3}\n".format(rname, start, prev_start, prev_cn))
def main():
parser = argparse.ArgumentParser(prog='freec2bedgraph', description="Convert FREEC ratio.txt file to bedgraph format")
parser.add_argument('txt', type=argparse.FileType('r', 1), help="FREEC ratio.txt file (- as stdin)")
parser.add_argument('bedgraph', type=argparse.FileType('w', 4096), help="bedgraph output file (- as stdout)")
args = parser.parse_args()
freec2bedgraph(args)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment