-
Notifications
You must be signed in to change notification settings - Fork 1
/
add_word_2_column.py
140 lines (122 loc) · 3.56 KB
/
add_word_2_column.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python
# Copyright (c) 2010 DFCI/HSPH
# Author: Chongzhi Zang
#
# This software is distributable under the terms of the GNU General
# Public License (GPL) v2, the text of which can be found at
# http://www.gnu.org/copyleft/gpl.html. Installing, importing or
# otherwise using this module constitutes acceptance of the terms of
# this License.
#
# Disclaimer
#
# This software is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# Comments and/or additions are welcome (send e-mail to:
import re, os, sys, shutil
from math import *
from string import *
from optparse import OptionParser
import operator
## from a input data list of id, drug name; output id "\t" drug_name_cell.
def item_in_list(a, List):
for item in List:
if re.match(a, item):
return True
return False
def get_list(infile):
f = open(infile, 'r')
List = []
for line in f:
if not re.match("#", line):
line = line.strip()
List.append(line)
f.close()
return List
'''
def process(infile, druglist, outfile):
f = open(infile,'r')
id_list = []
concentration_list = []
alldrug_list = []
for line in f:
if not re.match("#", line):
line = line.strip()
sline = line.split()
drug = ' '.join(sline[2:])
#print drug
#if item_in_list(drug, druglist):
if drug in druglist:
id_list.append(sline[0])
concentration_list.append(atof(sline[1]))
alldrug_list.append(drug)
f.close()
o = open(outfile, 'w')
#current_drug = alldrug_list[0]
#current_con = concentration_list[0]
for i in range(0, len(id_list)):
o.write(id_list[i]+'\t'+alldrug_list[i]+'\n')
#for batch in datadic.keys():
# o.write(datadic[batch]+'\n')
#f.close()
o.close()
'''
def process(infile, druglist, outfile):
f = open(infile,'r')
id_dic = {}
con_dic = {}
for line in f:
if not re.match("#", line):
line = line.strip()
sline = line.split()
drug = ' '.join(sline[2:])
#print drug
#if item_in_list(drug, druglist):
if drug in druglist:
if drug in id_dic.keys():
if atof(sline[1]) > con_dic[drug]:
id_dic[drug] = sline[0]
con_dic[drug] = atof(sline[1])
else:
id_dic[drug] = sline[0]
con_dic[drug] = atof(sline[1])
#id_list.append(sline[0])
#concentration_list.append(atof(sline[1]))
#alldrug_list.append(drug)
f.close()
o = open(outfile, 'w')
#current_drug = alldrug_list[0]
#current_con = concentration_list[0]
for drug in id_dic.keys():
o.write(id_dic[drug]+'\t'+drug+'\n')
#for batch in datadic.keys():
# o.write(datadic[batch]+'\n')
#f.close()
o.close()
def main(argv):
parser = OptionParser()
parser.add_option("-i", "--input_file", action="store", type="string",
dest="in_file", help="input file", metavar="<file>")
parser.add_option("-c", "--cell", action="store", type="string",
dest="cell", help="cell name", metavar="<string>")
parser.add_option("-o", "--output_file_name", action="store", type="string",
dest="out_file", help="output file name", metavar="<file>")
(opt, args) = parser.parse_args(argv)
if len(argv) < 4:
parser.print_help()
sys.exit(1)
f = open(opt.in_file,'r')
o = open(opt.out_file, 'w')
for line in f:
if not re.match("#", line):
line = line.strip()
sline = line.split()
o.write(sline[0] + '\t' + '_'.join(sline[1:]) + '_' + opt.cell + '\n')
f.close()
o.close()
if __name__ == "__main__":
main(sys.argv)