summaryrefslogtreecommitdiff
path: root/Code/make_target_tf_agris.py
blob: f95a03b4b5403d4c2bab1cfc2418505f432b2d80 (plain)
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
# Make target_tf from AtRegNet.txt and from AtRegNet.csv (much bigger).
# Usage: python3 make_target_tf_agris.py > ../Data/information/target_tf_agris.txt
# Last modified on 16 Feb 2021 by Hui Lan <lanhui@zjnu.edu.cn>

import codecs, sys

class GeneIdValidator:
    def __init__(self, organism):
        self.org = organism
    def isvalid(self, ID):
        if self.org == 'ath':
            return ID.startswith('AT') and len(ID) == 9 and ID[3] == 'G'
        else:
            return False


def make_dictionary(fname, separator):
    sample_id = 'C0000000000001'

    id_validator = GeneIdValidator('ath')

    f = codecs.open(fname, encoding='utf-8', errors='ignore')
    lines = f.readlines()
    f.close()
    
    d = {}
    duplicate = 0
    for line in lines[1:]:
        line = line.strip()
        lst = line.split(separator)
        if len(lst) >= 5:
            tf0     = lst[1].upper().strip()
            target0 = lst[4].upper().strip()
            tf_lst = [x.strip() for x in tf0.split('/') if id_validator.isvalid(x)]
            target_lst = [x.strip() for x in target0.split('/') if id_validator.isvalid(x)]
            for tf in tf_lst:
                for target in target_lst:
                    k = target + '.' + tf
                    if k in d:
                        duplicate += 1
                    else:
                        d[k] = [target, tf, sample_id]

    return duplicate, d


if __name__ == '__main__':
    duplicate1, d = make_dictionary('../Data/information/AtRegNet.txt', '\t')  # OSU AtRegNet, an older version.  Most are confirmed tf target pairs???
    duplicate2, d2 = make_dictionary('../Data/information/AtRegNet2.csv', ',') # OSU AtRegNet downloaded in Feb 2021.  This file includes much more tf target pairs (confirmed, unconfirmed, dap-seq, etc)    
    d.update(d2) # combine two dictionaries into one.
    
    print('pairs: %d' % len(d), file=sys.stderr)
    print('duplicate: %d' % (duplicate1 + duplicate2), file=sys.stderr)
    for k in sorted(d.keys()):
        print('\t'.join(d[k]))