comparison barcode_splitter-bc23f6946bb8/fastx_barcode_splitter_single_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
39 output = sys.argv[1]
40 output_unmatched = sys.argv[2]
41 file_ext = sys.argv[3]
42 select_barcode = sys.argv[4].replace('--barcodes=','')
43 barcodes = os.path.abspath("barcodes")
44 with open(barcodes, 'w') as f:
45 f.write("barcode\t%s\n" % (select_barcode))
46
47 #barcodes = sys.argv[6]
48 fastx = sys.argv[5]
49 fastx_name = sys.argv[6]
50 #extra_files_path = sys.argv[9]
51 script_args = ' '.join(sys.argv[7:])
52 #Sanitize library name, make sure we can create a file with this name
53 lib_name = re.sub('\W','_',re.sub('\.\W*$','',fastx_name))+'_'
54 # Check that input datasets exist
55 if not os.path.isfile(fastx):
56 stop_err('Error: Input file (%s) not found!' % fastx)
57 try:
58 prefix = lib_name
59 cmd_line = 'zcat -f %s | fastx_barcode_splitter.pl --bcfile %s --prefix %s --suffix %s %s' %(fastx,barcodes,prefix,'.'+file_ext,script_args)
60 # print >> sys.stderr, cmd_line
61 # Create file to collect results written to stdout
62 tmp_dir = tempfile.mkdtemp()
63 result_path = tempfile.NamedTemporaryFile(dir=tmp_dir, prefix='results_', suffix='.out' ).name
64 result_file = open( result_path, 'wb' )
65 proc = subprocess.Popen( args=cmd_line, shell=True, cwd=tmp_dir, stderr=subprocess.PIPE,stdout=result_file.fileno() )
66 returncode = proc.wait()
67 result_file.close()
68 stderr = proc.stderr.read()
69 if returncode != 0:
70 raise Exception, stderr
71 # copy results to ouptut
72 #results_to_html(result_path,output,extra_files_path,True)
73 # make new datasets for selected barcodes
74 flist = os.listdir(tmp_dir)
75 for fname in flist:
76 if fname.find('_'+barcode+'.'+file_ext) >= 0:
77 fpath = os.path.join(tmp_dir,fname)
78 shutil.copy2(fpath, output)
79 for fname in flist:
80 if fname.find('_unmatched.' + file_ext) > 0:
81 fpath = os.path.join(tmp_dir, fname)
82 shutil.copy2(fpath, output_unmatched)
83 except Exception, e:
84 raise Exception, 'Exception caught attempting conversion: ' + str( e )
85
86 if __name__ == "__main__": __main__()