2
|
1 #!/usr/bin/env python
|
|
2
|
50
|
3 import argparse
|
|
4 import logging
|
|
5 from subprocess import check_call
|
|
6 import os
|
|
7
|
2
|
8 tool_description = """
|
|
9 Remove spurious events originating from errors in random sequence tags.
|
|
10
|
|
11 This script compares all events sharing the same coordinates. Among each group
|
|
12 of events the maximum number of PCR duplicates is determined. All events that
|
|
13 are supported by less than 10 percent of this maximum count are removed.
|
|
14
|
|
15 Input:
|
|
16 * bed6 file containing crosslinking events with score field set to number of PCR
|
|
17 duplicates
|
|
18
|
|
19 Output:
|
|
20 * bed6 file with spurious crosslinking events removed, sorted by fields chrom,
|
|
21 start, stop, strand
|
|
22
|
|
23 Example usage:
|
|
24 - remove spurious events from spurious.bed and write results to file cleaned.bed
|
50
|
25 rm_spurious_events.py spurious.bed --oufile cleaned.bed
|
2
|
26 """
|
|
27
|
|
28 epilog = """
|
|
29 Author: Daniel Maticzka
|
|
30 Copyright: 2015
|
|
31 License: Apache
|
|
32 Email: maticzkd@informatik.uni-freiburg.de
|
|
33 Status: Testing
|
|
34 """
|
|
35
|
|
36
|
|
37 class DefaultsRawDescriptionHelpFormatter(argparse.ArgumentDefaultsHelpFormatter,
|
|
38 argparse.RawDescriptionHelpFormatter):
|
|
39 # To join the behaviour of RawDescriptionHelpFormatter with that of ArgumentDefaultsHelpFormatter
|
|
40 pass
|
|
41
|
|
42
|
50
|
43 def main():
|
|
44 # parse command line arguments
|
|
45 parser = argparse.ArgumentParser(description=tool_description,
|
|
46 epilog=epilog,
|
|
47 formatter_class=DefaultsRawDescriptionHelpFormatter)
|
|
48 # positional arguments
|
|
49 parser.add_argument(
|
|
50 "events",
|
|
51 help="Path to bed6 file containing alignments.")
|
|
52 # optional arguments
|
|
53 parser.add_argument(
|
|
54 "-o", "--outfile",
|
|
55 required=True,
|
|
56 help="Write results to this file.")
|
|
57 parser.add_argument(
|
|
58 "-t", "--threshold",
|
|
59 type=float,
|
|
60 default=0.1,
|
|
61 help="Threshold for spurious event removal."
|
|
62 )
|
|
63 # misc arguments
|
|
64 parser.add_argument(
|
|
65 "-v", "--verbose",
|
|
66 help="Be verbose.",
|
|
67 action="store_true")
|
|
68 parser.add_argument(
|
|
69 "-d", "--debug",
|
|
70 help="Print lots of debugging information",
|
|
71 action="store_true")
|
|
72 parser.add_argument(
|
|
73 '--version',
|
|
74 action='version',
|
|
75 version='0.1.0')
|
2
|
76
|
50
|
77 args = parser.parse_args()
|
2
|
78
|
50
|
79 if args.debug:
|
|
80 logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(filename)s - %(levelname)s - %(message)s")
|
|
81 elif args.verbose:
|
|
82 logging.basicConfig(level=logging.INFO, format="%(filename)s - %(levelname)s - %(message)s")
|
|
83 else:
|
|
84 logging.basicConfig(format="%(filename)s - %(levelname)s - %(message)s")
|
|
85 logging.info("Parsed arguments:")
|
|
86 logging.info(" alignments: '{}'".format(args.events))
|
|
87 logging.info(" threshold: '{}'".format(args.threshold))
|
|
88 if args.outfile:
|
|
89 logging.info(" outfile: enabled writing to file")
|
|
90 logging.info(" outfile: '{}'".format(args.outfile))
|
|
91 logging.info("")
|
2
|
92
|
50
|
93 # check threshold parameter value
|
|
94 if args.threshold < 0 or args.threshold > 1:
|
|
95 raise ValueError("Threshold must be in [0,1].")
|
|
96
|
|
97 if not os.path.isfile(args.events):
|
|
98 raise Exception("ERROR: file '{}' not found.")
|
|
99
|
|
100 # prepare barcode library
|
|
101 syscall = "cat " + args.events + " | sort -k1,1V -k6,6 -k2,2n -k3,3 -k5,5nr | perl " + os.path.dirname(os.path.realpath(__file__)) + "/rm_spurious_events.pl --frac_max " + str(args.threshold) + "| sort -k1,1V -k2,2n -k3,3n -k6,6 -k4,4 -k5,5nr > " + args.outfile
|
|
102 check_call(syscall, shell=True)
|
|
103
|
|
104
|
|
105 if __name__ == "__main__":
|
|
106 main()
|