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