comparison 0.2.0/bin/qual_confirm.py @ 11:a5f31c44f8c9

planemo upload
author kkonganti
date Mon, 15 Jul 2024 16:11:44 -0400
parents
children
comparison
equal deleted inserted replaced
10:ddf7a172bf30 11:a5f31c44f8c9
1 #!/usr/bin/env python3
2
3 # Kranti Konganti
4
5 import argparse
6 import inspect
7 import logging
8 import os
9 import pprint
10
11
12 # Multiple inheritence for pretty printing of help text.
13 class MultiArgFormatClasses(argparse.RawTextHelpFormatter, argparse.ArgumentDefaultsHelpFormatter):
14 pass
15
16
17 # Main
18 def main() -> None:
19 """
20 This script takes as input two files containing non whitespace lines and will
21 output lines if it is present in both files.
22 """
23
24 # Set logging.
25 logging.basicConfig(
26 format="\n" + "=" * 55 + "\n%(asctime)s - %(levelname)s\n" + "=" * 55 + "\n%(message)s\n\n",
27 level=logging.DEBUG,
28 )
29
30 # Debug print.
31 ppp = pprint.PrettyPrinter(width=55)
32 prog_name = os.path.basename(inspect.stack()[0].filename)
33
34 parser = argparse.ArgumentParser(
35 prog=prog_name, description=main.__doc__, formatter_class=MultiArgFormatClasses
36 )
37
38 required = parser.add_argument_group("required arguments")
39
40 required.add_argument(
41 "-f1",
42 dest="file1",
43 default=False,
44 required=True,
45 help="Absolute UNIX path to file no. 1 containing\nnon white space lines.",
46 )
47 required.add_argument(
48 "-f2",
49 dest="file2",
50 default=False,
51 required=True,
52 help="Absolute UNIX path to file no. 2 containing\nnon white space lines.",
53 )
54 parser.add_argument(
55 "-out",
56 dest="outfile",
57 default="accs_passed.txt",
58 help="The following output file will be created in\nthe current working directory.",
59 )
60
61 args = parser.parse_args()
62 f1 = args.file1
63 f2 = args.file2
64 out = args.outfile
65 f1d = dict()
66 f2d = dict()
67
68 # Basic checks
69
70 if not (os.path.exists(f1) and os.path.exists(f2)):
71 logging.error(
72 f"File {os.path.basename(f1)} or" + f"\nFile {os.path.basename(f2)} does not exist."
73 )
74 exit(1)
75 elif not (os.path.getsize(f1) > 0 and os.path.getsize(f2) > 0):
76 logging.error(
77 f"File {os.path.basename(f1)} or" + f"\nFile {os.path.basename(f2)} is empty."
78 )
79 exit(1)
80
81 with open(f1, "r") as f1_fh:
82 for line in f1_fh:
83 f1d[line.strip()] = 1
84
85 with open(f2, "r") as f2_fh:
86 for line in f2_fh:
87 f2d[line.strip()] = 1
88
89 big = f1d
90 small = f2d
91
92 if len(f1d.keys()) < len(f2d.keys()):
93 big = f2d
94 small = f1d
95
96 with open(out, "w") as out_fh:
97 for line in small.keys():
98 if line in big.keys():
99 out_fh.write(line + "\n")
100
101 f1_fh.close()
102 f2_fh.close()
103 out_fh.close()
104
105
106 if __name__ == "__main__":
107 main()