0
|
1 """
|
|
2 cwpair2.py
|
|
3
|
|
4 Takes a list of called peaks on both strands and produces a list of matched pairs and a list
|
|
5 of unmatched orphans using a specified method for finding matched pairs. Methods for finding
|
|
6 matched pairs are mode, closest, largest or all, where the analysis is run for each method
|
|
7
|
|
8 Input: list of one or more gff format files
|
|
9
|
|
10 Output: files produced for each input/mode combination:
|
|
11 S (simple), D (detailed), O (orphans), P (frequency preview plot), F (final frequency plot),
|
|
12 C (statistics graph), statistics.tabular
|
|
13 """
|
|
14
|
|
15 import argparse
|
|
16 import csv
|
|
17 import cwpair2_util
|
|
18
|
|
19 if __name__ == '__main__':
|
|
20 parser = argparse.ArgumentParser()
|
|
21 parser.add_argument('--input', dest='inputs', action='append', nargs=2, help="Input datasets")
|
|
22 parser.add_argument('--method', dest='method', default='mode', help='Method of finding match.')
|
|
23 parser.add_argument('--up_distance', dest='up_distance', type=int, default=50, help='Distance upstream from a pair.')
|
|
24 parser.add_argument('--down_distance', dest='down_distance', type=int, default=100, help='Distance downstream of a pair.')
|
|
25 parser.add_argument('--binsize', dest='binsize', type=int, default=1, help='Width of bins for plots and mode.')
|
|
26 parser.add_argument('--threshold_format', dest='threshold_format', help='Percentage to filter the 95th percentile.')
|
|
27 parser.add_argument('--relative_threshold', dest='relative_threshold', type=float, default=0.0, help='Percentage to filter the 95th percentile.')
|
|
28 parser.add_argument('--absolute_threshold', dest='absolute_threshold', type=float, default=0.0, help='Absolute value to filter.')
|
|
29 parser.add_argument('--output_files', dest='output_files', default='simple', help='Restrict output dataset collections.')
|
|
30 parser.add_argument('--sort_score', dest='sort_score', default='no', help='Sort output by score.')
|
|
31 parser.add_argument('--statistics_output', dest='statistics_output', help='Statistics output file.')
|
|
32 args = parser.parse_args()
|
|
33
|
|
34 cwpair2_util.create_directories(args.method)
|
|
35
|
|
36 statistics = []
|
|
37 if args.absolute_threshold > 0:
|
|
38 threshold = args.absolute_threshold
|
|
39 elif args.relative_threshold > 0:
|
|
40 threshold = args.relative_threshold / 100.0
|
|
41 else:
|
|
42 threshold = 0
|
|
43 for (dataset_path, hid) in args.inputs:
|
|
44 stats = cwpair2_util.process_file(dataset_path,
|
|
45 hid,
|
|
46 args.method,
|
|
47 threshold,
|
|
48 args.up_distance,
|
|
49 args.down_distance,
|
|
50 args.binsize,
|
|
51 args.output_files,
|
|
52 args.sort_score)
|
|
53 statistics.extend(stats)
|
|
54 # Accumulate statistics.
|
|
55 by_file = {}
|
|
56 for stats in statistics:
|
|
57 # Skip "None" statistics from failed files
|
|
58 if not stats:
|
|
59 continue
|
|
60 path = stats['stats_path']
|
|
61 if path not in by_file:
|
|
62 by_file[path] = []
|
|
63 by_file[path].append(stats)
|
|
64 # Write tabular statistics file.
|
|
65 keys = ['fname', 'final_mode', 'preview_mode', 'perc95', 'paired', 'orphans']
|
|
66 statistics_out = csv.writer(open(args.statistics_output, 'wt'), delimiter='\t')
|
|
67 statistics_out.writerow(keys)
|
|
68 for file_path, statistics in by_file.items():
|
|
69 for stats in statistics:
|
|
70 statistics_out.writerow([stats[key] for key in keys])
|