annotate rmapq_wrapper.py @ 1:67e51dec71d0 draft default tip

planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
author devteam
date Tue, 13 Oct 2015 12:29:41 -0400
parents bccaba937482
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/env python
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
2
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
3 import os, sys, tempfile
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
4
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
5 assert sys.version_info[:2] >= (2.4)
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
6
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
7 def stop_err( msg ):
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
8
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
9 sys.stderr.write( "%s\n" % msg )
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
10 sys.exit()
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
11
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
12
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
13 def __main__():
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
14
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
15 # I/O
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
16 target_path = sys.argv[1]
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
17 infile = sys.argv[2]
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
18 scorefile = sys.argv[3]
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
19 high_score = sys.argv[4] # -q
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
20 high_len = sys.argv[5] # -M
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
21 read_len = sys.argv[6] # -w
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
22 align_len = sys.argv[7] # -h
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
23 mismatch = sys.argv[8] # -m
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
24 output_file = sys.argv[9]
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
25
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
26 try:
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
27 float(high_score)
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
28 except:
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
29 stop_err('Invalid value for minimal quality score.')
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
30
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
31 try:
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
32 int(high_len)
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
33 except:
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
34 stop_err('Invalid value for minimal high quality bases.')
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
35
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
36 # first guess the read length
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
37 guess_read_len = 0
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
38 seq = ''
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
39 for i, line in enumerate(open(infile)):
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
40 line = line.rstrip('\r\n')
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
41 if line.startswith('>'):
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
42 if seq:
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
43 guess_read_len = len(seq)
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
44 break
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
45 else:
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
46 seq += line
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
47
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
48 try:
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
49 test = int(read_len)
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
50 if test == 0:
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
51 read_len = str(guess_read_len)
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
52 else:
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
53 assert test >= 20 and test <= 64
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
54 except:
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
55 stop_err('Invalid value for read length. Must be between 20 and 64.')
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
56
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
57
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
58 try:
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
59 int(align_len)
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
60 except:
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
61 stop_err('Invalid value for minimal length of a hit.')
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
62
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
63 try:
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
64 int(mismatch)
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
65 except:
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
66 stop_err('Invalid value for mismatch numbers in an alignment.')
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
67
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
68 all_files = []
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
69 if os.path.isdir(target_path):
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
70 # check target genome
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
71 fa_files = os.listdir(target_path)
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
72
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
73 for file in fa_files:
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
74 file = "%s/%s" % ( target_path, file )
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
75 file = os.path.normpath(file)
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
76 all_files.append(file)
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
77 else:
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
78 stop_err("No sequences for %s are available for search, please report this error." %(target_path))
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
79
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
80 for detail_file_path in all_files:
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
81 output_tempfile = tempfile.NamedTemporaryFile().name
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
82 command = "rmapq -q %s -M %s -h %s -w %s -m %s -Q %s -c %s %s -o %s 2>&1" % ( high_score, high_len, align_len, read_len, mismatch, scorefile, detail_file_path, infile, output_tempfile )
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
83 #print command
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
84 try:
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
85 os.system( command )
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
86 except Exception, e:
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
87 stop_err( str( e ) )
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
88
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
89 try:
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
90 assert os.system( 'cat %s >> %s' % ( output_tempfile, output_file ) ) == 0
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
91 except Exception, e:
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
92 stop_err( str( e ) )
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
93
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
94 try:
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
95 os.remove( output_tempfile )
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
96 except:
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
97 pass
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
98
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
99
bccaba937482 Imported from capsule None
devteam
parents:
diff changeset
100 if __name__ == '__main__': __main__()