shapes.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. """
  2. Tests of various shape generation against reference examples.
  3. """
  4. import subprocess
  5. import sys
  6. from pathlib import Path
  7. # Import helper function to compare graphs from tests/regressions_tests
  8. sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
  9. from regression_test_helpers import ( # pylint: disable=import-error,wrong-import-position
  10. compare_graphs,
  11. )
  12. shapes = [
  13. "box",
  14. "polygon",
  15. "ellipse",
  16. "oval",
  17. "circle",
  18. "point",
  19. "egg",
  20. "triangle",
  21. "none",
  22. "plaintext",
  23. "plain",
  24. "diamond",
  25. "trapezium",
  26. "parallelogram",
  27. "house",
  28. "pentagon",
  29. "hexagon",
  30. "septagon",
  31. "octagon",
  32. "note",
  33. "tab",
  34. "folder",
  35. "box3d",
  36. "component",
  37. "cylinder",
  38. "rect",
  39. "rectangle",
  40. "square",
  41. "star",
  42. "doublecircle",
  43. "doubleoctagon",
  44. "tripleoctagon",
  45. "invtriangle",
  46. "invtrapezium",
  47. "invhouse",
  48. "underline",
  49. "Mdiamond",
  50. "Msquare",
  51. "Mcircle",
  52. # biological circuit shapes
  53. # gene expression symbols
  54. "promoter",
  55. "cds",
  56. "terminator",
  57. "utr",
  58. "insulator",
  59. "ribosite",
  60. "rnastab",
  61. "proteasesite",
  62. "proteinstab",
  63. # dna construction symbols
  64. "primersite",
  65. "restrictionsite",
  66. "fivepoverhang",
  67. "threepoverhang",
  68. "noverhang",
  69. "assembly",
  70. "signature",
  71. "rpromoter",
  72. "larrow",
  73. "rarrow",
  74. "lpromoter",
  75. ]
  76. output_types = ["gv", "svg", "xdot"]
  77. def generate_shape_graph(shape, output_type):
  78. """
  79. Generate a graph of the given shape and output format.
  80. """
  81. if not Path("output").exists():
  82. Path("output").mkdir(parents=True)
  83. output_file = Path("output") / f"{shape}.{output_type}"
  84. input_graph = f'graph G {{ a [label="" shape={shape}] }}'
  85. try:
  86. subprocess.run(
  87. ["dot", f"-T{output_type}", "-o", output_file],
  88. input=input_graph.encode("utf_8"),
  89. check=True,
  90. )
  91. except subprocess.CalledProcessError:
  92. print(f"An error occurred while generating: {output_file}")
  93. sys.exit(1)
  94. if output_type == "svg":
  95. # Remove the number in "Generated by graphviz version <number>"
  96. # to able to compare the output to the reference. This version
  97. # number is different for every Graphviz compilation.
  98. with open(output_file, "rt", encoding="utf-8") as file:
  99. lines = file.readlines()
  100. with open(output_file, "wt", encoding="utf-8") as file:
  101. for line in lines:
  102. if "<!-- Generated by graphviz version " in line:
  103. file.write("<!-- Generated by graphviz version\n")
  104. else:
  105. file.write(line)
  106. failures = 0
  107. for s in shapes:
  108. for o in output_types:
  109. generate_shape_graph(s, o)
  110. if not compare_graphs(s, o):
  111. failures += 1
  112. print("")
  113. print('Results for "shapes" regression test:')
  114. print(f" Number of tests: {len(shapes) * len(output_types)}")
  115. print(f" Number of failures: {failures}")
  116. if not failures == 0:
  117. sys.exit(1)