Mercurial > repos > immport-devteam > collapse_pop
comparison collapse_pops.py @ 1:a3ae90eb1232 draft default tip
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/collapse_pop commit 1e75b0df5680c0cdd8b2b3e5d4c1f8077b430944"
author | azomics |
---|---|
date | Mon, 22 Jun 2020 16:34:35 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:59f859ea3122 | 1:a3ae90eb1232 |
---|---|
1 #!/usr/bin/env python | |
2 ###################################################################### | |
3 # Copyright (c) 2016 Northrop Grumman. | |
4 # All rights reserved. | |
5 ###################################################################### | |
6 | |
7 from __future__ import print_function | |
8 import sys | |
9 import pandas as pd | |
10 | |
11 from argparse import ArgumentParser | |
12 | |
13 | |
14 def is_int(s): | |
15 try: | |
16 int(s) | |
17 return True | |
18 except ValueError: | |
19 return False | |
20 | |
21 | |
22 def collapse_populations(in_file, out_file, populations, collapse_in, exit_code): | |
23 df = pd.read_table(in_file, dtype={'Population': object}) | |
24 df['new_population'] = df.Population | |
25 | |
26 for i, sets_pop in enumerate(populations): | |
27 df.loc[df['Population'].isin(sets_pop), ['new_population']] = collapse_in[i] | |
28 | |
29 df.Population = df.new_population | |
30 df.drop(['new_population'], inplace=True, axis=1) | |
31 | |
32 df.to_csv(out_file, sep="\t", index=False) | |
33 | |
34 sys.exit(exit_code) | |
35 | |
36 | |
37 if __name__ == "__main__": | |
38 parser = ArgumentParser( | |
39 prog="ExtractPop", | |
40 description="Extract events associated to given population numbers.") | |
41 | |
42 parser.add_argument( | |
43 '-i', | |
44 dest="input_file", | |
45 required=True, | |
46 help="File location for the text file.") | |
47 | |
48 parser.add_argument( | |
49 '-p', | |
50 dest="pops", | |
51 required=True, | |
52 action='append', | |
53 help="List of populations to collapse.") | |
54 | |
55 parser.add_argument( | |
56 '-o', | |
57 dest="output_file", | |
58 required=True, | |
59 help="Name of the output file.") | |
60 | |
61 parser.add_argument( | |
62 '-c', | |
63 dest="collapse_pop", | |
64 required=True, | |
65 action='append', | |
66 help="What to collapse the populations in.") | |
67 | |
68 args = parser.parse_args() | |
69 | |
70 # check populations | |
71 default_values_pop = ["i.e.:2,3,11,25", "default", "Default"] | |
72 default_values_col = ["i.e.:4", "default", "Default"] | |
73 pops = [p for p in args.pops] | |
74 popc = [pc.strip() for pc in args.collapse_pop] | |
75 exit_code = 0 | |
76 # Check sets of pops to collapse | |
77 populations = [] | |
78 total_pops = [] | |
79 for pop_set in pops: | |
80 if pop_set not in default_values_pop: | |
81 tmp_pops = pop_set.split(",") | |
82 for popn in tmp_pops: | |
83 if not is_int(popn): | |
84 sys.exit(6) | |
85 else: | |
86 total_pops.append(int(popn)) | |
87 strp_pops = [p.strip() for p in tmp_pops] | |
88 populations.append(strp_pops) | |
89 else: | |
90 sys.exit(4) | |
91 if len(total_pops) != len(set(total_pops)): | |
92 sys.exit(7) | |
93 # Check pops to collapse in | |
94 collapse_in = [] | |
95 for col_pop in popc: | |
96 if col_pop not in default_values_col: | |
97 if not is_int(col_pop): | |
98 sys.exit(6) | |
99 else: | |
100 if int(col_pop) > 40: | |
101 exit_code = 2 | |
102 collapse_in.append(col_pop) | |
103 else: | |
104 sys.exit(6) | |
105 if len(collapse_in) != len(set(collapse_in)): | |
106 exit_code += 3 | |
107 | |
108 collapse_populations(args.input_file, args.output_file, populations, collapse_in, exit_code) |