/
filename2id.py
132 lines (109 loc) · 4.54 KB
/
filename2id.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import os, argparse, re, sys, xlrd3
import csv
import datetime, shutil, json
#import xlsx
from os.path import join, splitext, isfile
def minimalist_xldate_as_datetime(xldate, datemode):
return (
datetime.datetime(1899, 12, 30)
+ datetime.timedelta(days=xldate + 1462 * datemode)
)
def main (dir, cache_file):
fna = re.compile ("([0-9]+).+\.fna$")
xls = re.compile ("\.xls$")
mapper = re.compile ("\.mapper$")
xlsx = re.compile ("^[^\.]+\.xlsx$")
taginname = re.compile ("^([A-Z][0-9]+)\ ([0-9]+)")
if os.path.exists(cache_file):
with open (cache_file, "r") as fh:
list_of_fna = json.load (fh)
else:
list_of_fna = {}
for root, dirs, files in os.walk(dir):
haz_xls = False
haz_csv = True
xls_map = {}
csv_map = {}
locallist = {}
for file in files:
ma = fna.search (file)
if ma is not None:
base_path = join(root, splitext(file)[0]);
if base_path not in list_of_fna and isfile ( base_path + ".qual"):
pat = taginname.search (file)
if pat is not None:
locallist[base_path] = [pat.group(1), datetime.datetime.strptime(pat.group(2),"%m%d%Y")]
#print(locallist[base_path])
else:
locallist[base_path] = int(ma.group(1))
#print (base_path)
else:
if xls.search (file) is not None:
#print ("\n\n\nHAZ excel! %s %s" % (root,file) )
try:
workbook = xlrd3.open_workbook (join (root, file))
info = workbook.sheet_by_name('Sheet1')
header_row = info.row_values(0)
mbn_id = header_row.index('MBN')
sample_date = header_row.index('Sample Date')
sample_well = header_row.index('Sample Well')
for rownum in range(1,info.nrows):
da_row = info.row_values(rownum)
xls_map [int (da_row[sample_well])] = [da_row[mbn_id], minimalist_xldate_as_datetime (da_row[sample_date], 1)]
except:
pass
haz_xls = True
if mapper.search (file) is not None:
#print ("\n\n\nHAZ! %s %s" % (root,file) )
try:
data_reader = csv.reader(open(join (root, file), 'r'))
for row in data_reader:
if len (row) == 3:
csv_map[int(row[0])] = [row[1],datetime.datetime.strptime(row[2],"%m/%d/%Y")]
else:
raise BaseException ("FAIL")
#print (csv_map)
except:
raise
haz_csv = True
done_by_map = False
for k,l in [(haz_xls, xls_map), (haz_csv, csv_map)]:
if k and len (l) == len (locallist):
for fname, val in locallist.items():
locallist[fname] = l[val]
list_of_fna.update (locallist)
done_by_map = True
break
if not done_by_map:
for f, v in locallist.items ():
if isinstance (v, list):
list_of_fna [f] = v
else:
print ("Unknown sample %s" % f)
dthandler = lambda obj: obj.isoformat() if isinstance(obj, datetime.datetime) else None
with open (cache_file, "w") as fh:
json.dump (list_of_fna, fh, default=dthandler, sort_keys=True, indent=4)
return 0
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='scan the directory of 454 files and process them'
)
parser.add_argument(
'-i', '--input',
metavar='DIR',
type=str,
help='the directory to scan',
required = True
)
parser.add_argument(
'-c', '--cache',
metavar='JSON',
type=str,
help='the file which contains the .json cache file',
required = True
)
args = None
retcode = -1
args = parser.parse_args()
retcode = main(args.input, args.cache)
sys.exit(retcode)