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