Mercurial > repos > immport-devteam > run_flock
comparison run_flock/runFlockMFI.py @ 2:b6b4d08b6858 draft default tip
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/run_flock commit 7e94637827c3637229f3b568fa7f9d38428d6607"
author | azomics |
---|---|
date | Fri, 17 Jul 2020 09:06:54 -0400 |
parents | 81f9b44f5242 |
children |
comparison
equal
deleted
inserted
replaced
1:81f9b44f5242 | 2:b6b4d08b6858 |
---|---|
1 #!/usr/bin/env python | |
2 ###################################################################### | |
3 # Copyright (c) 2016 Northrop Grumman. | |
4 # All rights reserved. | |
5 ###################################################################### | |
6 # version 2 | |
7 from __future__ import print_function | |
8 | |
9 import sys | |
10 import os | |
11 from argparse import ArgumentParser | |
12 import pandas as pd | |
13 from scipy.stats import gmean | |
14 | |
15 | |
16 def run_FLOCK(input_file, method, bins, density, output_file, mfi_file, | |
17 mfi_calc, profile, tool_directory): | |
18 run_command = tool_directory + "/bin/" + method + " " + input_file | |
19 if bins: | |
20 run_command += " " + bins | |
21 if density: | |
22 run_command += " " + density | |
23 | |
24 os.system(run_command) | |
25 | |
26 move_command = "mv flock_results.txt " + output_file | |
27 os.system(move_command) | |
28 | |
29 # Here add some way to calculate the count and tack it on to profile file. | |
30 flockdf = pd.read_table(output_file) | |
31 if mfi_calc == "mfi": | |
32 MFIs = flockdf.groupby('Population').mean().round(decimals=2) | |
33 elif mfi_calc == "gmfi": | |
34 MFIs = flockdf.groupby('Population').agg(lambda x: gmean(list(x))).round(decimals=2) | |
35 else: | |
36 MFIs = flockdf.groupby('Population').median().round(decimals=2) | |
37 | |
38 with open(mfi_file, "w") as outf: | |
39 MFIs.to_csv(outf, sep="\t", float_format='%.0f') | |
40 | |
41 (events, columns) = flockdf.shape | |
42 fstats = {} | |
43 fstats['population'] = flockdf.iloc[:, -1:].iloc[:, 0] | |
44 fstats['population_freq'] = fstats['population'].value_counts() | |
45 fstats['population_freq_sort'] = fstats['population_freq'].sort_index() | |
46 fstats['population_per'] = (fstats['population'].value_counts(normalize=True) * 100).round(decimals=2) | |
47 fstats['population_per_sort'] = fstats['population_per'].sort_index() | |
48 fstats['population_all'] = pd.concat([fstats['population_freq_sort'], fstats['population_per_sort']], axis=1) | |
49 fstats['population_all'].columns = ['Count', 'Percentage'] | |
50 fstats['population_all']['Population_ID'] = fstats['population_all'].index | |
51 | |
52 flock_profile = pd.read_table('profile.txt') | |
53 profile_pop = flock_profile.merge(fstats['population_all'], on='Population_ID') | |
54 profile_pop.to_csv(profile, sep="\t", float_format='%.2f', index=False) | |
55 | |
56 # get_profile = "mv profile.txt " + profile | |
57 # os.system(get_profile) | |
58 return | |
59 | |
60 | |
61 if __name__ == "__main__": | |
62 parser = ArgumentParser( | |
63 prog="runFlockMFI", | |
64 description="Run Flock on text file and generate centroid file") | |
65 | |
66 parser.add_argument( | |
67 '-i', | |
68 dest="input_file", | |
69 required=True, | |
70 help="File location for the FCS file.") | |
71 | |
72 parser.add_argument( | |
73 '-m', | |
74 dest="method", | |
75 required=True, | |
76 help="Run flock1 or flock2.") | |
77 | |
78 parser.add_argument( | |
79 '-M', | |
80 dest="mfi_calc", | |
81 required=True, | |
82 help="what to calculate for centroids.") | |
83 | |
84 parser.add_argument( | |
85 '-b', | |
86 dest="bins", | |
87 required=False, | |
88 help="Number of Bins.") | |
89 | |
90 parser.add_argument( | |
91 '-d', | |
92 dest="density", | |
93 required=False, | |
94 help="Density.") | |
95 | |
96 parser.add_argument( | |
97 '-o', | |
98 dest="output_file", | |
99 required=True, | |
100 help="File location for the output file.") | |
101 | |
102 parser.add_argument( | |
103 '-t', | |
104 dest="tool_directory", | |
105 required=True, | |
106 help="File location for the output file.") | |
107 | |
108 parser.add_argument( | |
109 '-c', | |
110 dest="centroids", | |
111 required=True, | |
112 help="File location for the output centroid file.") | |
113 | |
114 parser.add_argument( | |
115 '-p', | |
116 dest="profile", | |
117 required=True, | |
118 help="File location for the output profile file.") | |
119 | |
120 args = parser.parse_args() | |
121 run_FLOCK(args.input_file, args.method, args.bins, | |
122 args.density, args.output_file, args.centroids, args.mfi_calc, | |
123 args.profile, args.tool_directory) | |
124 | |
125 sys.exit(0) |