Project

General

Profile

« Previous | Next » 

Revision 177

Renamed join_maps to join

View differences:

scripts/util/join_maps
1
#!/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
    try: _prog_name, map_1_path = sys.argv
9
    except ValueError:
10
        raise SystemExit('Usage: '+sys.argv[0]+' map_1 <map_0 >joined_map')
11
    
12
    # Get map 1
13
    map_1 = {}
14
    stream = open(map_1_path, 'rb')
15
    reader = csv.reader(stream)
16
    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
    reader = csv.reader(sys.stdin)
23
    writer = csv.writer(sys.stdout)
24
    cols = reader.next()
25
    if not cols[1] == map_1_in: raise SystemExit( 'Map error: '
26
        'Map 0 output column name doesn\'t match map 1 input column name')
27
    cols[1] = map_1_out
28
    writer.writerow(cols)
29
    for row in reader:
30
        try: row[1] = map_1[row[1]]
31
        except KeyError: continue # skip row
32
        writer.writerow(row)
33

  
34
main()
35 0

  
scripts/util/join
1
#!/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
    try: _prog_name, map_1_path = sys.argv
9
    except ValueError:
10
        raise SystemExit('Usage: '+sys.argv[0]+' map_1 <map_0 >joined_map')
11
    
12
    # Get map 1
13
    map_1 = {}
14
    stream = open(map_1_path, 'rb')
15
    reader = csv.reader(stream)
16
    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
    reader = csv.reader(sys.stdin)
23
    writer = csv.writer(sys.stdout)
24
    cols = reader.next()
25
    if not cols[1] == map_1_in: raise SystemExit('Map error: '
26
        'Map 0 output column name doesn\'t match map 1 input column name')
27
    cols[1] = map_1_out
28
    writer.writerow(cols)
29
    for row in reader:
30
        try: row[1] = map_1[row[1]]
31
        except KeyError: continue # skip row
32
        writer.writerow(row)
33

  
34
main()
0 35

  
mappings/join_all_vegbank
7 7
for map in *-VegX.*.csv; do
8 8
    type="${map%.*}" # remove extension
9 9
    type="${type#*.}" # remove up to last "."
10
    ../scripts/util/join_maps "VegX-VegBank.$type.csv" <"$map"|\
10
    ../scripts/util/join "VegX-VegBank.$type.csv" <"$map"|\
11 11
    ../scripts/util/sort 1 >"${map/VegX/VegBank}" # sort on output col
12 12
done

Also available in: Unified diff