Mercurial > repos > bgruening > rrna
comparison rna_hmm3.py @ 0:1a12c379df0c draft default tip
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rRNA commit 1973f3035c10db80883d80847ea254289f5cce2a-dirty
author | bgruening |
---|---|
date | Thu, 17 Sep 2015 16:50:41 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1a12c379df0c |
---|---|
1 #! /usr/bin/env python | |
2 import os | |
3 import re | |
4 import sys | |
5 import string | |
6 import optparse | |
7 import fasta | |
8 import math | |
9 import tempfile | |
10 | |
11 def format(seq, N=60): | |
12 nseg = int(math.ceil(len(seq)/(N+0.0))) | |
13 return '\n'.join([seq[i*N:(i+1)*N] for i in range(nseg)]) | |
14 # write into fasta format file | |
15 | |
16 parser = optparse.OptionParser(version="%prog ") | |
17 | |
18 parser.add_option("-i", "--input", dest="input_fasta",action="store", | |
19 help="name of input file in fasta format") | |
20 parser.add_option("-L", "--LibHmm", dest="hmm_path",action="store", | |
21 default="HMM3",help="path of hmm database") | |
22 parser.add_option("--gff", dest="out_gff",action="store", | |
23 help="name of output gff file") | |
24 parser.add_option("--seq", dest="out_seq",action="store", | |
25 help="name of output sequence file") | |
26 parser.add_option("--mask", dest="out_mask",action="store", | |
27 help="name of output mask file") | |
28 parser.add_option("-k", "--kingdoms", dest="kingdoms",action="store", | |
29 default="arc,bac,euk",help="kingdom used") | |
30 parser.add_option("-m", "--moltypes", dest="moltypes",action="store", | |
31 default="lsu,ssu,tsu",help="molecule type detected") | |
32 parser.add_option("-e","--Evalue", dest="evalue",action="store",type="float", | |
33 default=0.01,help="evalue cut-off for hmmsearch") | |
34 parser.add_option("--cpu", dest="cpu",action="store",type="int", | |
35 default=4,help="number of cpus hmmsearch should use") | |
36 | |
37 | |
38 try: | |
39 (options, args) = parser.parse_args() | |
40 except: | |
41 parser.print_help() | |
42 sys.exit(1) | |
43 | |
44 if options.input_fasta is None or options.hmm_path is None: | |
45 parser.print_help() | |
46 sys.exit(1) | |
47 | |
48 #print "%s"% os.path.abspath(options.hmm_path) | |
49 #os.environ["HMMERDB"] += ":"+os.path.abspath(options.hmm_path) | |
50 #print os.environ["HMMERDB"] | |
51 fname = os.path.abspath(options.input_fasta) | |
52 | |
53 tr = string.maketrans("gatcryswkmbdhvnGATCRYSWKMBDHVN","ctagyrswmkvhdbnCTAGYRSWMKVHDBN") | |
54 | |
55 | |
56 def rev_record(record): | |
57 return ">"+record.header+"|rev\n"+format(record.sequence[::-1].translate(tr)) | |
58 | |
59 | |
60 records = [rec for rec in fasta.fasta_itr(fname)] | |
61 headers = [[rec.header,len(rec.sequence)] for rec in records] | |
62 | |
63 | |
64 temp_fasta = tempfile.NamedTemporaryFile(delete=False) | |
65 ff = open(temp_fasta.name,'w') | |
66 for (i, rec) in enumerate(records): | |
67 ff.write('>s'+str(i)+'\n'+format(rec.sequence)+'\n') | |
68 ff.write('>s'+str(i)+'|rev\n'+format(rec.sequence[::-1].translate(tr))+'\n') | |
69 ff.close() | |
70 #sys.exit(1) | |
71 # a temporary fasta file, use s(int) to easy the parsing | |
72 | |
73 def parse_hmmsearch(kingdom, moltype, src): | |
74 # function to parse hmmsearch output | |
75 resu = [] | |
76 data = open(src).readlines() | |
77 inds = [-1]+[i for (i,x) in enumerate(data[2]) if x==" "] | |
78 inds = [(inds[j]+1,inds[j+1]) for j in range(len(inds)-1)] | |
79 data = [line for line in data if line[0] != "#"] | |
80 for line in data: | |
81 if not len(line.strip()): | |
82 continue | |
83 [read, acc, tlen, qname, qaccr, qlen, seq_evalue, seq_score, seq_bias, \ | |
84 seq_num, seq_of, dom_cEvalue, dom_iEvalue, dom_score, dom_bias, \ | |
85 hmm_start, hmm_end, dom_start, dom_end, env_start, env_end] = \ | |
86 line.split()[:21] | |
87 # [line[x[0]:x[1]].strip() for x in inds[:21]] | |
88 if string.atof(dom_iEvalue) < options.evalue: | |
89 # resu.append("\t".join([read, acc, tlen, qname, qaccr, \ | |
90 # qlen, seq_evalue, seq_score, seq_bias, seq_num, seq_of, \ | |
91 # dom_cEvalue, dom_iEvalue, dom_score, dom_bias, hmm_start, \ | |
92 # hmm_end, dom_start, dom_end, env_start, env_end])) | |
93 resu.append("\t".join([qname, dom_start, dom_end, read, dom_iEvalue])) | |
94 | |
95 # print resu[0] | |
96 # print resu[-1] | |
97 return resu | |
98 | |
99 | |
100 | |
101 hmm_resu = [] | |
102 for kingdom in options.kingdoms.split(','): | |
103 for moltype in options.moltypes.split(','): | |
104 #print kingdom, moltype | |
105 #hmm_out_fname = "%s.%s_%s.out" % (out_fname, kingdom, moltype) | |
106 temp_hmm_out = tempfile.NamedTemporaryFile(delete=False) | |
107 #dom_out_fname = "%s.%s_%s.dom" % (out_fname, kingdom, moltype) | |
108 temp_dom_out = tempfile.NamedTemporaryFile(delete=False) | |
109 #cmd = '/home/thumper6/camera-annotation/sitao/hmmer3.0/hmmer-3.0-linux-intel-x86_64/binaries/hmmsearch --cpu 1 -o %s --domtblout %s -E %g %s/%s_%s.hmm %s' % \ | |
110 cmd = 'hmmsearch --cpu %s -o %s --domtblout %s -E %g %s/%s_%s.hmm %s' % \ | |
111 (options.cpu, temp_hmm_out.name, temp_dom_out.name, \ | |
112 options.evalue,os.path.abspath(options.hmm_path),kingdom,moltype,temp_fasta.name) | |
113 # hmm_resu += parse_hmmsearch(os.popen(cmd)) | |
114 os.system(cmd) | |
115 hmm_resu += parse_hmmsearch(kingdom, moltype, temp_dom_out.name) | |
116 os.remove(temp_hmm_out.name) | |
117 os.remove(temp_dom_out.name) | |
118 | |
119 dict_read2kingdom = {} | |
120 for line in hmm_resu: | |
121 [feature_type, r_start, r_end, read, evalue] = line.strip().split('\t') | |
122 read = read.split('|')[0] | |
123 evalue = string.atof(evalue) | |
124 kingdom = feature_type.split('_')[0] | |
125 if read in dict_read2kingdom: | |
126 if evalue < dict_read2kingdom[read][1]: | |
127 dict_read2kingdom[read] = [kingdom, evalue] | |
128 else: | |
129 dict_read2kingdom[read] = [kingdom, evalue] | |
130 | |
131 header = ['##seq_name','method','feature','start','end','evalue','strand','gene'] | |
132 ff = open(options.out_gff,"w") | |
133 dict_rRNA = {'arc_lsu':'Archaeal:23S_rRNA','arc_ssu':'Archaeal:16S_rRNA','arc_tsu':'Archaeal:5S_rRNA', | |
134 'bac_lsu':'Bacterial:23S_rRNA','bac_ssu':'Bacterial:16S_rRNA','bac_tsu':'Bacterial:5S_rRNA', | |
135 'euk_lsu':'Eukaryotic:28S_rRNA','euk_ssu':'Eukaryotic18S_rRNA','euk_tsu':'Eukaryotic:8S_rRNA'} | |
136 | |
137 ff.write('\t'.join(header)+'\n') | |
138 for line in hmm_resu: | |
139 # [kingdom, moltype, read, acc, tlen, qname, qaccr, \ | |
140 # qlen, seq_evalue, seq_score, seq_bias, seq_num, seq_of, \ | |
141 # dom_cEvalue, dom_iEvalue, dom_score, dom_bias, hmm_start, \ | |
142 # hmm_end, dom_start, dom_end, env_start, env_end] = line.strip().split('\t') | |
143 [feature_type, r_start, r_end, read, evalue] = line.strip().split('\t') | |
144 if dict_read2kingdom[read.split('|')[0]][0] != feature_type.split('_')[0]: | |
145 continue | |
146 feature_type = dict_rRNA[feature_type] | |
147 if read.endswith('|rev'): | |
148 strand = '-' | |
149 tmp = map(string.atoi,[r_start,r_end]) | |
150 pos = string.atoi(read[1:-4]) | |
151 header = headers[pos][0] | |
152 L = headers[pos][1] | |
153 [r_end,r_start] = [str(L+1-x) for x in tmp] | |
154 else: | |
155 strand = '+' | |
156 pos = string.atoi(read[1:]) | |
157 header = headers[pos][0] | |
158 ff.write('\t'.join([header.split()[0], 'rna_hmm3','rRNA',r_start,r_end,evalue,strand,feature_type])+'\n') | |
159 ff.close() | |
160 | |
161 | |
162 os.remove(temp_fasta.name) | |
163 | |
164 #postprocessing | |
165 rRNA_dict={} | |
166 | |
167 if options.out_gff: | |
168 for line in open(options.out_gff).readlines()[1:]: | |
169 line=line.strip().split() | |
170 #print '%s %s'% (line[0],line[1]) | |
171 if rRNA_dict.get(line[0],None) is None: | |
172 rRNA_dict[line[0]] = [] | |
173 #else: | |
174 rRNA_dict[line[0]].append( (string.atoi(line[3]),string.atoi(line[4]),line[6],line[7])) | |
175 | |
176 if options.out_mask: | |
177 f_mask = open(options.out_mask,"w") | |
178 | |
179 if options.out_seq: | |
180 f_seq = open(options.out_seq,"w") | |
181 for rec in fasta.fasta_itr( options.input_fasta ): | |
182 header = rec.header.split()[0] | |
183 seq = rec.sequence[:] | |
184 tno = 1 | |
185 for (start,end,strand,rRNA_type) in rRNA_dict.get(header,[]): | |
186 seq = seq[:(start-1)]+'N'*(end-start+1)+seq[end:] | |
187 | |
188 #print "%s %d"% (header,tno) | |
189 if options.out_seq: | |
190 f_seq.write(">%s.%d /start=%d /end=%d /strand=%s %s\n" % (header,tno,start,end,strand,rRNA_type)) | |
191 if strand=="+": # forward strand | |
192 f_seq.write(format(rec.sequence[(start-1):end])+"\n") | |
193 else: | |
194 f_seq.write(format(rec.sequence[(start-1):end][::-1].translate(tr))+"\n") | |
195 tno = tno+1 | |
196 | |
197 #f_mask.write(">"+header+"\n") | |
198 #next line by liwz | |
199 if options.out_mask: | |
200 f_mask.write(">"+rec.header+"\n") | |
201 f_mask.write(format(seq)+"\n") | |
202 | |
203 if options.out_mask: | |
204 f_mask.close() | |
205 if options.out_seq: | |
206 f_seq.close() | |
207 | |
208 | |
209 |