Guest User

Untitled

a guest
Apr 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # Python3
  3. #
  4. # Run from command line
  5. #
  6.  
  7. import logging
  8. from argparse import ArgumentParser
  9. from copy import deepcopy, copy
  10. from datetime import timedelta
  11. from math import ceil
  12. from os import remove, makedirs
  13. from os.path import exists
  14. from re import sub
  15. from time import time
  16.  
  17. from PIL import Image, ImageDraw
  18.  
  19. from largearray import Array
  20. import numpy as np
  21.  
  22. uuid = id(time())
  23.  
  24. parser = ArgumentParser()
  25. parser.add_argument("file", help="Location of input file. path/to/file (FASTA file formats supported)")
  26. parser.add_argument("-i", "--image-name",
  27. help="Where to save finished image file. path/to/file (Default: Name_of_input_file.png)")
  28. parser.add_argument("-s", "--dump-size", help="The size of temp files to dump to disk. (Default & Max: 5)", type=int)
  29. parser.add_argument("-t", "--temp", help="Where to dump temp files. path/to/directory/ (Default: .cache/)", type=str)
  30. parser.add_argument("-d", "--debug-file", help="Where to store debug file. path/to/file (Default ./cache/debug.log")
  31. args = parser.parse_args()
  32. filepath = args.file
  33. ipath = ".".join(filepath.split(".")[:-1]) + ".png"
  34. if args.image_name:
  35. ipath = args.image_name
  36. print(ipath)
  37. dsize = 5
  38. if args.dump_size:
  39. dsize = args.dump_size
  40. cachedir = ".cache/"
  41. if args.temp:
  42. cachedir = args.temp
  43. debugpath = '.cache/debug%d.log' % uuid
  44. if args.debug_file:
  45. debugpath = args.debug_file
  46. if not exists(filepath):
  47. raise Exception("Path of input file does not exist")
  48. print("Debug at %s" % debugpath)
  49. if exists(debugpath):
  50. remove(debugpath)
  51. if not exists(cachedir):
  52. makedirs(cachedir)
  53. logging.basicConfig(filename=debugpath, level=logging.DEBUG)
  54. logging.info("Init: %d" % uuid)
  55.  
  56. del parser, ArgumentParser, remove, exists,
  57.  
  58. print("Generating vizualization of %s" % filepath)
  59. starttime = time()
  60. file = open(filepath, 'r')
  61. logging.info("File opened")
  62. logging.info("Serializing %s ..." % filepath)
  63. raw = ''.join([n for n in file.readlines() if not n.startswith('>')]).replace('\n', "").lower()
  64. logging.info("Replaced FASTA info")
  65. file.close()
  66. del file
  67. raw = sub(r'[rykmswbdhv-]', "n", raw) # Handles miscellaneous FASTA characters
  68. raw = sub(r'[^atgcn]', "", raw) # Handles 4 bases and not-known
  69.  
  70. sequence = Array(name="sequence", cachedirectory=cachedir, a=list(raw), maxitems=(dsize * 10))
  71. sequence.trim()
  72. logging.info("Parsed characters (%d items)" % len(sequence))
  73. del sub, raw
  74. endtime = [time()]
  75. print("The input file has been serialized. %s (%d items) Calculating path..." % (
  76. str(timedelta(seconds=(endtime[0] - starttime))), len(sequence)))
  77.  
  78.  
  79. pendingactions = Array(name="pendingactions", cachedirectory=cachedir, maxitems=dsize)
  80. logging.info("%d temp files will be created [pendingactions]" % ceil(len(sequence) / pendingactions.maxitems))
  81.  
  82. action_lookup = {
  83. "a": np.array((0, -1)),
  84. "t": np.array((0, 1)),
  85. "g": np.array((-1, 0)),
  86. "c": np.array((1, 0)),
  87. "n": np.array((0, 0)),
  88. }
  89.  
  90. color_lookup = {
  91. "a": (0, 255, 0),
  92. "t": (255, 0, 0),
  93. "g": (255, 0, 255),
  94. "c": (0, 0, 255),
  95. "n": (0, 0, 0),
  96. }
  97. top_left = np.array((0, 0))
  98. bottom_right = np.array((0, 0))
  99. cursor = np.array((0, 0))
  100. for i in sequence:
  101. # get the actions associated from dict
  102. cursor += action_lookup[i]
  103. bottom_right = np.maximum(cursor, bottom_right)
  104. top_left = np.minimum(cursor, top_left)
  105. pendingactions.append(i)
  106. pendingactions.trim()
  107.  
  108. # Final dimensions of image + 10px border
  109. border = np.array((10, 10))
  110. dim = bottom_right - top_left + 2 * border
  111. endtime.append(time())
  112. print("The path has been calculated. %s Rendering image... %s" % (
  113. str(timedelta(seconds=(endtime[1] - starttime))), "(" + str(dim[0]) + "x" + str(dim[1]) + ")"))
  114.  
  115. with Image.new("RGBA", tuple(dim), None) as img:
  116. logging.info("Initial image created. (%d x %d)" % (dim[0], dim[1]))
  117. draw = ImageDraw.Draw(img)
  118. logging.info("Draw object created")
  119.  
  120. cursor = np.abs(top_left) + border
  121. for i in pendingactions:
  122. cursor += action_lookup[i]
  123. color = color_lookup[i]
  124. draw.point(tuple(cursor), fill=color)
  125. logging.info("Path Drawn")
  126.  
  127. # Start and End points are dynamically sized to the dimensions of the final image
  128. start_cursor = np.abs(top_left) + border
  129. dot_size = np.ceil(np.mean(dim) / 500)
  130. size = np.array((dot_size, dot_size))
  131. draw.ellipse([tuple(start_cursor-size), tuple(start_cursor+size)],
  132. fill=(255, 255, 0), outline=(255, 255, 0)) # yellow
  133. draw.ellipse([tuple(cursor-size), tuple(cursor+size)], fill=(51, 255, 255),
  134. outline=(51, 255, 255)) # neon blue
  135. logging.info("Start and End points drawn")
  136.  
  137. endtime.append(time())
  138. print("The image has been rendered. %s Saving..." % str(timedelta(seconds=(endtime[2] - endtime[1]))))
  139. img.save(ipath, "PNG", optimize=True)
  140. logging.info("Image saved at %s" % ipath)
  141.  
  142. endtime.append(time())
  143. del img, Image
  144. print("Done! %s Image is saved as: %s" % (str(timedelta(seconds=(endtime[3] - endtime[2]))), ipath))
  145. print("Program took %s to run" % str(timedelta(seconds=(endtime[3] - starttime))))
  146. logging.info("%s | %s | %s | %s # Parsing File | Computing Path | Rendering | Saving" % (
  147. str(timedelta(seconds=(endtime[0] - starttime))), str(timedelta(seconds=(endtime[1] - starttime))),
  148. str(timedelta(seconds=(endtime[2] - starttime))), str(timedelta(seconds=(endtime[3] - starttime)))))
Add Comment
Please, Sign In to add comment