comparison coords2clnt.py @ 2:de4ea3aa1090 draft

Uploaded
author rnateam
date Thu, 22 Oct 2015 10:26:45 -0400
parents
children 0b9aab6aaebf
comparison
equal deleted inserted replaced
1:ae0f58d3318f 2:de4ea3aa1090
1 #!/usr/bin/env python
2
3 tool_description = """
4 Given coordinates of the aligned reads, calculate positions of the crosslinked
5 nucleotides. Crosslinked nts are assumed to be one nt upstream of the 5'-end of
6 the read.
7
8 By default output is written to stdout.
9
10 Input:
11 * bed6 file containing coordinates of aligned reads
12 * bed6 file containing coordinates of crosslinking events
13
14 Example usage:
15 - convert read coordinates from file in.bed to coordinates of the crosslinking
16 events, written to out.bed:
17 coords2clnt.py in.bed --outfile out.bed
18 """
19
20 epilog = """
21 Author: Daniel Maticzka
22 Copyright: 2015
23 License: Apache
24 Email: maticzkd@informatik.uni-freiburg.de
25 Status: Testing
26 """
27
28 import argparse
29 import logging
30 from sys import stdout
31 from pybedtools import BedTool
32 from pybedtools.featurefuncs import five_prime
33 # avoid ugly python IOError when stdout output is piped into another program
34 # and then truncated (such as piping to head)
35 from signal import signal, SIGPIPE, SIG_DFL
36 signal(SIGPIPE, SIG_DFL)
37
38 # parse command line arguments
39 parser = argparse.ArgumentParser(description=tool_description,
40 epilog=epilog,
41 formatter_class=argparse.RawDescriptionHelpFormatter)
42 # positional arguments
43 parser.add_argument(
44 "infile",
45 help="Path to bed input file.")
46 # optional arguments
47 parser.add_argument(
48 "-o", "--outfile",
49 help="Write results to this file.")
50 parser.add_argument(
51 "-v", "--verbose",
52 help="Be verbose.",
53 action="store_true")
54 parser.add_argument(
55 "-d", "--debug",
56 help="Print lots of debugging information",
57 action="store_true")
58 parser.add_argument(
59 '--version',
60 action='version',
61 version='0.1.0')
62
63
64 # handle arguments
65 args = parser.parse_args()
66 if args.debug:
67 logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(filename)s - %(levelname)s - %(message)s")
68 elif args.verbose:
69 logging.basicConfig(level=logging.INFO, format="%(filename)s - %(levelname)s - %(message)s")
70 else:
71 logging.basicConfig(format="%(filename)s - %(levelname)s - %(message)s")
72 logging.info("Parsed arguments:")
73 if args.outfile:
74 logging.info(" outfile: enabled writing to file")
75 logging.info(" outfile: '{}'".format(args.outfile))
76 logging.info(" outfile: '{}'".format(args.outfile))
77 logging.info("")
78
79 # data processing
80 alns = BedTool(args.infile)
81 clnts = alns.each(five_prime, upstream=1, downstream=0)
82
83 # write to file or to stdout
84 if args.outfile:
85 clnts.saveas(args.outfile)
86 else:
87 tmptool = clnts.saveas()
88 logging.debug("results written to temporary file :" + tmptool.fn)
89 tmp = open(tmptool.fn)
90 for line in tmp:
91 stdout.write(line)
92 tmp.close()