1 |
51
|
aaronmk
|
#!/usr/bin/env python
|
2 |
|
|
# Inner-joins two map spreadsheets A->B and B->C to A->C
|
3 |
|
|
|
4 |
|
|
import csv
|
5 |
|
|
import sys
|
6 |
|
|
|
7 |
|
|
def main():
|
8 |
67
|
aaronmk
|
try: _prog_name, map_1_path = sys.argv
|
9 |
51
|
aaronmk
|
except ValueError:
|
10 |
67
|
aaronmk
|
raise SystemExit('Usage: '+sys.argv[0]+' map_1 <map_0 >joined_map')
|
11 |
51
|
aaronmk
|
|
12 |
|
|
# Get map 1
|
13 |
|
|
map_1 = {}
|
14 |
|
|
stream = open(map_1_path, 'rb')
|
15 |
59
|
aaronmk
|
reader = csv.reader(stream)
|
16 |
51
|
aaronmk
|
map_1_in, map_1_out = reader.next()[:2]
|
17 |
|
|
for row in reader:
|
18 |
|
|
if row[1] != '': map_1[row[0]] = row[1]
|
19 |
|
|
stream.close()
|
20 |
|
|
|
21 |
|
|
# Join map 1 to map 0
|
22 |
59
|
aaronmk
|
reader = csv.reader(sys.stdin)
|
23 |
51
|
aaronmk
|
map_0_in, map_0_out = reader.next()[:2]
|
24 |
62
|
aaronmk
|
if not map_0_out == map_1_in: raise SystemExit( 'Map error: '
|
25 |
|
|
'Map 0 output column name doesn\'t match map 1 input column name')
|
26 |
59
|
aaronmk
|
writer = csv.writer(sys.stdout)
|
27 |
51
|
aaronmk
|
writer.writerow([map_0_in, map_1_out])
|
28 |
|
|
for row in reader:
|
29 |
|
|
if row[1] in map_1: writer.writerow([row[0], map_1[row[1]]])
|
30 |
|
|
|
31 |
|
|
main()
|