csv_renamer.py 3.82 KB
Newer Older
1
2
3
4
5
6
#!/usr/bin/python

import os
import sys
import csv
import re
Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
7
8
#import unicodedata
#from unidecode import unidecode
9

Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
10
11
12
13
14
"""
EX1=vcfmerger/csv_list_multicolumn.py
EX2=vcfmerger/csv_renamer.py
VCF=1001genomes_snp-short-indel_only_ACGTN.vcf.gz
LST=A_thaliana_master_accession_list_1135_20151008.csv
15

Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
16
17
18
${EX1} ${VCF}
${EX2} ${VCF}.list.csv ${LST} tg_ecotypeid name,othername,CS_number
"""
19

Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
20
def get_translation(intbl, tbl_k, tbl_vs):
21
22
23
24
25
    data      = {}
    atad      = {}
    col_names = None
    tbl_k_id  = None
    tbl_v_ids = None
Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
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
    with open(intbl, 'rb') as fhd:
        reader = csv.reader(fhd, delimiter=',', quotechar='"')
        for ln, cols in enumerate(reader):
            #print "ln", ln, "cols", cols

            if len(cols) == 0:
                continue

            if ln == 0: #header
                print cols
                assert tbl_k in cols, "key   %s not in header %s" % (tbl_k, ", ".join(cols))
                tbl_k_id  = cols.index(tbl_k)

                tbl_v_ids = []
                for tbl_v in tbl_vs:
                    assert tbl_v in cols, "value %s not in header %s" % (tbl_v, ", ".join(cols))
                    tbl_v_ids.append( cols.index(tbl_v) )

            else:
                k  =   cols[tbl_k_id]

                vs = []
                for tbl_v_id in tbl_v_ids:
                    try:
                        c = cols[tbl_v_id]
                        if c not in vs:
                            for s in vs:
                                if s in c:
                                    c = c.replace(s, '')
                                if c in s:
                                    c = c.replace(c, '')
                            vs.append( c )

                    except:
                        print "id", tbl_v_id, "cols", ", ".join(cols), "len", len(cols)
                        sys.exit(1)

                #k  =   unidecode(k)#unicodedata.normalize('NFKD', unidecode(k)).encode('ascii','ignore')
                #vs = [ unidecode(v) for v in vs ]

                v  = "_".join(vs)
Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
68
                #k  = sanitize(k, ' -.,:()=#&;')
69
70
71
72
73
74
75
76
                v  = sanitize(v, ' -.,:()=#&;')

                assert k not in data, "key %s found more than once" % ( k )
                assert v not in atad, "val %s found more than once" % ( v )

                data[k] = v
                atad[v] = k

Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
77
78
79
    return data, atad


Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
80
81
82
83
84
85
86
87
88
89
90
def sanitize(s, k, v="_"):
    for r in k:
        s = s.replace(r, v)

    s  = re.sub(v+'+', v, s)
    s  = s.strip(v)
    s  = s.decode('utf8').encode('ascii', 'backslashreplace')#, 'xmlcharrefreplace')

    return s


Aflitos, Saulo Alves's avatar
Aflitos, Saulo Alves committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
def main():
    try:
        inlst  = sys.argv[1]
        intbl  = sys.argv[2]
        tbl_k  = sys.argv[3]
        tbl_vs = sys.argv[4].split(',')

    except:
        print "<inlist> <in table> <column key name> <column val names>"
        sys.exit(1)

    print "input list             %s" % inlst
    print "input table            %s" % intbl
    print "table column key name  %s" % tbl_k
    print "table column val names %s" % tbl_vs

    data, atad = get_translation(intbl, tbl_k, tbl_vs)

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    print "data"
    for k,v in data.iteritems():
        print " %-7s %s" % (k, v)

    with open(inlst, 'rb') as fhdi:
        reader = csv.reader(fhdi, delimiter='\t', quotechar='"')
        with open(inlst + '.renamed.csv', 'wb') as fhdo:
            writer = csv.writer(fhdo, delimiter='\t', quotechar='"')
            for ln, cols in enumerate(reader):
                if len(cols) == 0:
                    continue

                if cols[0][0] == "#":
                    continue

                assert len(cols) == 3, "wrong number of columns: %d %d %s" % (len(cols), ln, str(cols))

                name = cols[2]

                assert name in data, "name %s not in db" % name

                cols[2] = data[name]

                writer.writerow(cols)



if __name__ == '__main__':
    main()