annotate BSseeker2/bs_seeker2-align.py @ 0:e6df770c0e58 draft

Initial upload
author weilong-guo
date Fri, 12 Jul 2013 18:47:28 -0400
parents
children 8b26adf64adc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
1 #!/usr/bin/python
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
2
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
3 from optparse import OptionParser, OptionGroup
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
4 import re
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
5 import tempfile
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
6 from bs_align import output
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
7 from bs_align.bs_pair_end import *
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
8 from bs_align.bs_single_end import *
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
9 from bs_align.bs_rrbs import *
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
10 from bs_utils.utils import *
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
11
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
12
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
13 if __name__ == '__main__':
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
14
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
15 parser = OptionParser()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
16 # option group 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
17 opt_group = OptionGroup(parser, "For single end reads")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
18 opt_group.add_option("-i", "--input", type="string", dest="infilename",help="Input your read file name (FORMAT: sequences, fastq, qseq,fasta)", metavar="INFILE")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
19 parser.add_option_group(opt_group)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
20
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
21 # option group 2
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
22 opt_group = OptionGroup(parser, "For pair end reads")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
23 opt_group.add_option("-1", "--input_1", type="string", dest="infilename_1",help="Input your read file end 1 (FORMAT: sequences, qseq, fasta, fastq)", metavar="FILE")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
24 opt_group.add_option("-2", "--input_2", type="string", dest="infilename_2",help="Input your read file end 2 (FORMAT: sequences, qseq, fasta, fastq)", metavar="FILE")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
25 opt_group.add_option("--minins",type = "int",dest = "min_insert_size", help="The minimum insert size for valid paired-end alignments [Default: %default]", default = -1)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
26 opt_group.add_option("--maxins",type = "int",dest = "max_insert_size", help="The maximum insert size for valid paired-end alignments [Default: %default]", default = 400)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
27 parser.add_option_group(opt_group)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
28
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
29 # option group 3
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
30 opt_group = OptionGroup(parser, "Reduced Representation Bisulfite Sequencing Options")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
31 opt_group.add_option("-r", "--rrbs", action="store_true", dest="rrbs", default = False, help = 'Process reads from Reduced Representation Bisulfite Sequencing experiments')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
32 opt_group.add_option("-c", "--cut-site", type="string",dest="cut_format", help="Cutting sites of restriction enzyme. Ex: MspI(C-CGG), Mael:(C-TAG), double-enzyme MspI&Mael:(C-CGG,C-TAG). [Default: %default]", metavar="pattern", default = "C-CGG")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
33 opt_group.add_option("-L", "--low", type = "int", dest="rrbs_low_bound", help="lower bound of fragment length (excluding C-CGG ends) [Default: %default]", default = 40)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
34 opt_group.add_option("-U", "--up", type = "int", dest="rrbs_up_bound", help="upper bound of fragment length (excluding C-CGG ends) [Default: %default]", default = 500)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
35 parser.add_option_group(opt_group)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
36
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
37 # option group 4
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
38 opt_group = OptionGroup(parser, "General options")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
39 opt_group.add_option("-t", "--tag", type="string", dest="taginfo",help="[Y]es for undirectional lib, [N]o for directional [Default: %default]", metavar="TAG", default = 'N')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
40 opt_group.add_option("-s","--start_base",type = "int",dest = "cutnumber1", help="The first base of your read to be mapped [Default: %default]", default = 1)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
41 opt_group.add_option("-e","--end_base",type = "int",dest = "cutnumber2", help="The last cycle number of your read to be mapped [Default: %default]", default = 200)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
42 opt_group.add_option("-a", "--adapter", type="string", dest="adapter_file",help="Input text file of your adaptor sequences (to be trimed from the 3'end of the reads). Input 1 seq for dir. lib., 2 seqs for undir. lib. One line per sequence", metavar="FILE", default = '')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
43 opt_group.add_option("--am",type = "int",dest = "adapter_mismatch", help="Number of mismatches allowed in adaptor [Default: %default]", default = 0)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
44 opt_group.add_option("-g", "--genome", type="string", dest="genome",help="Name of the reference genome (the same as the reference genome file in the preprocessing step) [ex. chr21_hg18.fa]")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
45 opt_group.add_option("-m", "--mismatches",type = "int", dest="int_no_mismatches",help="Number of mismatches in one read [Default: %default]", default = 4)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
46 opt_group.add_option("--aligner", dest="aligner",help="Aligner program to perform the analisys: " + ', '.join(supported_aligners) + " [Default: %default]", metavar="ALIGNER", default = BOWTIE2)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
47 opt_group.add_option("-p", "--path", dest="aligner_path", help="Path to the aligner program. Defaults: " +' '*70+ '\t'.join(('%s: %s '+' '*70) % (al, aligner_path[al]) for al in sorted(supported_aligners)),
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
48 metavar="PATH"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
49 )
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
50 opt_group.add_option("-d", "--db", type="string", dest="dbpath",help="Path to the reference genome library (generated in preprocessing genome) [Default: %default]" , metavar="DBPATH", default = reference_genome_path)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
51 opt_group.add_option("-l", "--split_line",type = "int", dest="no_split",help="Number of lines per split (the read file will be split into small files for mapping. The result will be merged. [Default: %default]", default = 4000000)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
52 opt_group.add_option("-o", "--output", type="string", dest="outfilename",help="The name of output file [INFILE.bs(se|pe|rrbs)]", metavar="OUTFILE")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
53 opt_group.add_option("-f", "--output-format", type="string", dest="output_format",help="Output format: "+', '.join(output.formats)+" [Default: %default]", metavar="FORMAT", default = output.BAM)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
54 opt_group.add_option("--no-header", action="store_true", dest="no_SAM_header",help="Suppress SAM header lines [Default: %default]", default = False)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
55 opt_group.add_option("--temp_dir", type="string", dest="temp_dir",help="The path to your temporary directory [Default: %default]", metavar="PATH", default = tempfile.gettempdir())
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
56 opt_group.add_option("--XS",type = "string", dest="XS_filter",help="Filter definition for tag XS, format X,Y. X=0.8 and y=5 indicate that for one read, if #(mCH sites)/#(all CH sites)>0.8 and #(mCH sites)>5, then tag XS=1; or else tag XS=0. [Default: %default]", default = "0.5,5") # added by weilong
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
57 opt_group.add_option("--multiple-hit", action="store_true", dest="Output_multiple_hit", default = False, help = 'Output reads with multiple hits to file\"Multiple_hit.fa\"')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
58
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
59 opt_group.add_option("-v", "--version", action="store_true", dest="version",help="show version of BS-Seeker2", metavar="version", default = False)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
60
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
61 parser.add_option_group(opt_group)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
62
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
63 # option group 5
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
64 opt_group = OptionGroup(parser, "Aligner Options",
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
65 "You may specify any additional options for the aligner. You just have to prefix them with " +
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
66 ', '.join('%s for %s' % (aligner_options_prefixes[aligner], aligner) for aligner in supported_aligners)+
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
67 ', and BS Seeker will pass them on. For example: --bt-p 4 will increase the number of threads for bowtie to 4, '
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
68 '--bt--tryhard will instruct bowtie to try as hard as possible to find valid alignments when they exist, and so on. '
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
69 'Be sure that you know what you are doing when using these options! Also, we don\'t do any validation on the values.')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
70 parser.add_option_group(opt_group)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
71
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
72
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
73 #----------------------------------------------------------------
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
74 # separate aligner options from BS Seeker options
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
75 aligner_options = {}
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
76 bs_seeker_options = []
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
77 i = 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
78 while i < len(sys.argv):
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
79 arg = sys.argv[i]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
80 m = re.match(r'^%s' % '|'.join('(%s)'% aligner_options_prefixes[al] for al in supported_aligners), arg)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
81 if m:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
82 a_opt = arg.replace(m.group(0),'-',1)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
83 aligner_options[a_opt] = []
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
84 while i + 1 < len(sys.argv) and sys.argv[i+1][0] != '-':
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
85 aligner_options[a_opt].append(sys.argv[i+1])
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
86 i += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
87 if len(aligner_options[a_opt]) == 0: # if it is a key-only option
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
88 aligner_options[a_opt] = True
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
89 else:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
90 bs_seeker_options.append(arg)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
91 i += 1
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
92
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
93
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
94 (options, args) = parser.parse_args(args = bs_seeker_options)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
95
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
96
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
97 # if no options were given by the user, print help and exit
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
98 if len(sys.argv) == 1:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
99 print parser.print_help()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
100 exit(0)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
101
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
102 if options.version :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
103 show_version()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
104 exit (-1)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
105 else :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
106 show_version()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
107
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
108 # check parameters
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
109 # input read files
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
110 if options.infilename and (options.infilename_1 or options.infilename_2):
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
111 error('-i and [-1|-2] options are exclusive. You should use only one of them.')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
112
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
113 if not (options.infilename or (options.infilename_1 and options.infilename_2)):
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
114 error('You should set either -i or -1 and -2 options.')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
115 # -t, directional / un-directional library
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
116 asktag=str(options.taginfo).upper()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
117 if asktag not in 'YN':
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
118 error('-t option should be either Y or N, not %s' % asktag)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
119 # -a
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
120 if options.aligner not in supported_aligners:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
121 error('-a option should be: %s' % ' ,'.join(supported_aligners)+'.')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
122 # path for aligner
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
123 aligner_exec = os.path.expanduser( os.path.join(options.aligner_path or aligner_path[options.aligner], options.aligner) )
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
124 # mismatch allowed: bowtie 1,build-in parameter '-m'; bowtie 2, post-filter paramter
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
125 # mismatch should no greater than the read length
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
126 int_no_mismatches=min(options.int_no_mismatches, options.cutnumber2-options.cutnumber1)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
127 str_no_mismatches=str(int_no_mismatches)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
128 # -g
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
129 if options.genome is None:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
130 error('-g is a required option')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
131 genome = os.path.split(options.genome)[1]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
132 genome_subdir = genome
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
133
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
134 # try to guess the location of the reference genome for RRBS
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
135 if options.rrbs:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
136 if options.rrbs_low_bound and options.rrbs_up_bound:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
137 if options.cut_format == "C-CGG" :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
138 genome_subdir += '_rrbs_%d_%d' % (options.rrbs_low_bound, options.rrbs_up_bound)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
139 else :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
140 genome_subdir += '_rrbs_%s_%d_%d' % ( re.sub(",","-",re.sub("-", "", options.cut_format)), options.rrbs_low_bound, options.rrbs_up_bound)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
141 else:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
142 possible_refs = filter(lambda dir: dir.startswith(genome+'_rrbs_'), os.listdir(options.dbpath))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
143 if len(possible_refs) == 1:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
144 genome_subdir = possible_refs[0]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
145 else:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
146 error('Cannot localize unambiguously the reference genome for RRBS. '
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
147 'Please, specify the options \"--low\" and \"--up\" that you used at the index-building step.\n'
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
148 'Possible choices are:\n' + '\n'.join([pr.split('_rrbs_')[-1].replace('_',', ') for pr in possible_refs]))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
149
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
150 db_path = os.path.expanduser(os.path.join(options.dbpath, genome_subdir + '_' + options.aligner))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
151
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
152 if not os.path.isdir(db_path):
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
153 error('Index DIR \"' + genome_subdir + '..\" cannot be found in ' + options.dbpath +'.\n\tPlease run the bs_seeker2-build.py '
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
154 'to create it with the correct parameters for -g, -r, --low, --up and --aligner.')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
155
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
156 # handle aligner options
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
157 #
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
158
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
159 # default aligner options
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
160 aligner_options_defaults = {
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
161 BOWTIE : { '-e' : 40*int_no_mismatches,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
162 '--nomaqround' : True,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
163 '--norc' : True,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
164 '-k' : 2,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
165 # -k=2; report two best hits, and filter by error rates
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
166 '--quiet' : True,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
167 '--best' : True,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
168 # '--suppress' : '2,5,6',
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
169 '--sam' : True,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
170 '--sam-nohead' : True,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
171 '-p' : 2
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
172 },
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
173 BOWTIE2 : {
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
174 #'-M' : 5,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
175 '--norc' : True,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
176 '--quiet' : True,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
177 '-p' : 2,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
178 '--sam-nohead' : True,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
179 # run bowtie2 in local mode by default
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
180 '--local' : '--end-to-end' not in aligner_options,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
181 #'--mm' : True,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
182 '-k' : 2
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
183 },
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
184 SOAP : { '-v' : int_no_mismatches,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
185 '-p' : 2,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
186 '-r' : 2,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
187 '-M' : 4
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
188 },
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
189 RMAP : { '-M' : 2
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
190 # to do # control for only mapping on + strand
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
191 }
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
192
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
193 }
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
194
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
195 if '--end-to-end' not in aligner_options:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
196 aligner_options_defaults[BOWTIE2].update({'-D' : 50})
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
197 #aligner_options_defaults[BOWTIE2].update({'-D' : 50, '-R': 3, '-N': 0, '-L': 15, '-i' : 'S,1,0.50'})
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
198 else:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
199 aligner_options_defaults[BOWTIE2].update({'-D' : 50, '-L': 15, '--score-min': 'L,-0.6,-0.6' })
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
200
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
201 aligner_options = dict(aligner_options_defaults[options.aligner], **aligner_options)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
202
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
203 aligner_options_string = lambda : ' %s ' % (' '.join(opt_key +
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
204 (' ' + ' '.join(map(str,opt_val)) # join all values if the value is an array
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
205 if type(opt_val) is list else
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
206 ('' if type(opt_val) is bool and opt_val # output an empty string if it is a key-only option
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
207 else ' ' +str(opt_val)) # output the value if it is a single value
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
208 )
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
209 for opt_key, opt_val in aligner_options.iteritems() if opt_val not in [None, False]))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
210
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
211
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
212 # tmp_path = (options.outfilename or options.infilename or options.infilename_1) +'-'+ options.aligner+ '-TMP'
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
213 # clear_dir(tmp_path)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
214
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
215 if options.output_format not in output.formats:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
216 error('Output format should be one of: ' + ', '.join(output.formats))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
217
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
218 if options.outfilename:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
219 outfilename = options.outfilename
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
220 logfilename = outfilename
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
221 elif options.infilename is not None:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
222 logfilename = options.infilename+'_'+ ('rr' if options.rrbs else '') + 'bsse'
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
223 outfilename = logfilename + '.' + options.output_format
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
224 else:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
225 logfilename = options.infilename_1+'_'+ ('rr' if options.rrbs else '') + 'bspe'
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
226 outfilename = logfilename + '.' + options.output_format
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
227
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
228 outfilename = os.path.expanduser(outfilename)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
229 logfilename = os.path.expanduser(logfilename)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
230 outfile = output.outfile(outfilename, options.output_format, deserialize(os.path.join(db_path, 'refname')), ' '.join(sys.argv), options.no_SAM_header)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
231
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
232 open_log(logfilename+'.bs_seeker2_log')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
233
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
234 aligner_title = options.aligner
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
235 if options.aligner == BOWTIE2 :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
236 if '--end-to-end' in aligner_options :
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
237 aligner_title = aligner_title + "-e2e"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
238 else:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
239 aligner_title = aligner_title + "-local"
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
240
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
241 tmp_path = tempfile.mkdtemp(prefix='bs_seeker2_%s_-%s-TMP-' % (os.path.split(outfilename)[1], aligner_title ), dir = options.temp_dir)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
242
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
243
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
244 (XS_x, XS_y) = options.XS_filter.split(",")
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
245 XS_pct = float(XS_x)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
246 XS_count = int(XS_y)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
247 logm('Filter for tag XS: #(mCH)/#(all CH)>%f and #(mCH)>%d' % (XS_pct, XS_count))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
248
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
249
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
250 logm('Temporary directory: %s' % tmp_path)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
251 logm('Reduced Representation Bisulfite Sequencing: %s' % str(options.rrbs))
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
252 if options.infilename is not None:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
253 logm('Single end')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
254
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
255 aligner_command = aligner_exec + aligner_options_string() + \
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
256 { BOWTIE : ' %(reference_genome)s -f %(input_file)s %(output_file)s',
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
257 BOWTIE2 : ' -x %(reference_genome)s -f -U %(input_file)s -S %(output_file)s',
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
258 SOAP : ' -D %(reference_genome)s.fa.index -o %(output_file)s -a %(input_file)s',
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
259 RMAP : ' -c %(reference_genome)s.fa -o %(output_file)s %(input_file)s'
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
260 }[options.aligner]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
261 logm ('Aligner command: %s' % aligner_command)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
262 # single end reads
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
263 if options.rrbs: # RRBS scan
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
264 bs_rrbs(options.infilename,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
265 asktag,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
266 # options.rrbs_taginfo,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
267 options.adapter_file,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
268 options.cutnumber1,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
269 options.cutnumber2,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
270 options.no_split,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
271 str_no_mismatches,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
272 aligner_command,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
273 db_path,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
274 tmp_path,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
275 outfile,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
276 XS_pct,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
277 XS_count,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
278 options.adapter_mismatch,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
279 options.cut_format,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
280 options.Output_multiple_hit
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
281 )
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
282 else: # Normal single end scan
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
283 bs_single_end( options.infilename,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
284 asktag,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
285 options.adapter_file,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
286 options.cutnumber1,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
287 options.cutnumber2,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
288 options.no_split,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
289 str_no_mismatches,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
290 aligner_command,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
291 db_path,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
292 tmp_path,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
293 outfile,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
294 XS_pct,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
295 XS_count,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
296 options.adapter_mismatch,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
297 options.Output_multiple_hit
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
298 )
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
299 else:
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
300 logm('Pair end')
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
301 # pair end specific default options
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
302 aligner_options = dict({BOWTIE: {'--ff' : asktag == 'N',
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
303 '--fr' : asktag == 'Y',
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
304 '-X' : options.max_insert_size,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
305 '-I' : options.min_insert_size if options.min_insert_size > 0 else None
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
306 },
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
307 BOWTIE2 : {
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
308 '--ff' : asktag == 'N',
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
309 '--fr' : asktag == 'Y',
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
310 '-X' : options.max_insert_size,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
311 '-I' : options.min_insert_size if options.min_insert_size > 0 else None,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
312 '--no-discordant' : True,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
313 '--no-mixed' : True
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
314 },
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
315 SOAP: {
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
316 '-x' : options.max_insert_size,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
317 '-m' : options.min_insert_size if options.min_insert_size > 0 else 100
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
318 }}[options.aligner],
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
319 # integrating 'rmappe' is different from others
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
320 **aligner_options)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
321
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
322 aligner_command = aligner_exec + aligner_options_string() + \
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
323 { BOWTIE : ' %(reference_genome)s -f -1 %(input_file_1)s -2 %(input_file_2)s %(output_file)s',
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
324 BOWTIE2 : ' -x %(reference_genome)s -f -1 %(input_file_1)s -2 %(input_file_2)s -S %(output_file)s',
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
325 SOAP : ' -D %(reference_genome)s.fa.index -o %(output_file)s -a %(input_file_1)s -b %(input_file_2)s -2 %(output_file)s.unpaired' #,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
326 # RMAP : # rmappe, also paste two inputs into one file.
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
327 }[options.aligner]
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
328
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
329 logm('Aligner command: %s' % aligner_command)
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
330
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
331 bs_pair_end(options.infilename_1,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
332 options.infilename_2,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
333 asktag,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
334 options.adapter_file,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
335 options.cutnumber1,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
336 options.cutnumber2,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
337 options.no_split,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
338 str_no_mismatches,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
339 aligner_command,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
340 db_path,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
341 tmp_path,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
342 outfile,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
343 XS_pct,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
344 XS_count,
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
345 options.Output_multiple_hit
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
346 )
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
347
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
348 outfile.close()
e6df770c0e58 Initial upload
weilong-guo
parents:
diff changeset
349