-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEAHelitronMFE
427 lines (380 loc) · 15.5 KB
/
EAHelitronMFE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/usr/bin/env perl
#AUTHORS
# Kaining Hu (c) 2023
# Easy to Annotate Helitrons command line, include MFE filter version (EAHelitronMFE) v1.6000 2023/10/05
use strict;
use warnings;
use 5.010;
use Getopt::Long;
use Pod::Usage;
use Time::HiRes 'time';
# use File::Temp qw( tempdir );
#use Parallel::ForkManager;
#our $MAX_processes=2;
#my $pm=Parallel::ForkManager->new($MAX_processes);
use re 'eval';
sub getLoggingTime {
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
# my $nice_timestamp = sprintf ( "%04d%02d%02d %02d:%02d:%02d",
# $year+1900,$mon+1,$mday,$hour,$min,$sec);
my $nice_timestamp = sprintf ( "%04d%02d%02d_%02d_%02d_%02d",
$year+1900,$mon+1,$mday,$hour,$min,$sec);
return $nice_timestamp;
}
our $timestamp = getLoggingTime();
# print "$timestamp\n";
our $opfn="EAHeli_out";
my $verbose;
our $upstreml=3000;
our $downstreml=500;
our $fuzzylv=0;
our $finalCTAGT="ctagt";
our $MFET= -5;
#our $hairpinpattern="[gc]{4}[at]{1}[gc]{1}|[gc]{3}[at]{1}[gc]{2}|[gc]{2}[at]{1}[gc]{3}|[gc]{1}[at]{1}[gc]{4}|[gc]{5}";
our $hairpinpattern="[gc]{4}[at]{1}[gc]{1}|[gc]{3}[at]{1}[gc]{2}|[gc]{2}[at]{1}[gc]{3}|[gc]{1}[at]{1}[gc]{4}|[gc]{5}|[gc]{4}[at]{2}[gc]{1}|[gc]{3}[at]{2}[gc]{2}|[gc]{2}[at]{2}[gc]{3}|[gc]{1}[at]{2}[gc]{4}|[gc]{1}[at]{1}[gc]{1}[at]{1}[gc]{3}|[gc]{1}[at]{1}[gc]{2}[at]{1}[gc]{2}|[gc]{1}[at]{1}[gc]{3}[at]{1}[gc]{1}|[gc]{2}[at]{1}[gc]{1}[at]{1}[gc]{2}|[gc]{2}[at]{1}[gc]{2}[at]{1}[gc]{1}|[gc]{3}[at]{1}[gc]{1}[at]{1}[gc]{1}";
our $TCpattern="TC(TCTACTA|T.TACTA.T|T.TACTAC|.{2}TACTACT|T.TAC.ACT|T.TA.TACT|T.TACT.CT|T.T.CTACT|.CTACTA.T|.{9}TATTAAG)";
our @CTAGT=qw/
CTAGT
CT[AG]GT
CTA[AG]T
CT[AG]{2}T
CT[AG]{2}.{1}
CTAG.{1}
/;
#our $seqfilename ='';
#GetOptions("i=s"=>\$seqfilename,"o=s" => \$opfn,"verbose"=>\$verbose)
#or die("Error in command line arguments\n perl Searchhelitron -o <outputfile> (inputfile)\n");
GetOptions("o=s" => \$opfn,"verbose"=>\$verbose,"u=i"=>\$upstreml,"d=i"=>\$downstreml, "r=i"=>\$fuzzylv,"T=s" => \$TCpattern,"H=s" => \$hairpinpattern,"E=f" => \$MFET)
or die("[-]Error in command line arguments
Usage: perl EAHelitronMFE [options] <input FASTA file(s)>
options:
[-o string|output prefix Default: EAHeli_out]
[-u int|upstream length Default: 3000]
[-d int|downstream length Default: 500]
Advanced options:
[-T string|TC pattern. User's 5'TC pattern]
[-H string|Hairpin pattern. User's Hairpin left pattern]
[-E float|minimum free energy (MFE) threshold, default: -5]
[-r int[0-5]|CTRRt 3' terminal fuzzy level;
0: CTAGT
1: CT[AG]GT
2: CTA[AG]T
3: CT[AG]{2}T
4: CT[AG]{2}.{1}
5: CTAG.{1}
Default: 0]
Note: Easy to Annotate Helitrons command line, include MFE filter version (EAHelitronMFE) v1.6000 2023/10/05.\n");
###################sub TRseq##########
#say $fuzzylv;
#if ($fuzzylv==""){
# $fuzzylv=0;
#}elsif ($fuzzylv>5 or $fuzzylv<0) {
# die ("Please input fuzzy levle in int 0 to 5.\n");
#}
if ($fuzzylv>5 or $fuzzylv<0) {
die ("Please input fuzzy levle in int 0 to 5.\n");
}
#say $fuzzylv;
$finalCTAGT = $CTAGT[$fuzzylv];
print "CTAGT fuzzy level: $fuzzylv $finalCTAGT\n";
print "TCpattern: $TCpattern\n";
print "hairpin left sequence pattern: $hairpinpattern\n";
print "upstream length: $upstreml\n";
print "downstream length: $downstreml \n";
print "minimum free energy (MFE): $MFET\n";
# print "CTAGT fuzzy level: $fuzzylv $finalCTAGT\n";
if ($opfn eq ""){
$opfn="EAHeli_out";
print "output prefix:$opfn\n";
}else{
print "output prefix:$opfn\n";
}
sub TRseq($)
{
my ($pinseq) = @_;
#say $pinseq;
my $pinseqtr = reverse $pinseq;
#say $pinseqtr;
$pinseqtr =~ tr/ACGTacgt/TGCAtgca/;
#say $pinseqtr;
return $pinseqtr;
}
##################TRseq End#############
###################loading############
our $loadingstarttime=time();
print "\nStart loading genomeic sequence.\n";
#if(!open SEQFILENAME,"< $seqfilename"){
# die "File not Found\n";
#}
#if(!open SEQFILENAME,"< $_"){
# die "File not Found\n";
#}
our $Chri=0;
our @Chrname=();
our @Chrseq=();
#@ARGV = qw#'' Not_Find_a_File#;
#say @ARGV;
#say $0;
while(defined(our $seq = <>)){
#while(our $seq = <SEQFILENAME>){
if ($seq =~ m/^.*>/) {
#$seq=~ m/^.*>([a-zA-Z0-9_.-]+) ?.*/;
$seq=~ m/^\s*>\s*(\S+)\s*(.*)/; #1.52 new RE based on BioPerl
print "$1\n";
$Chrname[$Chri]= $1;
$Chri++;
}else{
$seq =~ s/\s//;
$seq =~ tr/MRWSYKVHDBmrwsykvhdb/CGTGCGGCGGCGTGCGGCGG/;#1.31add snp replace
$Chrseq[$Chri-1] .=$seq;
}
}
#close SEQFILENAME;
our $loadingendtime=time();
print "$Chri Sequences\n";
print "Finished loading!\n";
printf "%g Sec %g Min\n",$loadingendtime-$loadingstarttime,($loadingendtime-$loadingstarttime)/60;
our $starttime=time();
#our $hairpinpattern="cccgccc";
#our $hairpinpattern="[gc]{4}[at]{1}[gc]{1}|[gc]{3}[at]{1}[gc]{2}|[gc]{2}[at]{1}[gc]{3}|[gc]{1}[at]{1}[gc]{4}|[gc]{5}";
#say our $testseq='([atcg]{10}([gc]{4}[at]{1}[gc]{1}|[gc]{3}[at]{1}[gc]{2}|[gc]{2}[at]{1}[gc]{3}|[gc]{1}[at]{1}[gc]{4}|[gc]{5})([atcg]{0,4}[at]{1}[atgc]{0,4})(??{TRseq($2)})([atcg]{3,12}ctagt)[atcgn]{4})';
#say our $testseq='([atcg]{10}([gc]{4}[at]{1}[gc]{1}|[gc]{3}[at]{1}[gc]{2}|[gc]{2}[at]{1}[gc]{3}|[gc]{1}[at]{1}[gc]{4}|[gc]{5})([atcg]{0,4}[at]{1}[atgc]{0,4})(??{TRseq($2)})([atcg]{3,12}'."$finalCTAGT".')[atcgn]{4})';
say our $testseq='([atcg]{10}('."$hairpinpattern".')([atcg]{0,4}[at]{1}[atgc]{0,4})(??{TRseq($2)})([atcg]{3,12}'."$finalCTAGT".')[atcgn]{4})';
#say our $TCseq='([atgcn]{5}(TC(TCTACTA|T.TACTA.T|T.TACTAC|.{2}TACTACT|T.TAC.ACT|T.TA.TACT|T.TACT.CT|T.T.CTACT|.CTACTA.T|.{9}TATTAAG))[atgcn]{20})';
say our $TCseq='([atgcn]{5}('."$TCpattern".')[atgcn]{20})';
print "\nRunning. Please wait for a few minutes.\n";
#####################################
#Start main
#####################################
open RSUT,"> $opfn.3.txt" or die ("[-] Error: Can't open or create $opfn.3.txt\n");
open FASTARESULT,"> $opfn.u$upstreml.fa" or die ("[-] Error: Can't open or create $opfn.u$upstreml.fa\n");
open TCRSUT, "> $opfn.5.txt" or die ("[-] Error: Can't open or create $opfn.5.txt\n");
open TCFARESULT, "> $opfn.5.fa" or die ("[-] Error: Can't open or create $opfn.5.fa\n");
open OUTGFF, "> $opfn.gff3" or die ("[-] Error: Can't open or create $opfn.gff3\n");
print OUTGFF "##gff-version 3\n";
open DOWNFA,"> $opfn.down$downstreml.fa" or die ("[-] Error: Can't open or create $opfn.down$downstreml.fa\n");
open GENOMELENH,"> $opfn.len.txt" or die ("[-] Error: Can't open or create $opfn.len.txt\n");
print GENOMELENH "Seqname\tSeqLength\t[+]\t[-]\tall\t[+]Helitron Density\t[-]Helitron Density\tSeq Helitron Density\n";
open OUTBED, "> $opfn.bed" or die ("[-] Error: Can't open or create $opfn.bed\n");
our $genomel =0;
our $countall=0;
our $count1all=0;
our $count2all=0;
# our $tmpHPMFE=0;
######## For start !!!##################
for (our $ni=0;$ni<$Chri;$ni++){
say "Deal with $Chrname[$ni].";
our $count1=1;
our $countTMP=0;
our $seqall = $Chrseq[$ni];
################Start deal with plus seqs.################
while ($seqall=~ /$testseq/igc){
#while ($seqall =~ /$testseq/igc){
#say $seqfilename;
#say "Chrom:",$AN;
# $count1++; ## 2023.10.05 move to the last step.
$countTMP++;
#say "No.",$count1;
our $l3=length ($3);
our $l1=length ($1);
our $seqpos=pos($seqall)-5;
our $seqstarpos=$seqpos-$l1+16;
our $seqdownpos=$seqpos+$downstreml;
our $ChrID=$Chrname[$ni];
our $HID ="$ChrID"."H$count1";
##### Calculate MFE
#say $HID;
my $tmp_hairpin_name = "cache.$timestamp.$opfn.$HID.tmp$countTMP";
my $tmp_hairpin_fa = substr($1, 0, -9); # Clip last 9 nt.
open TEMPHP,">$tmp_hairpin_name.txt" or die ("[-] Error: Can't open or create temp file $tmp_hairpin_name.txt\n");
print TEMPHP ">tmp.$HID $ChrID:$seqstarpos..$seqpos $l3 $2 $3 $4\n";
print TEMPHP "$tmp_hairpin_fa\n";
close TEMPHP;
## RNAfold
system("RNAfold", "--noPS", "$tmp_hairpin_name.txt", "-o$tmp_hairpin_name.MFE.txt");
unlink "$tmp_hairpin_name.txt";
open TEMPHPMFE, "$tmp_hairpin_name.MFE.txt" or die ("[-] Error: Can't open temp MFE file $tmp_hairpin_name.MFE.txt\n");
my $tmpHPMFE=1;
while(my $tmpMFE = <TEMPHPMFE>){
# say $.;
if ($. == 3 ){
$tmpHPMFE=substr($tmpMFE,-8);
$tmpHPMFE=substr($tmpHPMFE,0,6);
# say $tmpHPMFE;
$tmpHPMFE=$tmpHPMFE+0.0;
# say $tmpHPMFE;
}else{
next;
}
}
close TEMPHPMFE;
unlink "$tmp_hairpin_name.MFE.txt";
##### End of Calculate MFE
# say $tmpHPMFE;
# say $MFET;
if ($tmpHPMFE <= $MFET){
# say $tmpHPMFE;
# say $count1;
# say $countTMP;
print RSUT ">$HID $ChrID:$seqstarpos..$seqpos $l3 $2 $3 $4 $tmpHPMFE\n";
print RSUT "$1\n";
print FASTARESULT ">$ChrID"."H$count1.up $ChrID:$seqstarpos..$seqpos\n";
our $rightfinalseq="";
if ($seqpos<$upstreml){
$rightfinalseq = substr($seqall,1,$seqpos);
}else{
$rightfinalseq = substr($seqall,$seqpos-$upstreml,$upstreml);
}
print FASTARESULT "$rightfinalseq\n";
print DOWNFA ">$ChrID"."H$count1.down $ChrID:$seqpos..$seqdownpos\n";
our $downfasta = substr($seqall,$seqpos,$downstreml);
print DOWNFA "$downfasta\n";
print OUTGFF "$ChrID\tEAhelitron\tCDS\t$seqstarpos\t$seqpos\t.\t+\t.\tName=$HID.3;ID=$HID;Parent=$HID;Type=Helitron;Super_Family=Helitron;MFE=$tmpHPMFE\n";
print OUTBED "$ChrID\t".($seqstarpos-1)."\t".($seqpos)."\t$HID\t1\t+\n";
our $TCi=0;
#say our $TCseq='([atgcn]{10}(TC(TCTACTA|T.TACTA.T|T.TACTAC|.{2}TACTACT|T.TAC.ACT|T.TA.TACT|T.TACT.CT|T.T.CTACT|.CTACTA.T|.{9}TATTAAG))[atgcn]{15})';
our $rfsl=length($rightfinalseq);
while($rightfinalseq=~/$TCseq/igc){
$TCi++;
my $TCL=length($1)-25;
my $TCpos=pos($rightfinalseq)-20;
my $TCposstar=$TCpos-$TCL+1;
my $TCgpos=$seqpos-$rfsl+$TCpos;
my $TCgposstar=$seqpos-$rfsl+$TCposstar;
print TCRSUT ">$HID.5.$TCi $ChrID:$TCgposstar..$TCgpos $2\n";
print TCRSUT "$1\n";
print OUTGFF "$ChrID\tEAhelitron\tCDS\t$TCgposstar\t$TCgpos\t.\t+\t.\tName=$HID.5.$TCi;ID=$HID.5.$TCi;Parent=$HID;Type=Helitron;Super_Family=Helitron;MFE=$tmpHPMFE\n";
# print OUTBED "$ChrID\t".($seqstarpos-1)."\t".($seqpos-1)."\t$HID\t1\t+\n";
print TCFARESULT ">$HID.5.$TCi $ChrID:$TCgposstar..$seqpos\n";
my $FullHseq=substr($rightfinalseq,$TCposstar-1,$upstreml);
print TCFARESULT "$FullHseq\n";
}
$count1++; ## 2023.10.05 move to the last step.
}else{
next;
}
}
################ deal with plus seqs End#################
################Start TR-seqs#############################
$seqall = TRseq($seqall);
our $count2=1; ## 2023.10.05 move to the last step.
our $count2TMP=0;
our $seqalll = length($seqall);
while ($seqall=~ /$testseq/igc){
#while ($seqall =~ /$testseq/igc){
#say $seqfilename;
#say "Chrom:",$AN;
# $count2++;
$count2TMP++;
#say "No.",$count2;
our $l3=length ($3);
our $l1=length ($1);
our $seqpos=$seqalll-(pos($seqall)-5)+1;
our $seqstarpos=$seqpos+$l1-16;
our $ChrID=$Chrname[$ni];
our $trHID ="tr$ChrID"."H$count2";
#say $trHID;
##### Calculate MFE
#say $HID;
my $tmp_hairpin_name = "cache.$timestamp.$opfn.$trHID.tmp$count2TMP";
my $tmp_hairpin_fa = substr($1, 0, -9); # Clip last 9 nt.
open TEMPHP,">$tmp_hairpin_name.txt" or die ("[-] Error: Can't open or create temp file $tmp_hairpin_name.txt\n");
print TEMPHP ">tr$ChrID"."H"."$count2 $ChrID:$seqstarpos..$seqpos $l3 $2 $3 $4\n";
print TEMPHP "$tmp_hairpin_fa\n";
close TEMPHP;
## RNAfold
system("RNAfold", "--noPS", "$tmp_hairpin_name.txt", "-o$tmp_hairpin_name.MFE.txt");
unlink "$tmp_hairpin_name.txt";
open TEMPHPMFE, "$tmp_hairpin_name.MFE.txt" or die ("[-] Error: Can't open temp MFE file $tmp_hairpin_name.MFE.txt\n");
my $tmpHPMFE=1;
while(my $tmpMFE = <TEMPHPMFE>){
# say $.;
if ($. == 3 ){
$tmpHPMFE=substr($tmpMFE,-8);
$tmpHPMFE=substr($tmpHPMFE,0,6);
# say $tmpHPMFE;
$tmpHPMFE=$tmpHPMFE+0.0;
# say $tmpHPMFE;
}else{
next;
}
}
close TEMPHPMFE;
unlink "$tmp_hairpin_name.MFE.txt";
##### End of Calculate MFE
# say $tmpHPMFE;
# say $MFET;
if ($tmpHPMFE <= $MFET){
print RSUT ">tr$ChrID"."H"."$count2 ";
print RSUT "$ChrID:$seqstarpos..$seqpos $l3 $2 $3 $4 $tmpHPMFE\n";
print RSUT "$1\n";
# print RSUT "-----------------------------------------\n";
print FASTARESULT ">tr$ChrID"."H$count2.up $ChrID:$seqstarpos..$seqpos\n";
our $trseqpos = pos($seqall)-5;
our $rightfinalseq="";
if($trseqpos<$upstreml){
$rightfinalseq = substr($seqall,1,$trseqpos);
}else{
$rightfinalseq = substr($seqall,$trseqpos-$upstreml,$upstreml);
}
print FASTARESULT "$rightfinalseq\n";
if ($seqpos<$downstreml) {
print DOWNFA ">tr$ChrID"."H$count2.down $ChrID:$seqpos..1\n";
}else {
my $temppos=$seqpos-$downstreml;
print DOWNFA ">tr$ChrID"."H$count2.down $ChrID:$seqpos..$temppos\n";
}
#print DOWNFA ">tr$ChrID"."H$count2.down $ChrID:$seqpos..$seqpos-$downstreml\n";
our $downfasta = substr($seqall,$trseqpos,$downstreml);
print DOWNFA "$downfasta\n";
print OUTGFF "$ChrID\tEAhelitron\tCDS\t$seqpos\t$seqstarpos\t.\t-\t.\tName=$trHID.3;ID=$trHID;Parent=$trHID;Type=Helitron;Super_Family=Helitron;MFE=$tmpHPMFE\n";
print OUTBED "$ChrID\t".($seqpos-1)."\t".($seqstarpos)."\t$trHID\t1\t-\n";
our $TCi=0;
#say our $TCseq='([atgcn]{10}(TC(TCTACTA|T.TACTA.T|T.TACTAC|.{2}TACTACT|T.TAC.ACT|T.TA.TACT|T.TACT.CT|T.T.CTACT|.CTACTA.T|.{9}TATTAAG))[atgcn]{15})';
our $rfsl=length($rightfinalseq);
while($rightfinalseq=~/$TCseq/igc){
$TCi++;
my $TCL=length($1)-25;
my $TCpos=pos($rightfinalseq)-20;
my $TCposstar=$TCpos-$TCL+1;
my $TCgpos=$seqpos+$rfsl-$TCpos;
my $TCgposstar=$seqpos+$rfsl-$TCposstar;
print TCRSUT ">$trHID.5.$TCi $ChrID:$TCgposstar..$TCgpos $2\n";
print TCRSUT "$1\n";
print OUTGFF "$ChrID\tEAhelitron\tCDS\t$TCgpos\t$TCgposstar\t.\t-\t.\tName=$trHID.5.$TCi;ID=$trHID.5.$TCi;Parent=$trHID;Type=Helitron;Super_Family=Helitron;MFE=$tmpHPMFE\n";
print TCFARESULT ">$trHID.5.$TCi $ChrID:$TCgposstar..$seqpos\n";
my $FullHseq=substr($rightfinalseq,$TCposstar-1,$upstreml);
print TCFARESULT "$FullHseq\n";
}
$count2++; ## 2023.10.05 move to the last step
}
}
################ TR-seqs End#############################
$count2=$count2-1; ## 2023.10.05 move to the last step
$count1=$count1-1; ## 2023.10.05 move to the last step
print GENOMELENH "$Chrname[$ni]\t$seqalll\t$count1\t$count2\t".($count1 + $count2)."\t".($count1/$seqalll)*1e6."\t".($count2/$seqalll)*1e6."\t".($count1+$count2)/$seqalll*1e6."\n";
print "$Chrname[$ni]\t$seqalll\t$count1\t$count2\t".($count1 + $count2)."\t".($count1/$seqalll)*1e6."\t".($count2/$seqalll)*1e6."\t".($count1+$count2)/$seqalll*1e6."\n";
# print "$Chrname[$ni]\t$seqalll\t$count1\t$count2\t".($count1 + $count2)."\t".$count1/$seqalll."\t".$count2/$seqalll."\t".($count1+$count2)/$seqalll."\n";
# print "$Chrname[$ni]$seqalll\t[+]:$count1\t [-]:$count2\tall:".$count1+$count2."\n";
$countall = $countall + $count1 + $count2;
$genomel += $seqalll;
$count1all += $count1;
$count2all += $count2;
}
print GENOMELENH "Whole_genome\t$genomel\t$count1all\t$count2all\t$countall\t".$count1all/$genomel*1e6."\t".$count2all/$genomel*1e6."\t".$countall/$genomel*1e6."\n";
close RSUT;
close TCRSUT;
close FASTARESULT;
close TCFARESULT;
close OUTGFF;
close DOWNFA;
close GENOMELENH;
close OUTBED;
######################################
#End main
######################################
our $endtime=time();
#say $starttime;
#say $endtime;
printf "\nDone! %g Sec %g Min\n",$endtime-$starttime,($endtime-$starttime)/60;