annotate cwpair2.py @ 1:d4db13c9dd7f draft

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