Mercurial > repos > immport-devteam > run_flock
comparison 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 | |
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 | |
8 import sys | |
9 import os | |
10 from argparse import ArgumentParser | |
11 import pandas as pd | |
12 from scipy.stats import gmean | |
13 | |
14 | |
15 def run_FLOCK(input_file, method, bins, density, output_file, mfi_file, | |
16 mfi_calc, profile): | |
17 run_command = method + " " + input_file | |
18 if bins: | |
19 run_command += " " + bins | |
20 if density: | |
21 run_command += " " + density | |
22 | |
23 os.system(run_command) | |
24 | |
25 move_command = "mv flock_results.txt " + output_file | |
26 os.system(move_command) | |
27 | |
28 # Here add some way to calculate the count and tack it on to profile file. | |
29 flockdf = pd.read_table(output_file) | |
30 if mfi_calc == "mfi": | |
31 MFIs = flockdf.groupby('Population').mean().round(decimals=2) | |
32 elif mfi_calc == "gmfi": | |
33 MFIs = flockdf.groupby('Population').agg(lambda x: gmean(list(x))).round(decimals=2) | |
34 else: | |
35 MFIs = flockdf.groupby('Population').median().round(decimals=2) | |
36 | |
37 with open(mfi_file, "w") as outf: | |
38 MFIs.to_csv(outf, sep="\t", float_format='%.0f') | |
39 | |
40 (events, columns) = flockdf.shape | |
41 fstats = {} | |
42 fstats['population'] = flockdf.iloc[:, -1:].iloc[:, 0] | |
43 fstats['population_freq'] = fstats['population'].value_counts() | |
44 fstats['population_freq_sort'] = fstats['population_freq'].sort_index() | |
45 fstats['population_per'] = (fstats['population'].value_counts(normalize=True) * 100).round(decimals=2) | |
46 fstats['population_per_sort'] = fstats['population_per'].sort_index() | |
47 fstats['population_all'] = pd.concat([fstats['population_freq_sort'], fstats['population_per_sort']], axis=1) | |
48 fstats['population_all'].columns = ['Count', 'Percentage'] | |
49 fstats['population_all']['Population_ID'] = fstats['population_all'].index | |
50 | |
51 flock_profile = pd.read_table('profile.txt') | |
52 profile_pop = flock_profile.merge(fstats['population_all'], on='Population_ID') | |
53 profile_pop.to_csv(profile, sep="\t", float_format='%.2f', index=False) | |
54 | |
55 # get_profile = "mv profile.txt " + profile | |
56 # os.system(get_profile) | |
57 return | |
58 | |
59 | |
60 if __name__ == "__main__": | |
61 parser = ArgumentParser( | |
62 prog="runFlockMFI", | |
63 description="Run Flock on text file and generate centroid file") | |
64 | |
65 parser.add_argument( | |
66 '-i', | |
67 dest="input_file", | |
68 required=True, | |
69 help="File location for the FCS file.") | |
70 | |
71 parser.add_argument( | |
72 '-m', | |
73 dest="method", | |
74 required=True, | |
75 help="Run flock1 or flock2.") | |
76 | |
77 parser.add_argument( | |
78 '-M', | |
79 dest="mfi_calc", | |
80 required=True, | |
81 help="what to calculate for centroids.") | |
82 | |
83 parser.add_argument( | |
84 '-b', | |
85 dest="bins", | |
86 required=False, | |
87 help="Number of Bins.") | |
88 | |
89 parser.add_argument( | |
90 '-d', | |
91 dest="density", | |
92 required=False, | |
93 help="Density.") | |
94 | |
95 parser.add_argument( | |
96 '-o', | |
97 dest="output_file", | |
98 required=True, | |
99 help="File location for the output file.") | |
100 | |
101 parser.add_argument( | |
102 '-c', | |
103 dest="centroids", | |
104 required=True, | |
105 help="File location for the output centroid file.") | |
106 | |
107 parser.add_argument( | |
108 '-p', | |
109 dest="profile", | |
110 required=True, | |
111 help="File location for the output profile file.") | |
112 | |
113 args = parser.parse_args() | |
114 run_FLOCK(args.input_file, args.method, args.bins, | |
115 args.density, args.output_file, args.centroids, args.mfi_calc, | |
116 args.profile) |