annotate trf_wrapper.py @ 1:2a3c8b2d5979 default tip

Uploaded the wrapper file
author malex
date Wed, 30 Nov 2011 12:18:55 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
1 #!/usr/bin/env python
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
2
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
3 """
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
4 Runs TRF on a sequence file.
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
5 For use with trf version 4.04
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
6
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
7 usage: trf_wrapper.py input_file <required parameters> [options]
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
8 Required parameters:
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
9 match
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
10 mismatch
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
11 indels
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
12 match_probability (%, 10-100)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
13 indel_probability (%, 10-100)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
14 minimal score (30-150)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
15 maximum period size (1-2000)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
16 """
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
17
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
18 import optparse, os, shutil, subprocess, sys, re
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
19 from os.path import abspath
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
20
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
21 def remove_bgcolor(infilename,outfilename):
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
22 infile = open(infilename, "r")
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
23 outfile = open(outfilename, "w")
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
24 bgcolor = re.compile(r'FBF8BC')
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
25 for line in infile:
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
26 line = bgcolor.sub('FFFFFF', line)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
27 outfile.write(line)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
28 infile.close()
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
29 outfile.close()
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
30 os.remove(infilename)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
31
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
32 def remove_html(infilename,outfilename):
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
33 infile = open(infilename, "r")
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
34 outfile = open(outfilename, "w")
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
35 opena = re.compile(r'<A\ HREF.*?>')
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
36 bgcolor = re.compile(r'FBF8BC')
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
37 closea = re.compile(r'</A>')
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
38 for line in infile:
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
39 line = opena.sub('', line)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
40 line = bgcolor.sub('FFFFFF', line)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
41 line = closea.sub('', line)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
42 outfile.write(line)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
43 infile.close()
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
44 outfile.close()
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
45 os.remove(infilename)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
46
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
47 def stop_err(msg):
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
48 sys.stderr.write("%s\n" % msg)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
49 sys.exit()
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
50
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
51 def __main__():
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
52 #Parse arguments
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
53 usage = "usage: %prog input match mismatch indels match_p indel_p min_score max_period [options]"
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
54 parser = optparse.OptionParser(usage)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
55 parser.add_option( '-f', '--flanking', dest='flanking', action='store_true', help='Flanking sequence' )
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
56 parser.add_option( '-m', '--masked', dest='masked', action='store_true', help='Masked sequence' )
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
57 parser.add_option( '-r', '--redundancy', dest='redundancy', action='store_true', help='No redundance elimination' )
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
58 parser.add_option( '-o', '--datoutput', dest='datoutput', action='store', help='Output data file name' )
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
59 parser.add_option( '-k', '--maskoutput', dest='maskoutput', action='store', help='Output mask file name' )
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
60 parser.add_option( '-t', '--report', dest='report', action='store', help='Report file name' )
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
61 parser.add_option( '-i', '--indices', dest='indices', action='store', help='Indices file name' )
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
62 (opts, arguments) = parser.parse_args()
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
63
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
64 # Arguments and options
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
65 if len(arguments) != 8:
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
66 print(usage)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
67 stop_err('Wrong number of arguments passed')
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
68 output_dat = "%s.%s.%s.%s.%s.%s.%s.%s.dat" % (tuple(arguments))
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
69 output_mask = "%s.%s.%s.%s.%s.%s.%s.%s.mask" % (tuple(arguments))
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
70 output_report = "%s.%s.%s.%s.%s.%s.%s.%s.1.html" % (tuple(arguments))
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
71 output_indices = "%s.%s.%s.%s.%s.%s.%s.%s.1.txt.html" % (tuple(arguments))
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
72
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
73 if opts.masked and opts.masked == True:
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
74 arguments.append('-m')
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
75 if opts.flanking and opts.flanking == True:
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
76 arguments.append('-f')
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
77 if opts.redundancy and opts.redundancy == True:
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
78 arguments.append('-r')
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
79 if opts.datoutput and opts.datoutput != '':
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
80 output_dat_filename = opts.datoutput
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
81 if opts.maskoutput and opts.maskoutput != '':
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
82 output_mask_filename = opts.maskoutput
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
83 if opts.report and opts.report != '':
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
84 output_report_filename = opts.report
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
85 if opts.indices and opts.indices != '':
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
86 output_indices_filename = opts.indices
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
87
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
88 # Run
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
89 cmd = arguments
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
90 cmd.insert(0,"trf")
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
91 # Produce both html and dat files
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
92 cmd.append("-d")
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
93 try:
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
94 proc = subprocess.Popen(args=cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
95 except Exception, err:
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
96 sys.stderr.write("Error invoking command: \n%s\n\n%s\n" % (cmd, err))
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
97 sys.exit(1)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
98 stdout, stderr = proc.communicate()
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
99 return_code = proc.returncode
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
100 if return_code != 1:
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
101 sys.stdout.write(stdout)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
102 sys.stderr.write(stderr)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
103 sys.stderr.write("Return error code %i from command:\n" % return_code)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
104 sys.stderr.write("%s\n" % cmd)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
105 else:
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
106 sys.stdout.write(stdout)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
107 sys.stdout.write(stderr)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
108 cdir = os.getcwd()
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
109 file_list = os.listdir(cdir)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
110 try:
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
111 report_file = os.path.basename(output_report)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
112 indices_file = os.path.basename(output_indices)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
113 remove_html(report_file, output_report_filename)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
114 remove_bgcolor(indices_file, output_indices_filename)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
115 shutil.move(os.path.basename(output_dat), output_dat_filename)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
116 # shutil.copyfile(os.path.basename(output_report), output_report_filename)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
117 # shutil.copyfile(os.path.basename(output_indices), output_indices_filename)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
118 if opts.masked and opts.masked == True:
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
119 shutil.move(os.path.basename(output_mask), output_mask_filename)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
120 except Exception, err:
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
121 sys.stderr.write("Error copying output files: \n%s\n" % err)
2a3c8b2d5979 Uploaded the wrapper file
malex
parents:
diff changeset
122 if __name__=="__main__": __main__()