Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. $cat file_1
  2.  
  3. name_of_sequence C other_information
  4. name_of_sequence C other_information
  5. name_of_sequence C other_information
  6. name_of_sequence D other_information
  7. ...
  8.  
  9. $cat file_2
  10.  
  11. name_of_sequence B other_information
  12. name_of_sequence C other_information
  13. name_of_sequence C other_information
  14. name_of_sequence C other_information
  15. ...
  16.  
  17. $cat file_3
  18.  
  19. name_of_sequence A other_information
  20. name_of_sequence A other_information
  21. name_of_sequence A other_information
  22. name_of_sequence A other_information
  23. ...
  24.  
  25. $cat .csv/.tsv output:
  26.  
  27. taxa A B C D E
  28. File_1 0 0 3 8 1
  29. File_2 0 1 3 2 9
  30. File_3 7 3 9 0 6
  31.  
  32. import sys
  33. import csv
  34.  
  35. names = set()
  36.  
  37. files = {}
  38.  
  39. for file_name in sys.argv[1:]:
  40. b = files.setdefault(file_name, {})
  41. with open(file_name) as fp:
  42. for line in fp:
  43. x = line.strip().split()
  44. names.add(x[1])
  45. b.setdefault(x[1], [0])[0] += 1
  46.  
  47. names = sorted(list(names))
  48. grid = []
  49. top_line = ['taxa']
  50. grid.append(top_line)
  51. for name in names:
  52. top_line.append(name)
  53. for file_name in sys.argv[1:]:
  54. data = files[file_name]
  55. line = [file_name]
  56. grid.append(line)
  57. for name in names:
  58. line.append(data.get(name, [0])[0])
  59.  
  60. with open('out.csv', 'w') as fp:
  61. writer = csv.writer(fp)
  62. writer.writerows(grid)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement