-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetaData.py
More file actions
73 lines (58 loc) · 1.91 KB
/
Copy pathMetaData.py
File metadata and controls
73 lines (58 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from __future__ import print_function
import sys
import csv
import json
# import pymongo
def parseMiseqSheet(f):
'''
Description: Read sample sheet and put it in json format
In: filename
Out: json object
'''
js = dict()
section = "doc"
keys = list()
with open(f) as csvfile:
fh = csv.reader(csvfile)
for c in fh:
# print(c)
# Which section?
if len(c) == 0: # skip empty
continue
elif c[0] == "": # skip empty
continue
elif c[0] == "[Header]":
section = "Header"
continue
elif c[0] == "[Reads]":
section = "Reads"
continue
elif c[0] == "[Settings]":
section = "Settings"
continue
elif c[0] == "[Data]":
section = "Data"
continue
if section == "Header" or section == "Settings":
js[c[0]] = c[1] # first column is key, second is the value
elif section == "Data":
if len(keys) == 0: # column names -> keys
keys = c[:]
js["Samples"] = list()
else: # data, columns are values
d = dict()
for i in range(len(c)): # first column (c[0]) is the sample name, rest is meta data
d[keys[i]] = c[i]
js["Samples"].append(d)
csvfile.close()
return(js)
if __name__ == '__main__':
if len(sys.argv) < 2:
sys.exit("Usage: " + sys.argv[0] + " Miseq-sample-sheet.csv")
samplesheet = sys.argv[1]
# client = pymongo.MongoClient()
# db = client.immuno
js = parseMiseqSheet(samplesheet)
print(json.dumps(js, indent=4))
# result = db.samples.insert_one(js)
# print("Data stored:", result.inserted_id)