Mercurial > repos > immport-devteam > auto_collapse_pop
annotate auto_collapse_pops.py @ 1:08b71aee9b80 draft default tip
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
author | azomics |
---|---|
date | Mon, 22 Jun 2020 17:22:03 -0400 |
parents | |
children |
rev | line source |
---|---|
1
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
1 #!/usr/bin/env python |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
2 |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
3 ###################################################################### |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
4 # Copyright (c) 2016 Northrop Grumman. |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
5 # All rights reserved. |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
6 ###################################################################### |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
7 from __future__ import print_function |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
8 import sys |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
9 import pandas as pd |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
10 from argparse import ArgumentParser |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
11 |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
12 |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
13 def auto_collapse(input_file, profile_file, output, report): |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
14 profile_pop_list = {} |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
15 pop_to_collapse = [] |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
16 markers = [] |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
17 with open(profile_file, "r") as pf: |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
18 pffl = pf.readline() |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
19 markers = pffl.strip().split("\t") |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
20 for pfline in pf: |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
21 line = pfline.strip().split("\t") |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
22 pop = line[0] |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
23 profil = "\t".join(line[1:-2]) |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
24 if profil in profile_pop_list: |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
25 profile_pop_list[profil].append(pop) |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
26 else: |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
27 profile_pop_list[profil] = [pop] |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
28 i = 1 |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
29 with open(report, "w") as rt: |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
30 rt.write("New_Population\tFormer_Populations\t") |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
31 rt.write("\t".join(markers[1:-2]) + "\n") |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
32 for profs in profile_pop_list: |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
33 pop_to_collapse.append(profile_pop_list[profs]) |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
34 pop_ls = ", ".join(profile_pop_list[profs]) |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
35 rt.write("\t".join([str(i), pop_ls, profs]) + "\n") |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
36 i += 1 |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
37 df = pd.read_table(input_file, dtype={'Population': object}) |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
38 df['new_population'] = df.Population |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
39 for i, sets_pop in enumerate(pop_to_collapse): |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
40 df.loc[df['Population'].isin(sets_pop), ['new_population']] = i + 1 |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
41 |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
42 df.Population = df.new_population |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
43 df.drop(['new_population'], inplace=True, axis=1) |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
44 df.to_csv(output, sep="\t", index=False) |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
45 |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
46 |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
47 if __name__ == "__main__": |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
48 parser = ArgumentParser( |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
49 prog="auto_pop_collapse_from_profile", |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
50 description="collapses FLOCK populations based on profile.") |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
51 |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
52 parser.add_argument( |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
53 '-i', |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
54 dest="input_file", |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
55 required=True, |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
56 help="FLOCK output file") |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
57 |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
58 parser.add_argument( |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
59 '-o', |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
60 dest="output", |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
61 required=True, |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
62 help="Name of the output file.") |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
63 |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
64 parser.add_argument( |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
65 '-r', |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
66 dest="report", |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
67 required=True, |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
68 help="Name of the report file.") |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
69 |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
70 parser.add_argument( |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
71 '-p', |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
72 dest="profile", |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
73 required=True, |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
74 help="File location for the profile.txt from FLOCK.") |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
75 |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
76 args = parser.parse_args() |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
77 |
08b71aee9b80
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/auto_collapse_pop commit c4cd54a5b7a57f494a82d2a5e9b94f24473fff24"
azomics
parents:
diff
changeset
|
78 auto_collapse(args.input_file, args.profile, args.output, args.report) |