intersect_biomarkers.py 6.26 KB
Newer Older
Marco Chierici's avatar
Marco Chierici committed
1
2
3
4
5
6
## This code is written by Alessandro Zandona' <zandona@fbk.eu>.

## Requires Python >= 2.7, mlpy >= 3.5


from __future__ import division
7

Marco Chierici's avatar
Marco Chierici committed
8
9
import argparse
import configparser as ConfigParser
10
11
12
import csv
import os.path
import sys
Marco Chierici's avatar
Marco Chierici committed
13
14
from distutils.version import StrictVersion

15
16
17
18
19
20
import matplotlib
import matplotlib.pyplot as plt
import numpy as np

matplotlib.use('Agg')

Alessia Marcolini's avatar
Alessia Marcolini committed
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
parser = argparse.ArgumentParser(
    description='Find the intersection between feature lists and produce Venn diagrams.'
)
parser.add_argument(
    'CONFIGFILE1',
    type=str,
    help='Training experiment configuration file 1 (with info about number of top discriminant features)',
)
parser.add_argument(
    'CONFIGFILE2',
    type=str,
    help='Training experiment configuration file 2 (with info about number of top discriminant features)',
)
parser.add_argument(
    'OUTLIST', type=str, help='Output file for intersected feature list.'
)
parser.add_argument(
    'OUTFILE', type=str, nargs='?', help='Output file for Venn diagram plot.'
)

parser.add_argument(
    '--title1',
    type=str,
    default='List_1',
    nargs='?',
    help='Name for first diagram (default: %(default)s)',
)
parser.add_argument(
    '--title2',
    type=str,
    default='List_2',
    nargs='?',
    help='Name for second diagram (default: %(default)s)',
)
parser.add_argument(
    '--configFile3',
    type=str,
    default='NO',
    nargs='?',
    help='Third configuration file - optional (default: %(default)s)',
)
parser.add_argument(
    '--title3',
    type=str,
    default='List_3',
    nargs='?',
    help='Name for third diagram (default: %(default)s)',
)

__author__ = 'Alessandro Zandona'
__date__ = '15 December 2016'

if len(sys.argv) == 1:
Marco Chierici's avatar
Marco Chierici committed
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
    parser.print_help()
    sys.exit(1)

args = parser.parse_args()
CONFIGFILE1 = vars(args)['CONFIGFILE1']
CONFIGFILE2 = vars(args)['CONFIGFILE2']
OUTFILE = vars(args)['OUTFILE']
OUTLIST = vars(args)['OUTLIST']
title1 = vars(args)['title1']
title2 = vars(args)['title2']
configfile3 = vars(args)['configFile3']
title3 = vars(args)['title3']

config = ConfigParser.RawConfigParser()
config.read(CONFIGFILE1)
if not config.has_section('INPUT'):
    print("%s is not a valid configuration file." % CONFIGFILE1)
    sys.exit(3)

RANK = config.get("OUTPUT", "Borda")
NFEATS = config.getint("OUTPUT", "N_feats")

# Feature lists
fl_1 = np.loadtxt(RANK, dtype=str, delimiter='\t', skiprows=1)
# Features name
feats1 = fl_1[:NFEATS, 1]
# Convert lists into sets
feats1_set = set(feats1)

config.read(CONFIGFILE2)
if not config.has_section('INPUT'):
    print("%s is not a valid configuration file." % CONFIGFILE2)
    sys.exit(3)

RANK = config.get("OUTPUT", "Borda")
NFEATS = config.getint("OUTPUT", "N_feats")

# Feature lists
fl_2 = np.loadtxt(RANK, dtype=str, delimiter='\t', skiprows=1)
# Features name
feats2 = fl_2[:NFEATS, 1]
# Convert lists into sets
feats2_set = set(feats2)

Alessia Marcolini's avatar
Alessia Marcolini committed
118
119
120
121
122
if configfile3 != 'NO':
    config.read(configfile3)
    if not config.has_section('INPUT'):
        print("%s is not a valid configuration file." % CONFIGFILE2)
        sys.exit(3)
Marco Chierici's avatar
Marco Chierici committed
123

Alessia Marcolini's avatar
Alessia Marcolini committed
124
125
    RANK = config.get("OUTPUT", "Borda")
    NFEATS = config.getint("OUTPUT", "N_feats")
