comparison barcode_splitter-bc23f6946bb8/fastx_barcode_splitter_galaxy_wrapper.py @ 0:2b6d577dd1ab default tip

Uploaded
author bccarstens
date Mon, 16 Jan 2012 22:38:10 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:2b6d577dd1ab
1 import sys, os, os.path, tempfile, shutil, re, shlex, subprocess
2
3 def stop_err( msg ):
4 sys.stderr.write( "%s\n" % msg )
5 sys.exit()
6
7 # tranform fastx_barcode_splitter result to html
8 def results_to_html(results_path,html_path,basepath,print_stdout ):
9 pat = '%s[/]?([^\t]*)' % basepath
10 rep = '<a href=\"\\1\">\\1</a>'
11 txt = open(results_path,'r')
12 html = open(html_path,'w')
13 html.write('<html><body><table border=1>\n')
14 try:
15 for line in txt:
16 html.write('<tr><td>%s</td></tr>' % re.sub('\t','</td><td>',re.sub(pat,rep,line)))
17 if print_stdout:
18 print >> sys.stdout, '\t'.join(line.split('\t')[:2])
19 except Exception, e:
20 print(str(e))
21 pass
22 html.write('</table></body></html>\n')
23 html.close()
24 txt.close()
25
26 def __main__():
27 """
28 ##params for galaxy wrapper
29 $output
30 "$output.id"
31 "$input.ext"
32 "$__new_file_path__"
33 --barcodes='$barcodes'
34 $BARCODE $input "$input.name" "$output.extra_files_path"
35 ## params for fastx_barcode_splitter
36 --mismatches $mismatches --partial $partial $EOL
37 """
38 output = sys.argv[1]
39 output_id = sys.argv[2]
40 file_ext = sys.argv[3]
41 new_file_path = sys.argv[4]
42 select_barcodes = sys.argv[5].replace('--barcodes=','')
43 barcodes = sys.argv[6]
44 fastx = sys.argv[7]
45 fastx_name = sys.argv[8]
46 extra_files_path = sys.argv[9]
47 script_args = ' '.join(sys.argv[10:])
48 #Sanitize library name, make sure we can create a file with this name
49 lib_name = re.sub('\W','_',re.sub('\.\W*$','',fastx_name))+'_'
50 prefix = os.path.join(extra_files_path,lib_name)
51 # Check that input datasets exist
52 if not os.path.isfile(fastx):
53 stop_err('Error: Input file (%s) not found!' % fastx)
54 if not os.path.isfile(barcodes):
55 stop_err('Error: barcode file (%s) not found!' % barcodes)
56 try:
57 # Check that extra_files_path exists
58 if not os.path.isdir(extra_files_path):
59 os.makedirs(extra_files_path)
60 cmd_line = 'zcat -f %s | fastx_barcode_splitter.pl --bcfile %s --prefix %s --suffix %s %s' %(fastx,barcodes,prefix,'.'+file_ext,script_args)
61 # print >> sys.stderr, cmd_line
62 # Create file to collect results written to stdout
63 tmp_dir = tempfile.mkdtemp()
64 result_path = tempfile.NamedTemporaryFile(dir=tmp_dir, prefix='results_', suffix='.out' ).name
65 result_file = open( result_path, 'wb' )
66 proc = subprocess.Popen( args=cmd_line, shell=True, cwd=tmp_dir, stderr=subprocess.PIPE,stdout=result_file.fileno() )
67 returncode = proc.wait()
68 result_file.close()
69 stderr = proc.stderr.read()
70 if returncode != 0:
71 raise Exception, stderr
72 # copy results to ouptut
73 results_to_html(result_path,output,extra_files_path,True)
74 # make new datasets for selected barcodes
75 if select_barcodes != None and len(select_barcodes) > 0:
76 flist = os.listdir(extra_files_path)
77 for barcode in select_barcodes.split(','):
78 for fname in flist:
79 if fname.find('_'+barcode+'.'+file_ext) >= 0:
80 fpath = os.path.join(extra_files_path,fname)
81 # filename pattern required by galaxy
82 fn = "%s_%s_%s_%s_%s" % ( 'primary', output_id, barcode, 'visible', file_ext )
83 npath = os.path.join(new_file_path,fn)
84 try:
85 os.link(fpath, npath)
86 except:
87 shutil.copy2(fpath, npath)
88 except Exception, e:
89 raise Exception, 'Exception caught attempting conversion: ' + str( e )
90
91 if __name__ == "__main__": __main__()