Coverage for denofo/comparator/comparator_cli.py: 97%

33 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-09 15:27 +0200

1import argparse 

2import warnings 

3from pathlib import Path 

4from denofo.converter.convert import load_from_json 

5from denofo.utils.ncbiTaxDBcheck import check_NCBI_taxDB 

6from denofo.utils.helpers import compare_two_models, add_extension 

7from denofo.comparator.compare import write_comparison 

8 

9 

10def main(): 

11 """ 

12 The main function of the program including argument parsing. 

13 """ 

14 parser = argparse.ArgumentParser( 

15 description=("Compare two de novo gene annotation files.") 

16 ) 

17 parser.add_argument( 

18 "-m", 

19 "--mode", 

20 type=str, 

21 default="differences", 

22 choices=[ 

23 "differences", 

24 "d", 

25 "dif", 

26 "diff", 

27 "differ", 

28 "similarities", 

29 "s", 

30 "sim", 

31 "simi", 

32 "same", 

33 "similar", 

34 ], 

35 help=( 

36 "The mode of comparison. " 

37 "Options: '(d)ifferences' (default) or '(s)imilarities'." 

38 ), 

39 metavar="\b", # don't show capitalised param name and choices in help 

40 ) 

41 parser.add_argument( 

42 "-i1", 

43 "--input1", 

44 type=Path, 

45 required=True, 

46 help="Path to the first dngf file.", 

47 metavar="\b", 

48 ) 

49 parser.add_argument( 

50 "-i2", 

51 "--input2", 

52 type=Path, 

53 required=True, 

54 help="Path to the second dngf file.", 

55 metavar="\b", 

56 ) 

57 parser.add_argument( 

58 "-o", 

59 "--output", 

60 type=str, 

61 default="", # stdout 

62 help=( 

63 "Path to the output file to store the comparison result. " 

64 "If not provided, the result will be printed to stdout." 

65 ), 

66 metavar="\b", 

67 ) 

68 parser.add_argument( 

69 "-n1", 

70 "--name1", 

71 type=str, 

72 default="dngf_1", 

73 help=( 

74 "The name of the first dngf file in output. " 

75 "This can be used to give the compared studies/datasets a name for the output." 

76 " If the name should contain spaces, please use quotes around the name." 

77 ), 

78 metavar="\b", 

79 ) 

80 parser.add_argument( 

81 "-n2", 

82 "--name2", 

83 type=str, 

84 default="dngf_2", 

85 help=( 

86 "The name of the second dngf file in output. " 

87 "This can be used to give the compared studies/datasets a name for the output." 

88 " If the name should contain spaces, please use quotes around the name." 

89 ), 

90 metavar="\b", 

91 ) 

92 

93 args = parser.parse_args() 

94 

95 # Check the NCBI Taxonomy Database 

96 check_NCBI_taxDB() 

97 

98 warnings.filterwarnings("ignore") 

99 

100 if args.mode in {"d", "dif", "diff", "differ"}: 

101 args.mode = "differences" 

102 elif args.mode in {"s", "sim", "simi", "same", "similar"}: 

103 args.mode = "similarities" 

104 

105 if args.output: 

106 output = add_extension(Path(args.output)) 

107 else: 

108 output = None 

109 

110 # load the dngf files 

111 dngf1 = load_from_json(Path(args.input1)) 

112 dngf2 = load_from_json(Path(args.input2)) 

113 

114 # compare the dngf files and write the output 

115 comparison = compare_two_models(dngf1, dngf2, args.mode) 

116 outstr = write_comparison(comparison, args.mode, output, args.name1, args.name2) 

117 

118 # print the output to stdout if no output file is provided 

119 if not output: 

120 print(outstr) 

121 

122 

123if __name__ == "__main__": 

124 main()