Mercurial > repos > immport-devteam > extract_pop
comparison extractpop.py @ 1:4f28ee74079b draft default tip
"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/extract_pop commit 117c0e4a5c00dfd2e190359badf77d5643fccefa"
| author | azomics |
|---|---|
| date | Fri, 24 Jul 2020 19:24:49 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 0:21b2dc3934ed | 1:4f28ee74079b |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 | |
| 3 ###################################################################### | |
| 4 # Copyright (c) 2016 Northrop Grumman. | |
| 5 # All rights reserved. | |
| 6 ###################################################################### | |
| 7 | |
| 8 from __future__ import print_function | |
| 9 import sys | |
| 10 import pandas as pd | |
| 11 | |
| 12 from argparse import ArgumentParser | |
| 13 | |
| 14 | |
| 15 def is_int(s): | |
| 16 try: | |
| 17 int(s) | |
| 18 return True | |
| 19 except ValueError: | |
| 20 return False | |
| 21 | |
| 22 | |
| 23 def extract_pop(in_file, pop_list, out_file): | |
| 24 df = pd.read_table(in_file, dtype={'Population': object}) | |
| 25 dfout = df.loc[df['Population'].isin(pop_list)] | |
| 26 dfout.to_csv(out_file, sep="\t", index=False) | |
| 27 return | |
| 28 | |
| 29 | |
| 30 def remove_pop(in_file, pop_list, out_file): | |
| 31 df = pd.read_table(in_file, dtype={'Population': object}) | |
| 32 dfout = df.loc[~df['Population'].isin(pop_list)] | |
| 33 dfout.to_csv(out_file, sep="\t", index=False) | |
| 34 return | |
| 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 help="List of populations to extract.") | |
| 53 | |
| 54 parser.add_argument( | |
| 55 '-o', | |
| 56 dest="output_file", | |
| 57 required=True, | |
| 58 help="Name of the output file.") | |
| 59 | |
| 60 parser.add_argument( | |
| 61 '-m', | |
| 62 dest="method", | |
| 63 required=True, | |
| 64 help="What to do with the populations.") | |
| 65 | |
| 66 args = parser.parse_args() | |
| 67 | |
| 68 # check populations | |
| 69 default_values = ["i.e.:2,3,11,25", "default", "Default"] | |
| 70 populations = [] | |
| 71 if args.pops: | |
| 72 if args.pops not in default_values: | |
| 73 tmp_pops = args.pops.split(",") | |
| 74 for popn in tmp_pops: | |
| 75 populations.append(popn.strip()) | |
| 76 else: | |
| 77 sys.exit(2) | |
| 78 for pops in populations: | |
| 79 if not is_int(pops): | |
| 80 sys.exit(3) | |
| 81 if args.method == "selected": | |
| 82 extract_pop(args.input_file, populations, args.output_file) | |
| 83 else: | |
| 84 remove_pop(args.input_file, populations, args.output_file) |
