Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. from os import listdir
  2. from sys import argv
  3. from multiprocessing.pool import ThreadPool
  4. import multiprocessing
  5. import time
  6. import collections, functools, operator
  7.  
  8.  
  9. def process_file(name):
  10. counts = {'A': 0, 'C': 0, 'G': 0, 'T': 0}
  11. with open(name) as f:
  12. for line in f:
  13. for index in line:
  14. if index.upper() in counts:
  15. counts[index.upper()] += 1
  16. return counts
  17.  
  18.  
  19. dir_path = "influenza/" # Assumes trailing '/'.
  20. try:
  21. filenames = listdir(dir_path)
  22. start_time = time.time()
  23. except FileNotFoundError:
  24. print("No such directory found.")
  25. quit()
  26.  
  27. numthreads = 8
  28. pool = multiprocessing.Pool(processes=numthreads)
  29.  
  30. result_list = pool.map(process_file,
  31. (dir_path+filename for filename in filenames ) )
  32.  
  33. result = dict(functools.reduce(operator.add,
  34. map(collections.Counter, result_list)))
  35.  
  36. print(result)
  37.  
  38.  
  39. print("--- %s seconds ---" % (time.time() - start_time))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement