annotate galaxy_micropita/micropita_prepare.py @ 3:8fb4630ab314 draft default tip

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