annotate rmap_wrapper.py @ 1:264b036c0832 draft default tip

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