annotate tools/metag_tools/shrimp_color_wrapper.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 SHRiMP wrapper : Color space
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 import os, sys, tempfile, os.path, re
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 assert sys.version_info[:2] >= (2.4)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 def stop_err( msg ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 sys.stderr.write( "%s\n" % msg )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 def __main__():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 # SHRiMP path
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 shrimp = 'rmapper-cs'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 # I/O
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 input_target_file = sys.argv[1] # fasta
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 input_query_file = sys.argv[2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 shrimp_outfile = sys.argv[3] # shrimp output
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 # SHRiMP parameters
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 spaced_seed = '1111001111'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 seed_matches_per_window = '2'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 seed_hit_taboo_length = '4'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 seed_generation_taboo_length = '0'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 seed_window_length = '115.0'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 max_hits_per_read = '100'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 max_read_length = '1000'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 kmer = '-1'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 sw_match_value = '100'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 sw_mismatch_value = '-150'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 sw_gap_open_ref = '-400'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 sw_gap_open_query = '-400'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 sw_gap_ext_ref = '-70'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 sw_gap_ext_query = '-70'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 sw_crossover_penalty = '-140'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 sw_full_hit_threshold = '68.0'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 sw_vector_hit_threshold = '60.0'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 # TODO: put the threshold on each of these parameters
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 if len(sys.argv) > 4:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 if sys.argv[4].isdigit():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 spaced_seed = sys.argv[4]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 stop_err('Error in assigning parameter: Spaced seed.')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 stop_err('Spaced seed must be a combination of 1s and 0s.')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 seed_matches_per_window = sys.argv[5]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 seed_hit_taboo_length = sys.argv[6]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 seed_generation_taboo_length = sys.argv[7]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 seed_window_length = sys.argv[8]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 max_hits_per_read = sys.argv[9]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 max_read_length = sys.argv[10]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 kmer = sys.argv[11]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 sw_match_value = sys.argv[12]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 sw_mismatch_value = sys.argv[13]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 sw_gap_open_ref = sys.argv[14]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 sw_gap_open_query = sys.argv[15]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 sw_gap_ext_ref = sys.argv[16]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 sw_gap_ext_query = sys.argv[17]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 sw_crossover_penalty = sys.argv[18]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 sw_full_hit_threshold = sys.argv[19]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 sw_vector_hit_threshold = sys.argv[20]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 # temp file for shrimp log file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 shrimp_log = tempfile.NamedTemporaryFile().name
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 # SHRiMP command
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 command = ' '.join([shrimp, '-s', spaced_seed, '-n', seed_matches_per_window, '-t', seed_hit_taboo_length, '-9', seed_generation_taboo_length, '-w', seed_window_length, '-o', max_hits_per_read, '-r', max_read_length, '-d', kmer, '-m', sw_match_value, '-i', sw_mismatch_value, '-g', sw_gap_open_ref, '-q', sw_gap_open_query, '-e', sw_gap_ext_ref, '-f', sw_gap_ext_query, '-x', sw_crossover_penalty, '-h', sw_full_hit_threshold, '-v', sw_vector_hit_threshold, input_query_file, input_target_file, '>', shrimp_outfile, '2>', shrimp_log])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 os.system(command)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 if os.path.exists(query_fasta): os.remove(query_fasta)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 if os.path.exists(query_qual): os.remove(query_qual)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 stop_err(str(e))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 # check SHRiMP output: count number of lines
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 num_hits = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 if shrimp_outfile:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 for i, line in enumerate(file(shrimp_outfile)):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 line = line.rstrip('\r\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 if not line or line.startswith('#'): continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 fields = line.split()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 num_hits += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 stop_err(str(e))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 if num_hits == 0: # no hits generated
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 err_msg = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 if shrimp_log:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 for i, line in enumerate(file(shrimp_log)):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 if line.startswith('error'): # deal with memory error:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 err_msg += line # error: realloc failed: Cannot allocate memory
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 if re.search('Reads Matched', line): # deal with zero hits
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 if int(line[8:].split()[2]) == 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 err_msg = 'Zero hits found.\n'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 stop_err('SHRiMP Failed due to:\n' + err_msg)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 # remove temp. files
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 if os.path.exists(shrimp_log): os.remove(shrimp_log)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 if __name__ == '__main__': __main__()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116