annotate micropita_prepare.py @ 0:0de566f21448 draft default tip

v2
author sagun98
date Thu, 03 Jun 2021 18:13:32 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
sagun98
parents:
diff changeset
1 #!/usr/bin/env python
sagun98
parents:
diff changeset
2
sagun98
parents:
diff changeset
3 """
sagun98
parents:
diff changeset
4 Author: George Weingart
sagun98
parents:
diff changeset
5 Description: Prepare parameters to call micropita
sagun98
parents:
diff changeset
6 """
sagun98
parents:
diff changeset
7
sagun98
parents:
diff changeset
8 #####################################################################################
sagun98
parents:
diff changeset
9 #Copyright (C) <2012>
sagun98
parents:
diff changeset
10 #
sagun98
parents:
diff changeset
11 #Permission is hereby granted, free of charge, to any person obtaining a copy of
sagun98
parents:
diff changeset
12 #this software and associated documentation files (the "Software"), to deal in the
sagun98
parents:
diff changeset
13 #Software without restriction, including without limitation the rights to use, copy,
sagun98
parents:
diff changeset
14 #modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
sagun98
parents:
diff changeset
15 #and to permit persons to whom the Software is furnished to do so, subject to
sagun98
parents:
diff changeset
16 #the following conditions:
sagun98
parents:
diff changeset
17 #
sagun98
parents:
diff changeset
18 #The above copyright notice and this permission notice shall be included in all copies
sagun98
parents:
diff changeset
19 #or substantial portions of the Software.
sagun98
parents:
diff changeset
20 #
sagun98
parents:
diff changeset
21 #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
sagun98
parents:
diff changeset
22 #INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
sagun98
parents:
diff changeset
23 #PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
sagun98
parents:
diff changeset
24 #HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
sagun98
parents:
diff changeset
25 #OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
sagun98
parents:
diff changeset
26 #SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
sagun98
parents:
diff changeset
27 #####################################################################################
sagun98
parents:
diff changeset
28
sagun98
parents:
diff changeset
29 __author__ = "George Weingart"
sagun98
parents:
diff changeset
30 __copyright__ = "Copyright 2012"
sagun98
parents:
diff changeset
31 __credits__ = ["George Weingart"]
sagun98
parents:
diff changeset
32 __license__ = "MIT"
sagun98
parents:
diff changeset
33 __maintainer__ = "George Weingart"
sagun98
parents:
diff changeset
34 __email__ = "george.weingart@gmail.com"
sagun98
parents:
diff changeset
35 __status__ = "Development"
sagun98
parents:
diff changeset
36
sagun98
parents:
diff changeset
37 import argparse
sagun98
parents:
diff changeset
38 from cStringIO import StringIO
sagun98
parents:
diff changeset
39 import sys,string,time
sagun98
parents:
diff changeset
40 import os
sagun98
parents:
diff changeset
41 from time import gmtime, strftime
sagun98
parents:
diff changeset
42 from pprint import pprint
sagun98
parents:
diff changeset
43 import subprocess
sagun98
parents:
diff changeset
44 import blist
sagun98
parents:
diff changeset
45 import shlex
sagun98
parents:
diff changeset
46 import tempfile
sagun98
parents:
diff changeset
47
sagun98
parents:
diff changeset
48 ##################################################################################
sagun98
parents:
diff changeset
49 # Modification by George Weingart 5/6/2014 #
sagun98
parents:
diff changeset
50 # Using subprocess to invoke the calls to Micropita #
sagun98
parents:
diff changeset
51 # and allocating the temporary file using trmpfile #
sagun98
parents:
diff changeset
52 ##################################################################################
sagun98
parents:
diff changeset
53
sagun98
parents:
diff changeset
54
sagun98
parents:
diff changeset
55
sagun98
parents:
diff changeset
56
sagun98
parents:
diff changeset
57 ##################################################################################
sagun98
parents:
diff changeset
58 # Decode Parms #
sagun98
parents:
diff changeset
59 ##################################################################################
sagun98
parents:
diff changeset
60 def read_params(x):
sagun98
parents:
diff changeset
61 parser = argparse.ArgumentParser(description='Micropita Annotate Argparser')
sagun98
parents:
diff changeset
62 parser.add_argument('--input', action="store",dest='inputname')
sagun98
parents:
diff changeset
63 parser.add_argument('--output', action="store",dest='outputname')
sagun98
parents:
diff changeset
64 parser.add_argument('-m', action="store",dest='MParameter')
sagun98
parents:
diff changeset
65 parser.add_argument('-n', action="store",dest='NSamples')
sagun98
parents:
diff changeset
66 parser.add_argument('--lastmeta', action="store",dest='lastmeta')
sagun98
parents:
diff changeset
67 parser.add_argument('--stratify_value', action="store",dest='stratify_value')
sagun98
parents:
diff changeset
68
sagun98
parents:
diff changeset
69
sagun98
parents:
diff changeset
70 try:
sagun98
parents:
diff changeset
71 parser.add_argument('--feature_method', action="store",dest='feature_method')
sagun98
parents:
diff changeset
72 except:
sagun98
parents:
diff changeset
73 pass
sagun98
parents:
diff changeset
74 try:
sagun98
parents:
diff changeset
75 parser.add_argument('--targets', action="store",dest='targets')
sagun98
parents:
diff changeset
76 except:
sagun98
parents:
diff changeset
77 pass
sagun98
parents:
diff changeset
78 try:
sagun98
parents:
diff changeset
79 parser.add_argument('--label_value', action="store",dest='label_value')
sagun98
parents:
diff changeset
80 except:
sagun98
parents:
diff changeset
81 pass
sagun98
parents:
diff changeset
82 return parser
sagun98
parents:
diff changeset
83
sagun98
parents:
diff changeset
84
sagun98
parents:
diff changeset
85 ##################################################################################
sagun98
parents:
diff changeset
86 # Main Program #
sagun98
parents:
diff changeset
87 ##################################################################################
sagun98
parents:
diff changeset
88 parser = read_params( sys.argv )
sagun98
parents:
diff changeset
89 results = parser.parse_args()
sagun98
parents:
diff changeset
90 #root_dir = os.environ.get('micropita_SCRIPT_PATH')
sagun98
parents:
diff changeset
91 root_dir = os.path.dirname(os.path.realpath(__file__)) #Find the current directory where the program resides GW 20160810
sagun98
parents:
diff changeset
92
sagun98
parents:
diff changeset
93
sagun98
parents:
diff changeset
94 fname = results.inputname
sagun98
parents:
diff changeset
95 input_file = open(fname,'rU')
sagun98
parents:
diff changeset
96 input_lines = input_file.readlines()
sagun98
parents:
diff changeset
97 input_file.close()
sagun98
parents:
diff changeset
98 table_lines = []
sagun98
parents:
diff changeset
99 for x in input_lines:
sagun98
parents:
diff changeset
100 first_column = x.split('\t')[0]
sagun98
parents:
diff changeset
101 table_lines.append(first_column)
sagun98
parents:
diff changeset
102
sagun98
parents:
diff changeset
103
sagun98
parents:
diff changeset
104
sagun98
parents:
diff changeset
105 FileTimeStamp = strftime("%Y%m%d%H%M%S", gmtime())
sagun98
parents:
diff changeset
106 LastMetaInt = 0
sagun98
parents:
diff changeset
107 if results.lastmeta and not results.lastmeta == "None":
sagun98
parents:
diff changeset
108 LastMetaInt = int(results.lastmeta) - 1
sagun98
parents:
diff changeset
109
sagun98
parents:
diff changeset
110 StratifyValueInt = 0
sagun98
parents:
diff changeset
111 if results.stratify_value and not results.stratify_value == "None":
sagun98
parents:
diff changeset
112 StratifyValueInt = int(results.stratify_value) - 2
sagun98
parents:
diff changeset
113
sagun98
parents:
diff changeset
114 LabelValueInt = 0
sagun98
parents:
diff changeset
115 if results.label_value and not results.label_value == "None":
sagun98
parents:
diff changeset
116 LabelValueInt = int(results.label_value) - 1
sagun98
parents:
diff changeset
117
sagun98
parents:
diff changeset
118 stratify_string = ""
sagun98
parents:
diff changeset
119 q = '"'
sagun98
parents:
diff changeset
120 if not results.stratify_value == '1':
sagun98
parents:
diff changeset
121 stratify_string = " --stratify " + q + table_lines[StratifyValueInt] + q + " "
sagun98
parents:
diff changeset
122
sagun98
parents:
diff changeset
123 if results.MParameter == "features":
sagun98
parents:
diff changeset
124 TBTargets = list()
sagun98
parents:
diff changeset
125 TableTargets = results.targets.split(',')
sagun98
parents:
diff changeset
126 for t in TableTargets:
sagun98
parents:
diff changeset
127 tb_entry = int(t) + LastMetaInt
sagun98
parents:
diff changeset
128 TBTargets.append(int(tb_entry))
sagun98
parents:
diff changeset
129
sagun98
parents:
diff changeset
130
sagun98
parents:
diff changeset
131 OutTargetsFile = tempfile.NamedTemporaryFile('w', delete=False )
sagun98
parents:
diff changeset
132 TempTargetsFileName = OutTargetsFile.name
sagun98
parents:
diff changeset
133 indx = -1
sagun98
parents:
diff changeset
134 for c in table_lines:
sagun98
parents:
diff changeset
135 indx+=1
sagun98
parents:
diff changeset
136 if indx in TBTargets:
sagun98
parents:
diff changeset
137 OutputString = table_lines[indx] + "\n"
sagun98
parents:
diff changeset
138 OutTargetsFile.write(OutputString)
sagun98
parents:
diff changeset
139 OutTargetsFile.close()
sagun98
parents:
diff changeset
140 os_command = "python " + \
sagun98
parents:
diff changeset
141 root_dir + \
sagun98
parents:
diff changeset
142 "/MicroPITA.py "+\
sagun98
parents:
diff changeset
143 "--lastmeta " + table_lines[LastMetaInt]+ " " +\
sagun98
parents:
diff changeset
144 "--feature_method " + results.feature_method + " " + \
sagun98
parents:
diff changeset
145 "--target " + TempTargetsFileName + " " +\
sagun98
parents:
diff changeset
146 "-m " + results.MParameter + " " + \
sagun98
parents:
diff changeset
147 "-n " + results.NSamples + " " +\
sagun98
parents:
diff changeset
148 stratify_string + " " +\
sagun98
parents:
diff changeset
149 results.inputname + " " +\
sagun98
parents:
diff changeset
150 results.outputname
sagun98
parents:
diff changeset
151 #print os_command
sagun98
parents:
diff changeset
152 os.system(os_command)
sagun98
parents:
diff changeset
153 argsx = shlex.split(os_command) #Split the command
sagun98
parents:
diff changeset
154 try:
sagun98
parents:
diff changeset
155 subprocess.check_call(argsx , shell=False)
sagun98
parents:
diff changeset
156 except:
sagun98
parents:
diff changeset
157 print "The call to micropita failed============="
sagun98
parents:
diff changeset
158 sys.exit(0)
sagun98
parents:
diff changeset
159
sagun98
parents:
diff changeset
160
sagun98
parents:
diff changeset
161
sagun98
parents:
diff changeset
162 if results.MParameter == "representative"\
sagun98
parents:
diff changeset
163 or results.MParameter == "diverse"\
sagun98
parents:
diff changeset
164 or results.MParameter == "extreme":
sagun98
parents:
diff changeset
165 os_command = "python " + \
sagun98
parents:
diff changeset
166 root_dir + \
sagun98
parents:
diff changeset
167 "/MicroPITA.py "+\
sagun98
parents:
diff changeset
168 "--lastmeta " + table_lines[LastMetaInt]+ " " +\
sagun98
parents:
diff changeset
169 "-m " + results.MParameter + " " + \
sagun98
parents:
diff changeset
170 "-n " + results.NSamples + " " +\
sagun98
parents:
diff changeset
171 stratify_string + " " + \
sagun98
parents:
diff changeset
172 results.inputname + " " +\
sagun98
parents:
diff changeset
173 results.outputname
sagun98
parents:
diff changeset
174 argsx = shlex.split(os_command) #Split the command
sagun98
parents:
diff changeset
175 try:
sagun98
parents:
diff changeset
176 ###os.system(os_command)
sagun98
parents:
diff changeset
177 subprocess.check_call(argsx , shell=False)
sagun98
parents:
diff changeset
178 except:
sagun98
parents:
diff changeset
179 print "The call to micropita failed============="
sagun98
parents:
diff changeset
180 sys.exit(0)
sagun98
parents:
diff changeset
181
sagun98
parents:
diff changeset
182
sagun98
parents:
diff changeset
183
sagun98
parents:
diff changeset
184
sagun98
parents:
diff changeset
185 if results.MParameter == "distinct"\
sagun98
parents:
diff changeset
186 or results.MParameter == "discriminant":
sagun98
parents:
diff changeset
187 os_command = "python " + \
sagun98
parents:
diff changeset
188 root_dir + \
sagun98
parents:
diff changeset
189 "/MicroPITA.py "+\
sagun98
parents:
diff changeset
190 "--lastmeta " + table_lines[LastMetaInt]+ " " +\
sagun98
parents:
diff changeset
191 "--label " + table_lines[LastMetaInt]+ " " +\
sagun98
parents:
diff changeset
192 "-m " + results.MParameter + " " + \
sagun98
parents:
diff changeset
193 "-n " + results.NSamples + " " +\
sagun98
parents:
diff changeset
194 stratify_string + " " + \
sagun98
parents:
diff changeset
195 results.inputname + " " +\
sagun98
parents:
diff changeset
196 results.outputname
sagun98
parents:
diff changeset
197 #print os_command
sagun98
parents:
diff changeset
198 argsx = shlex.split(os_command) #Split the command
sagun98
parents:
diff changeset
199 try:
sagun98
parents:
diff changeset
200 subprocess.check_call(argsx , shell=False)
sagun98
parents:
diff changeset
201 except:
sagun98
parents:
diff changeset
202 print "The call to micropita failed============="
sagun98
parents:
diff changeset
203 sys.exit(0)