view save_z_15/save_z7_15nodes.py @ 0:ae6f616e7637 draft

Uploaded
author nanettec
date Fri, 18 Mar 2016 05:49:55 -0400
parents
children
line wrap: on
line source

"""
@summary: Save partial z file
@version 7

"""
# MAPPER --> save z file

# Input: qtlcart.inp
#        qtlcart.map
#      	 parameters.txt

# Output: z_partial.txt

import optparse, sys
import tempfile
import re

def stop_err( msg ):
	sys.stderr.write( "%s\n" % msg )
	sys.exit()
    
def __main__():
	parser = optparse.OptionParser()
	parser.add_option("-i", "--input1", default=None, dest="input1", 
			  help="qtlcart.inp file")
	parser.add_option("-j", "--input2", default=None, dest="input2", 
			  help="qtlcart.map file")
	parser.add_option("-k", "--input3", default=None, dest="input3", 
			  help="parameters.txt file")

	parser.add_option("-p", "--output1", default=None, dest="output1", 
			  help="qtlcart_partial.z file")
	(options, args) = parser.parse_args()
	
	try:
	    open(options.input1, "r").close()
	except TypeError, e:
	    stop_err("You need to supply the qtlcart.inp file:\n" + str(e))
	except IOError, e:
	    stop_err("Can not open the qtlcart.inp file:\n" + str(e))
    
	try:
	    open(options.input2, "r").close()
	except TypeError, e:
	    stop_err("You need to supply the qtlcart.map file:\n" + str(e))
	except IOError, e:
	    stop_err("Can not open the qtlcart.map file:\n" + str(e))

	try:
	    open(options.input3, "r").close()
	except TypeError, e:
	    stop_err("You need to supply the parameters.txt file:\n" + str(e))
	except IOError, e:
	    stop_err("Can not open the parameters.txt file:\n" + str(e))
	
	######################################################################
	# submit.py
	######################################################################
	
	import subprocess
	import os
	
	# Create temp direcotry
	tempdir = tempfile.mkdtemp()
	#print tempdir
	
	s = "cp %s %s/parameters.txt" %(options.input3, tempdir)
	subprocess.call(s, shell=True)
	paramters_file = open(tempdir+"/parameters.txt", "r")
	parvalues = []
	for line in paramters_file:
		l = line.strip().split("\t")
		if l[0] == "SRmodel":
			SRmodel = l[1]
		if l[0] == "Zmodel":
			Zmodel = l[1]
		if l[0] == "threshold":
			threshold = l[1]
		if l[0] == "walking_speed":
			walking_speed = l[1]
		if l[0] == "window_size":
			window_size = l[1]
		if l[0] == "minimum_cM_between_QTL":
			minimum_cM_between_QTL = l[1]
	paramters_file.close()		

	# copy INPUT file to the temp directory
	s = "cp %s %s/qtlcart.inp" %(options.input1, tempdir)
	subprocess.call(s, shell=True)
	s = "cp %s %s/qtlcart.map" %(options.input2, tempdir)
	subprocess.call(s, shell=True)
	
        f=open(tempdir+"/qtlcart.inp", "r")
	ln=f.readlines()
        f.close()
        count = 0
	
        if ln[0] == "NA":
		f1=open(tempdir+"/QTLs_total_parsed_LOD.txt", "w")
		f1.write("")
		f1.close()
		os.system("mv %s/QTLs_total_parsed_LOD.txt %s" %(tempdir,options.output1))
		#os.system("mv %s/qtlcart.rc %s" %(tempdir,options.output2))
        else:
		count += 1
		if count == 1:
			instruction = "/cluster1/bin/Rcross -i %s/qtlcart.inp -o %s/qtlcart.cro -A -V" %(tempdir, tempdir)
			os.system(instruction)
			
			instruction = "/cluster1/bin/SRmapqtl -i %s/qtlcart.cro -e %s/SRqtlcart.log -o %s/qtlcart.sr -m %s/qtlcart.map -M %s -t 9999999999 -A -V" %(tempdir, tempdir, tempdir, tempdir, str(SRmodel))
			os.system(instruction)        
			
			instruction = "/cluster1/bin/Zmapqtl -i %s/qtlcart.cro -o %s/qtlcart.z -m %s/qtlcart.map -S %s/qtlcart.sr -M %s -d %s -w %s -t 9999999999 -A -V" %(tempdir, tempdir, tempdir, tempdir, str(Zmodel), str(walking_speed), str(window_size))
			os.system(instruction)
		
		#####################
		# Save partial z file
		#####################
		
		zfile=open(tempdir+"/qtlcart_partial.z", "w")
		f=open(tempdir+"/qtlcart.z", "r")
		done = "no"
		for l in f:
		    if (done != "yes") and (not l.startswith("-e")):
			    zfile.write(l.strip()+"\n")
		    else:
			    done = "yes"
		f.close()
		zfile.write("-e\n")
		zfile.close()
		
		os.system("mv %s/qtlcart_partial.z %s" %(tempdir,options.output1))
		
if __name__=="__main__": 
    __main__()