| 
0
 | 
     1 #!/usr/bin/env python
 | 
| 
 | 
     2 
 | 
| 
 | 
     3 import sys, re
 | 
| 
 | 
     4 import os
 | 
| 
 | 
     5 import tempfile
 | 
| 
 | 
     6 import shutil, subprocess, glob
 | 
| 
 | 
     7 import optparse
 | 
| 
 | 
     8 from os.path import basename
 | 
| 
 | 
     9 import zipfile, tarfile, gzip
 | 
| 
 | 
    10 from galaxy.datatypes.checkers import *
 | 
| 
 | 
    11 from stacks import *
 | 
| 
 | 
    12 
 | 
| 
 | 
    13 """
 | 
| 
 | 
    14 
 | 
| 
 | 
    15 Created by Yvan Le Bras
 | 
| 
 | 
    16 yvan.le_bras@irisa.fr
 | 
| 
 | 
    17 
 | 
| 
 | 
    18 Last modifications : 02/17/2014
 | 
| 
 | 
    19 
 | 
| 
 | 
    20 WARNING :
 | 
| 
 | 
    21 
 | 
| 
 | 
    22 STACKS_sort_read_pairs.py needs:
 | 
| 
 | 
    23 
 | 
| 
 | 
    24 - STACKS scripts in your $PATH
 | 
| 
 | 
    25 
 | 
| 
 | 
    26 These scripts are available after compiling the sources of STACKS :
 | 
| 
 | 
    27 
 | 
| 
 | 
    28 http://creskolab.uoregon.edu/stacks/
 | 
| 
 | 
    29 
 | 
| 
 | 
    30 or with the galaxy_stacks package in the Genouest toolshed
 | 
| 
 | 
    31 
 | 
| 
 | 
    32 
 | 
| 
 | 
    33 """
 | 
| 
 | 
    34 
 | 
| 
 | 
    35 def __main__():
 | 
| 
 | 
    36 	
 | 
| 
 | 
    37 
 | 
| 
 | 
    38 	# create the working dir
 | 
| 
 | 
    39 	os.mkdir("sort_read_outputs")
 | 
| 
 | 
    40 	os.mkdir("assembly_outputs")
 | 
| 
 | 
    41 	os.mkdir("samples_inputs")
 | 
| 
 | 
    42         os.mkdir("stacks_inputs")
 | 
| 
 | 
    43 
 | 
| 
 | 
    44 	# arguments recuperation
 | 
| 
 | 
    45 	parser = optparse.OptionParser()
 | 
| 
 | 
    46 	parser.add_option("-a")
 | 
| 
 | 
    47 	parser.add_option("-e")
 | 
| 
 | 
    48 	parser.add_option("-b")
 | 
| 
 | 
    49 	parser.add_option("-c")
 | 
| 
 | 
    50    	parser.add_option("-d")
 | 
| 
 | 
    51    	parser.add_option("-o")
 | 
| 
 | 
    52 	(options, args) = parser.parse_args()
 | 
| 
 | 
    53 
 | 
| 
 | 
    54 	# edit the command line
 | 
| 
 | 
    55 	cmd_line1 = ["sort_read_pairs.pl"]
 | 
| 
 | 
    56 
 | 
| 
 | 
    57 	#parse config files and create symlink if individual files are selected
 | 
| 
 | 
    58 
 | 
| 
 | 
    59 	# STACKS_archive
 | 
| 
 | 
    60 	# check if zipped files are into the tab
 | 
| 
 | 
    61 	extract_compress_files(options.a, os.getcwd()+"/stacks_inputs")
 | 
| 
 | 
    62 
 | 
| 
 | 
    63 	# samples_archive
 | 
| 
 | 
    64 	# check if zipped files are into the tab and change tab content
 | 
| 
 | 
    65 	extract_compress_files(options.e, os.getcwd()+"/samples_inputs")
 | 
| 
 | 
    66 		
 | 
| 
 | 
    67 	# create the sort_read_pairs command input line
 | 
| 
 | 
    68 	cmd_line1.extend(["-p", "stacks_inputs", "-s", "samples_inputs", "-o", "sort_read_outputs"])
 | 
| 
 | 
    69 
 | 
| 
 | 
    70 	if options.b:
 | 
| 
 | 
    71 		cmd_line1.extend(["-w", options.b])
 | 
| 
 | 
    72 	if options.c:
 | 
| 
 | 
    73 		cmd_line1.extend(["-r", options.c])
 | 
| 
 | 
    74 
 | 
| 
 | 
    75 	# exec command line 1 
 | 
| 
 | 
    76 	p1 = subprocess.Popen(cmd_line1)
 | 
| 
 | 
    77         p1.communicate()
 | 
| 
 | 
    78 
 | 
| 
 | 
    79 	# parse all files list and remove empty files from the output dir
 | 
| 
 | 
    80         for fasta_file in glob.glob("sort_read_outputs/*"):
 | 
| 
 | 
    81                 if os.stat(fasta_file).st_size==0:
 | 
| 
 | 
    82                         print "File "+fasta_file+" is empty"
 | 
| 
 | 
    83                         os.remove(fasta_file)
 | 
| 
 | 
    84         
 | 
| 
 | 
    85 
 | 
| 
 | 
    86 	# create the exec_velvet command input line
 | 
| 
 | 
    87 	cmd_line2 = ["exec_velvet.pl"]
 | 
| 
 | 
    88 	cmd_line2.extend(["-s", "sort_read_outputs", "-o", "assembly_outputs"])
 | 
| 
 | 
    89         cmd_line2.append("-c")
 | 
| 
 | 
    90 
 | 
| 
 | 
    91 	if options.d:
 | 
| 
 | 
    92 		cmd_line2.extend(["-M", options.d])
 | 
| 
 | 
    93 
 | 
| 
 | 
    94 	# version
 | 
| 
 | 
    95 	#cmd = 'sort_read_pairs.pl'+cmd_files+" "+cmd_options+" 2>&1"
 | 
| 
 | 
    96 	#cmd2 = 'exec_velvet.pl'+cmd_files2+" -c -e /softs/local/velvet/velvet_1.2.03/ "+cmd_options2+" 2>&1"
 | 
| 
 | 
    97 	
 | 
| 
 | 
    98 	# launch the command line 2 
 | 
| 
 | 
    99         p2 = subprocess.Popen(cmd_line2)
 | 
| 
 | 
   100         p2.communicate()
 | 
| 
 | 
   101 
 | 
| 
 | 
   102 	# get collated.fa file
 | 
| 
 | 
   103         try:
 | 
| 
 | 
   104 	    shutil.copy("assembly_outputs/collated.fa", options.o)
 | 
| 
 | 
   105 	except:
 | 
| 
 | 
   106             print "No result file"
 | 
| 
 | 
   107             sys.exit(1)
 | 
| 
 | 
   108 
 | 
| 
 | 
   109 if __name__ == "__main__": __main__()
 |