annotate getHeaders.py @ 1:05440ef97f8b draft default tip

"planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
author azomics
date Tue, 14 Jul 2020 09:46:31 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
1 #!/usr/bin/env python
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
2 ######################################################################
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
3 # Copyright (c) 2016 Northrop Grumman.
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
4 # All rights reserved.
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
5 ######################################################################
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
6 #
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
7 # Version 1.2 - May 2018
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
8 # added leeway for files with different nb of headers
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
9 #
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
10
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
11
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
12 import sys
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
13
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
14 from argparse import ArgumentParser
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
15
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
16
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
17 def print_headers(files, filenames, outfile):
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
18 header_table = {}
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
19 for i, eachfile in enumerate(files):
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
20 with open(eachfile, "r") as ef:
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
21 headers = ef.readline().strip()
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
22 header_table[filenames[i]] = headers.split("\t")
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
23
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
24 h = 0
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
25 for f in header_table:
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
26 j = len(header_table[f]) + 1
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
27 if j > h:
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
28 h = j
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
29
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
30 idx = [str(x) for x in range(1, h)]
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
31
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
32 with open(outfile, "w") as outf:
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
33 outf.write("Index\t")
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
34 outf.write("\t".join(idx) + "\n")
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
35 for f in header_table:
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
36 if len(header_table[f]) < h:
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
37 for k in range(len(header_table[f]), h-1):
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
38 header_table[f].append("")
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
39 sys.stderr.write(str(len(header_table[f])))
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
40 outf.write(f + "\t")
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
41 outf.write("\t".join(header_table[f]) + "\n")
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
42 return
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
43
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
44
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
45 if __name__ == "__main__":
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
46 parser = ArgumentParser(
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
47 prog="GetHeaders",
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
48 description="Gets the headers of all files in given set.")
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
49
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
50 parser.add_argument(
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
51 '-i',
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
52 dest="input_files",
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
53 required=True,
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
54 action='append',
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
55 help="File location for the text files.")
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
56
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
57 parser.add_argument(
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
58 '-n',
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
59 dest="file_names",
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
60 required=True,
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
61 action='append',
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
62 help="File names.")
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
63
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
64 parser.add_argument(
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
65 '-o',
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
66 dest="output_file",
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
67 required=True,
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
68 help="Name of the output file.")
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
69
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
70 args = parser.parse_args()
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
71 input_files = [f for f in args.input_files]
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
72 file_names = [fn for fn in args.file_names]
05440ef97f8b "planemo upload for repository https://github.com/ImmPortDB/immport-galaxy-tools/tree/master/flowtools/check_headers commit 14d780f8710fb0962a85c262d0689a9551f4f8e1"
azomics
parents:
diff changeset
73 print_headers(input_files, file_names, args.output_file)