Marco Chierici's avatar
Marco Chierici committed
126

Alessia Marcolini's avatar
Alessia Marcolini committed
127
128
129
130
131
132
    # Feature lists
    fl_3 = np.loadtxt(RANK, dtype=str, delimiter='\t', skiprows=1)
    # Features name
    feats3 = fl_3[:NFEATS, 1]
    # Convert lists into sets
    feats3_set = set(feats3)
Marco Chierici's avatar
Marco Chierici committed
133
134
135
136


# Intersection between lists
f1f2 = feats1_set.intersection(feats2_set)
Alessia Marcolini's avatar
Alessia Marcolini committed
137
138
139
if configfile3 != 'NO':
    f1f3 = feats1_set.intersection(feats3_set)
    f2f3 = feats2_set.intersection(feats3_set)
Marco Chierici's avatar
Marco Chierici committed
140
141

# associate to each common feature the position in each lists
Alessia Marcolini's avatar
Alessia Marcolini committed
142
143
# outFile_f1f2=os.path.join(os.path.dirname(OUTFILE),'Intersection_%s_%s.txt' %(title1,title2))
# outw=open(outFile_f1f2, 'w')
144
with open(OUTLIST, 'w') as outw:
Alessia Marcolini's avatar
Alessia Marcolini committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
    writer = csv.writer(outw, delimiter='\t', lineterminator='\n')
    writer.writerow(['Feature', 'Position in %s' % title1, 'Postition in %s' % title2])
    for i in range(len(list(f1f2))):
        # current feature in intersection
        interF = list(f1f2)[i]
        # position of current feature in first list
        idx_list1 = np.where(feats1 == interF)[0][0]
        # position of current feature in second list
        idx_list2 = np.where(feats2 == interF)[0][0]
        writer.writerow([list(f1f2)[i], idx_list1 + 1, idx_list2 + 1])

if configfile3 != 'NO':
    # associate to each common feature the position in each lists
    outFile_f1f3 = os.path.join(
        os.path.dirname(OUTFILE), 'Intersection_%s_%s.txt' % (title1, title3)
    )
    with open(outFile_f1f3, 'w') as outw:
        writer = csv.writer(outw, delimiter='\t', lineterminator='\n')
        writer.writerow(
            ['Feature', 'Position in %s ' % title1, 'Postition in %s ' % title3]
        )
        for i in range(len(list(f1f3))):
            # current feature in intersection
            interF = list(f1f3)[i]
            # position of current feature in first list
            idx_list1 = np.where(feats1 == interF)[0][0]
            # position of current feature in second list
            idx_list3 = np.where(feats3 == interF)[0][0]
            writer.writerow([list(f1f3)[i], idx_list1 + 1, idx_list3 + 1])

    outFile_f2f3 = os.path.join(
        os.path.dirname(OUTFILE), 'Intersection_%s_%s.txt' % (title2, title3)
    )
    with open(outFile_f2f3, 'w') as outw:
        writer = csv.writer(outw, delimiter='\t', lineterminator='\n')
        writer.writerow(
            ['Feature', 'Position in %s ' % title2, 'Postition in %s ' % title3]
        )
        for i in range(len(list(f2f3))):
            # current feature in intersection
            interF = list(f2f3)[i]
            # position of current feature in first list
            idx_list2 = np.where(feats2 == interF)[0][0]
            # position of current feature in second list
            idx_list3 = np.where(feats3 == interF)[0][0]
            writer.writerow([list(f2f3)[i], idx_list2 + 1, idx_list3 + 1])
191

Marco Chierici's avatar
Marco Chierici committed
192

Alessia Marcolini's avatar
Alessia Marcolini committed
193
194
195
196
197
198
199
200
201
202
# # plot Venn diagrams
# if (configfile3 != 'NO'):
#    v3_inter = pltv.venn3([feats1_set, feats2_set, feats3_set], (title1, title2, title3))
#    plt.title('Intersection of top discriminant features from %s, %s and %s' %(title1,title2,title3))
# else:
#    v2_inter = pltv.venn2([feats1_set, feats2_set], (title1, title2))
#    plt.title('Intersection of top discriminant features from %s and %s' %(title1,title2))

# plt.savefig(OUTFILE)
# plt.close()