annotate DownloadFiles.py @ 3:fe42761670f1 draft default tip

Uploaded
author geert-vandeweyer
date Tue, 25 Mar 2014 13:15:25 -0400
parents 837fafa672d2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
1 #!/usr/bin/env python
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
2 #by Geert Vandeweyer.
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
3
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
4 #from galaxy import eggs
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
5 import sys, os
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
6 import os.path
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
7 import datetime
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
8
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
9 def stop_err(msg):
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
10 sys.stderr.write(msg)
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
11 sys.exit()
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
12
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
13 def __main__():
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
14 htmloutfile = sys.argv[1]
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
15 useremail = sys.argv[2]
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
16 folder = sys.argv[3]
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
17 ftpdirconfig = sys.argv[4]
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
18 try:
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
19 fout = open(sys.argv[1],'w')
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
20 except:
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
21 stop_err("Output file cannot be opened for writing.")
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
22
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
23 if len(sys.argv) < 5:
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
24 stop_err("No datafiles specified!");
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
25
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
26 # write header to outfile
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
27 fout.write("<html><body><h2>Files copied to your FTP account.</h2>")
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
28 filecounter = 5
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
29 fout.write("<p><ol> ")
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
30 now = datetime.datetime.now()
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
31 date=str(now)[:10]
2
837fafa672d2 common uncompressed files are now gzipped upons transfer to the ftp directory to save space.
geert-vandeweyer
parents: 1
diff changeset
32 #fout.write("<li>%s</li>" %(len(sys.argv)))
1
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
33 # compose ftp storage location
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
34 ftpdir="%s/%s/" %(ftpdirconfig,useremail)
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
35 if len(folder) == 0:
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
36 subdir = date
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
37 storedir="%s/%s/%s/" %(ftpdirconfig,useremail,date)
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
38 else:
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
39 storedir="%s/%s/%s/" %(ftpdirconfig,useremail,folder)
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
40 subdir = folder
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
41 if not os.path.exists(ftpdir):
3
fe42761670f1 Uploaded
geert-vandeweyer
parents: 2
diff changeset
42 try:
fe42761670f1 Uploaded
geert-vandeweyer
parents: 2
diff changeset
43 os.makedirs(ftpdir)
fe42761670f1 Uploaded
geert-vandeweyer
parents: 2
diff changeset
44 except:
fe42761670f1 Uploaded
geert-vandeweyer
parents: 2
diff changeset
45 stop_err("User dir on FTP could not be created: %s" % (ftpdir))
1
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
46 if not os.path.exists(storedir):
3
fe42761670f1 Uploaded
geert-vandeweyer
parents: 2
diff changeset
47
fe42761670f1 Uploaded
geert-vandeweyer
parents: 2
diff changeset
48 try:
fe42761670f1 Uploaded
geert-vandeweyer
parents: 2
diff changeset
49 os.makedirs(storedir)
fe42761670f1 Uploaded
geert-vandeweyer
parents: 2
diff changeset
50 except:
fe42761670f1 Uploaded
geert-vandeweyer
parents: 2
diff changeset
51 stop_err("Target dir could not be created: %s" % (storedir))
1
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
52
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
53 while filecounter < len(sys.argv):
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
54 inp = sys.argv[filecounter]
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
55 name = sys.argv[(filecounter+1)]
2
837fafa672d2 common uncompressed files are now gzipped upons transfer to the ftp directory to save space.
geert-vandeweyer
parents: 1
diff changeset
56 name = name.replace(':','_')
837fafa672d2 common uncompressed files are now gzipped upons transfer to the ftp directory to save space.
geert-vandeweyer
parents: 1
diff changeset
57 name = name.replace(' ','_')
837fafa672d2 common uncompressed files are now gzipped upons transfer to the ftp directory to save space.
geert-vandeweyer
parents: 1
diff changeset
58 name = name.replace('@','_AT_')
1
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
59 ext = sys.argv[(filecounter+2)]
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
60 # copy files to tmp dir & filenames
3
fe42761670f1 Uploaded
geert-vandeweyer
parents: 2
diff changeset
61 try:
fe42761670f1 Uploaded
geert-vandeweyer
parents: 2
diff changeset
62 os.system("cp '%s' '%s/%s.%s'" %(inp, storedir,name,ext))
fe42761670f1 Uploaded
geert-vandeweyer
parents: 2
diff changeset
63 except:
fe42761670f1 Uploaded
geert-vandeweyer
parents: 2
diff changeset
64 stop_err("Copy failed for target file : %s/%s.%s. Command was\ncp '%s' '%s/%s.%s'" % (storedir,name,ext,inp,storedir,name,ext))
2
837fafa672d2 common uncompressed files are now gzipped upons transfer to the ftp directory to save space.
geert-vandeweyer
parents: 1
diff changeset
65 ## gzip if fastq/fastqsanger/vcf
837fafa672d2 common uncompressed files are now gzipped upons transfer to the ftp directory to save space.
geert-vandeweyer
parents: 1
diff changeset
66 if ( ext == "fastq" or ext == "fastqsanger" or ext == "vcf" or ext == "sam" or ext == "txt"):
3
fe42761670f1 Uploaded
geert-vandeweyer
parents: 2
diff changeset
67 try:
fe42761670f1 Uploaded
geert-vandeweyer
parents: 2
diff changeset
68 os.system("gzip -9 '%s/%s.%s'" %(storedir,name,ext))
fe42761670f1 Uploaded
geert-vandeweyer
parents: 2
diff changeset
69 except:
fe42761670f1 Uploaded
geert-vandeweyer
parents: 2
diff changeset
70 stop_err("Compression of %s/%s.%s failed." % (storedir,name,ext))
1
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
71 fout.write("<li>%s.%s</li>" %(name,ext))
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
72 filecounter +=3
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
73
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
74 fout.write("</ol></p>")
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
75 fout.write("<p>Info: Files written to the '%s' subdirectory in your FTP account</p></body></html>" %(subdir))
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
76 # close file
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
77 fout.close
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
78
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
79
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
80 if __name__ == "__main__": __main__()
47070a3859df Uploaded
geert-vandeweyer
parents:
diff changeset
81