annotate STACKS_sort_read_pairs.py @ 1:ccfa8e539bdf

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