view mrf.py @ 2:0442b1c31ef2 draft default tip

Uploaded
author da-intersect
date Tue, 25 Jun 2013 20:45:01 -0400
parents
children
line wrap: on
line source

#!/usr/bin/env python2.6
import subprocess
import sys
import re
import shutil
import os
from optparse import OptionParser

toolname = "MRF"


def main():

	tool_path = os.path.dirname(os.path.realpath(sys.argv[0])) + "/" + toolname

	usage = "usage: %prog [options] arg"
	parser = OptionParser(usage)
	parser.add_option("-f", "--file",
        	action="store", type="string", dest="configfile",
		help="File containing the Values")
	
	parser.add_option("-o", "--output",
		action="store", type="string", dest="outputfile", nargs=4, 
		help="Output file where to store the result")

	(options, args) = parser.parse_args()

	if options.configfile is None or options.outputfile is None:
		print "Missing arguments"
		parser.print_help()
		exit (1)

	mrf(tool_path, options.configfile, options.outputfile)
	
def mrf(tool_path, configfile, outputfile):

	
	f = open(configfile, 'r+b')

	tool = subprocess.Popen([tool_path],stdin=f, stdout=subprocess.PIPE)
	tool.wait()
	f.close()

	if tool.returncode != 0:
		sys.stderr.write("There was a problem when running the $toolname tool")
		output = tool.stdout.read()
		print output
		exit (1)

	output = tool.stdout.read()
	print output

	# Determining the Output file generated by MRF
	m1 = re.search("(.*Cluster: )*(\/.*\.pos$)", output, re.MULTILINE )
	m1_1 = re.search("(\/.*\/dataset_[0-9]*.dat-dataset_[0-9]*\.dat-\w*)(\..*)", m1.group(2), re.MULTILINE)
	prefix = m1_1.group(1)

	m2 = re.search("(.*Cluster: )*(\/.*\.pos$)", output, re.MULTILINE )
	file1 = m2.group(2)

	file2 = prefix + '.mrf.txt'

	m3 = re.search("(.*Morphology: )*(\/.*\.txt$)",output, re.MULTILINE )
	file3 = m3.group(2)

	file4 = prefix + '.wc_histogram.txt'

	#Move the generated files to the Galaxy outputs and remove the tmp file	

	shutil.move(file1, outputfile[0])

	lines = open(file2).readlines()
	open(outputfile[1], 'w').writelines(lines[1:])
	os.remove(file2)

	shutil.move(file3, outputfile[2])
	shutil.move(file4, outputfile[3])

if __name__ == "__main__":
    main()