view DownloadFiles.py @ 2:837fafa672d2 draft

common uncompressed files are now gzipped upons transfer to the ftp directory to save space.
author geert-vandeweyer
date Tue, 07 May 2013 09:02:02 -0400
parents 47070a3859df
children fe42761670f1
line wrap: on
line source

#!/usr/bin/env python
#by Geert Vandeweyer.

#from galaxy import eggs
import sys, os
import os.path
import datetime

def stop_err(msg):
    sys.stderr.write(msg)
    sys.exit()
    
def __main__():
    htmloutfile = sys.argv[1]
    useremail = sys.argv[2]
    folder = sys.argv[3] 
    ftpdirconfig = sys.argv[4]
    try:
        fout = open(sys.argv[1],'w')
    except:
        stop_err("Output file cannot be opened for writing.")
    
    if len(sys.argv) < 5:
   	stop_err("No datafiles specified!"); 

    # write header to outfile	
    fout.write("<html><body><h2>Files copied to your FTP account.</h2>")
    filecounter = 5 
    fout.write("<p><ol> ")
    now = datetime.datetime.now()
    date=str(now)[:10]
    #fout.write("<li>%s</li>" %(len(sys.argv)))
    # compose ftp storage location
    ftpdir="%s/%s/" %(ftpdirconfig,useremail)
    if len(folder) == 0:
	    subdir = date
	    storedir="%s/%s/%s/" %(ftpdirconfig,useremail,date)
    else:
	    storedir="%s/%s/%s/" %(ftpdirconfig,useremail,folder)
	    subdir = folder
    if not os.path.exists(ftpdir):
    	os.makedirs(ftpdir)
    if not os.path.exists(storedir):
    	os.makedirs(storedir)
	
    while filecounter < len(sys.argv):
	inp = sys.argv[filecounter]
	name = sys.argv[(filecounter+1)]
	name = name.replace(':','_')
	name = name.replace(' ','_')
	name = name.replace('@','_AT_')
	ext = sys.argv[(filecounter+2)]
	# copy files to tmp dir & filenames
	os.system("cp '%s' '%s/%s.%s'" %(inp, storedir,name,ext))
	## gzip if fastq/fastqsanger/vcf
	if ( ext == "fastq" or ext == "fastqsanger" or ext == "vcf" or ext == "sam" or ext == "txt"): 
		os.system("gzip -9 '%s/%s.%s'" %(storedir,name,ext))
	fout.write("<li>%s.%s</li>" %(name,ext))
	filecounter +=3
	
    fout.write("</ol></p>")
    fout.write("<p>Info: Files written to the '%s' subdirectory in your FTP account</p></body></html>" %(subdir))
    # close file
    fout.close 

        
if __name__ == "__main__": __main__()