-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMITP.pl
2946 lines (2726 loc) · 110 KB
/
MITP.pl
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
use strict;
use warnings;
no warnings qw(uninitialized);
use Cwd 'abs_path';
use Getopt::Long;
use File::Basename;
#Variable setup
my %opt;
use vars qw/%opt/;
my $version="1.1.0 version";
my $program=$0;
$program=abs_path $program;
my @programpath = split /\//, $program;
my $programname = pop @programpath;
my $programdir = join '/', @programpath;
my $usage=<<USAGE; #******* Instruction of this program *********#
Author: Wanfei Liu & Chengqi Xin
Email: <liuwf\@big.ac.cn> & <xinchq\@big.ac.cn>
Date: Jun 18, 2013
Version: $version
Introduction: miRNA is a widely known small non-coding RNA which can mediate gene regulation of most important biological processes in plants and animals. Therefore, identification conserve and novel miRNA and their target genes in model and new sequenced species are inevitable. However, the associated tools are often inconvenient, multi-step and difficult to use, especially for biologists who are short for bioinformatics knowledge. MITP is designed to identify miRNA easily and faster based on sequence mapping result from any mapping software which producing sam format output result, blast result (default output result) or blat result (default output result). The program provide a step praramter (8 steps) which can allow running program from any step and finishing all remaining steps. You also can run step by step using each step program. Please run these step programs at the same directory for running main program MITP.pl. Some steps are optional (filter, expression, class and target). When you select the related parameters belong to these optional steps, the program will run these steps. Otherwise, it skip these steps.
Need to Note: for identification miRNA, the parameter for blast and blat should change comparison to long sequence mapping. In our opinion, the blast command should be as \"blastall -p blastn -d database -i query -v 1000 -b 1000 -W 7 -o outfile\" and the blat command should be as \"blat database query -tileSize=8 -oneOff=1 -minMatch=1 -minIdentity=80 -noHead outfile\".
Usage: perl $program --step <*> { --sam <*> | --blast <*> | --blat <*> } --genome|-g <*.fa>
The following options are necessary.
--step :This parameter allow you run program from any step. The details about each step was explained bellow.
cluster Run all steps from cluster. In cluster step, the program will cluster reads to obtain candidate regions for downsteam process. If you only want to run this step, please run cluster.pl.
filter Run remaining steps from filter. In filter step, the program will filter all regions in filter file (gff2 format) from candiate regions and calculate expression for filtered regions (adding a new attribute EXPRESS=\"Read_number\" at the end of gff2 file named as *.gff.read). The filter file can including any non-intron region from mRNA, rRNA, tRNA, snoRNA and even known miRNA. Please do not include intron region in this file. Otherwise, it will remove all reads in introns. This step is optional. If you only want to run this step, please run filter.pl.
extract_seq Run remaining steps from extract_seq. In this step, the program will extract candidate sequence and create *.gff file for candidate sequence. If you only want to run this step, please run extract_seq.pl.
candidate Run remaining steps from candidate. In this step, the program can obtain candidate miRNA.
candidate_filter Run remaining steps from candidate_filter. In this step, the program will filter hairpin sequences according to their attribute values and create the final sequence and gff files for candidate miRNA.
expression Run remaining steps from expression. In this step, the program will calculate the expression for candidate miRNA. If you only want to run this step, please run expression.pl.
class Run remaining steps from class. In this step, the program will extract conserve miRNA from candidate miRNA. If you only want to run this step, please run class.pl.
target Run the last step which is target. In this step, the program will obtain target genes for candidate miRNA. If you only want to run this step, please run target.pl.
--sam <*> :mapping result file in sam format
--blast <*> :mapping result file in blast default output format
--blat <*> :mapping result file in blat default output format
--genome|-g <*.fa> :genome sequence file in fasta format
The following options are optional.
--output_dir|-o <output_dir> :default is sample under the current directory, you should change it if you have more than one sample to distinguish different samples
--prefix|-p <prefix=MI> :prefix for file and miRNA ID, if you want to compare different samples, you should use different prefix, the prefix should be end with letters, default is MI
--strand_specific|-ss :if you assign this parameter, it means the data is strand specific, if not, as default, it means the data is not strand specific
--maxmap|-mm <maxmap=10> :maximum mapping position for each sequence, default is 10
--mismatch|-m <mismatch=3> :maximum mismatch value for mapping result (including indels), default is 3
--identity|-i <identity=90> :minimum identity percent(%) (in mapped regions), default is 90
--minimum|-min <minimum=18> :minimum miRNA length, default is 18
--maximum|-max <maximum=25> :maximum miRNA length, default is 25
--alignsoft|-as <blast|blat> :in default, blast was used.
--help|-h :print the usage information
These parameters belong to cluster step.
--oversize|-os <oversize=16> :minimum overlap size for read cluster, default is 16
--overrate|-or <overrate=80> :minimum overlap rate percent(%) for read cluster, default is 80
These parameters belong to filter step (This step is optional, it will run only when you assign --filter_gff|-fg or --filter_fa parameter). You can provide a gff file (--filter_gff|-fg) or a fasta file (--filter_fa|-fa) for filter.
--filter_gff|-fg <*.gff> :the filter record file in gff2 format, all clusters overlap with these records will be removed
--filter_fa|-ff <*.fa> :the filter record file in fasta format, all clusters mapped to these sequences will be removed
--filter_rate|-fr <filter_rate=50> :minimum filter overlap rate percent(%) between filter region and read cluster, default is 50
These parameters belong to extract_seq step.
--mincov|-mc <mincov=10> :minimum miRNA mapping coverage, default is 10 (for conserve miRNA, it should be 1, for novel miRNA from expression, it should be minimum expressed read number)
These parameters belong to candidate step.
--minbasepair|-mbp <minbasepair=16> :minimum base-pairs between mature and star of miRNA comparison, default is 16
--maxbasebulge|-mbb <maxbasebulge=3> :maximum base bulge in mature and star miRNA comparison, default is 3
--maxunpairbase|-mub <maxunpairbase=6> :maximum unpair base number in mature or star miRNA region, default is 6
--matoverrate|-mor <matoverrate=60> :minimum redundant mature sequence overlap rate percent(%), default is 60
These parameters belong to candidate_filter step. You can filter candidate miRNA according to the candidate sequence and structure attribute values. In default, it include the bellowing parameters.
--mfei <mfei=-0.85> :the mfei means the minimal folding free energy index (MFEI). In default, keep miRNA which mfei value is equal or lower than -0.85
--mfe <mfe=-25> :the mfe means the minimal folding free energy (MFE). In default, keep miRNA which mfe value is equal or lower than -25
--hairlen <len=50> :in default, keep miRNA which hairpin length is equal or larger than 50
we also provide other filter parameters if you want to further filter candidate miRNA. In default, these parameters are not used
--pairbase :minimum pair base number
--pairpercent :minimum pair base percent(%)
--amfe :the amfe means adjusted mfe (AMFE) represented the mfe of 100 nucleotides. You can set the maximum amfe value
--minapercent & --maxapercent :minimum and maximum A base percent(%)
--minupercent & --maxapercent :minimum and maximum U base percent(%)
--mingpercent & --maxapercent :minimum and maximum G base percent(%)
--mincpercent & --maxapercent :minimum and maximum C base percent(%)
--minaupercent & --maxapercent :minimum and maximum AU base percent(%)
--mingcpercent & --maxapercent :minimum and maximum GC base percent(%)
we can do self alignment for candidate miRNA to filter candidate miRNA with same mature and hairpin sequence, but located in different genome region.
--selfalign|-sa :if you assign this parameter, it means the program will filter candidate miRNA according to self alignment for mature and hairpin sequence, as default, does not do anything.
--selfidentity|-si <selfidentity=100> :minimum identity percent(%) for filter self alignment result, default is 100
--selfoverrate|-sor <overrate=80> :minimum overlap rate percent(%) for read cluster, default is 80
we can draw second structure for candidate miRNA.
--figure <yes|no> :default is yes, the program will produce second structure figure in pdf format for all candidate miRNA in a subdirectory named $opt{prefix}\_figure at output directory.
These parameters belong to expression step. (This step is optional, it will run only when you assign --expression|-e parameter).
--expression|-e :if you assign this parameter, it means the program will calculate the expression for candidate miRNA, if not, as default, it means the program will not calculate the expression for candidate miRNA
These parameters belong to class step. (This step is optional, it will run only when you assign --conserveseq|-cs parameter).
--conserveseq|-cs <*.fa> :The mature sequence of conserve miRNA. If you assign this parameter, the program will extract conserve miRNA from candidate miRNA. This file format must like the mature sequence file in miRBase
--triletterabbr|-tla <triletterabbr=new> :three letter abbreviation for studied species, it used to compare miRNA conservation in multiple species, default is new
These parameters belong to target step. (This step is optional, it will run only when you assign --target_seq|-ts parameter).
--target_seq|-ts <*.fa> :The target sequence file. If you assign this parameter, the program will do target prediction for candidate miRNA.
--target_tool|-tt <*> :The program for target prediction (blast, blat, targetfinder, miranda and RNAhybrid). In default, it will run using the program assigned by --alignsoft|-as parameter. For plant, you can identify target genes by our rule set according to blast or blat alignment (in default) or by targetfinder; for animal, you can use miranda or RNAhybrid.
--dataset|-ds <dataset=3utr_fly|3utr_worm|3utr_human> :three data set name in RNAhybird program for target prediction, in default is 3utr_human. This parameter is only need when you use RNAhybrid to predict target
Note: for conserve miRNA identification, --mincov|-mc should be 1; for novel miRNA identification, --mincov|-mc should be the minimum read number for expression. For --filter_gff|-fg or --filter_fa|-ff parameter, please only inculde region or sequence you want to removed (for example, do not include intron in animal).
USAGE
#Gather input
&GetOptions(
"step=s" =>\$opt{step},
"sam=s" =>\$opt{sam},
"blast=s" =>\$opt{blast},
"blat=s" =>\$opt{blat},
"genome|g=s" =>\$opt{genome},
"output_dir|o=s" =>\$opt{output_dir},
"prefix|p=s" =>\$opt{prefix},
"strand_specific|ss"=>\$opt{strand_specific},
"maxmap|mm=i" =>\$opt{maxmap},
"mismatch|m=i" =>\$opt{mismatch},
"identity|i=i" =>\$opt{identity},
"minimum|min=i" =>\$opt{minimum},
"maximum|max=i" =>\$opt{maximum},
"alignsoft|as=s" =>\$opt{alignsoft},
"oversize|os=i" =>\$opt{oversize},
"overrate|or=i" =>\$opt{overrate},
"filter_gff|fg=s" =>\$opt{filter_gff},
"filter_fa|ff=s" =>\$opt{filter_fa},
"filter_rate|fr=i" =>\$opt{filter_rate},
"mincov|mc=s" =>\$opt{mincov},
"minbasepair|mbp=i" =>\$opt{minbasepair},
"maxbasebulge|mbb=i"=>\$opt{maxbasebulge},
"maxunpairbase|mub=i"=>\$opt{maxunpairbase},
"matoverrate|mor=i" =>\$opt{matoverrate},
"mfei=i" =>\$opt{mfei},
"mfe=i" =>\$opt{mfe},
"hairlen=i" =>\$opt{hairlen},
"pairbase=i" =>\$opt{pairbase},
"pairpercent=i" =>\$opt{pairpercent},
"amfe=i" =>\$opt{amfe},
"minapercent=i" =>\$opt{minapercent},
"maxapercent=i" =>\$opt{maxapercent},
"minupercent=i" =>\$opt{minupercent},
"maxupercent=i" =>\$opt{maxupercent},
"mingpercent=i" =>\$opt{mingpercent},
"maxgpercent=i" =>\$opt{maxgpercent},
"mincpercent=i" =>\$opt{mincpercent},
"maxcpercent=i" =>\$opt{maxcpercent},
"minaupercent=i" =>\$opt{minaupercent},
"maxaupercent=i" =>\$opt{maxaupercent},
"mingcpercent=i" =>\$opt{mingcpercent},
"maxgcpercent=i" =>\$opt{maxgcpercent},
"selfalign|sa" =>\$opt{selfalign},
"selfidentity|si=i" =>\$opt{selfidentity},
"selfoverrate|sor=i"=>\$opt{selfoverrate},
"figure=s" =>\$opt{figure},
"expression|e" =>\$opt{expression},
"conserveseq|cs=s" =>\$opt{conserveseq},
"triletterabbr|tla=s" =>\$opt{triletterabbr},
"target_seq|ts=s" =>\$opt{target_seq},
"target_tool|tt=s" =>\$opt{target_tool},
"dataset|ds=s" =>\$opt{dataset},
"help|h" =>\$opt{help},
);
#Verify input
if ((!defined $opt{sam} and !defined $opt{blast} and !defined $opt{blat}) or !defined $opt{genome} or defined $opt{help}) {
die "$usage\n";
}
#Default parameters
$opt{step} ="cluster" unless defined $opt{step};
$opt{prefix} = "MI" unless defined $opt{prefix};
$opt{maxmap} = 10 unless defined $opt{maxmap};
$opt{mismatch} = 3 unless defined $opt{mismatch};
$opt{identity} = 90 unless defined $opt{identity};
$opt{minimum} = 18 unless defined $opt{minimum};
$opt{maximum} = 25 unless defined $opt{maximum};
$opt{alignsoft} = "blast" unless defined $opt{alignsoft};
$opt{oversize} = 16 unless defined $opt{oversize};
$opt{overrate} = 80 unless defined $opt{overrate};
$opt{filter_rate} = 50 unless defined $opt{filter_rate};
$opt{mincov} = 10 unless defined $opt{mincov};
$opt{minbasepair} = 16 unless defined $opt{minbasepair};
$opt{maxbasebulge} = 3 unless defined $opt{maxbasebulge};
$opt{maxunpairbase} = 6 unless defined $opt{maxunpairbase};
$opt{matoverrate} = 60 unless defined $opt{matoverrate};
$opt{mfei} = -0.85 unless defined $opt{mfei};
$opt{mfe} = -25 unless defined $opt{mfe};
$opt{hairlen} = 50 unless defined $opt{hairlen};
$opt{selfidentity} = 100 unless defined $opt{selfidentity};
$opt{selfoverrate} = 80 unless defined $opt{selfoverrate};
$opt{figure} = "yes" unless defined $opt{figure};
$opt{triletterabbr} = "new" unless defined $opt{triletterabbr};
$opt{target_tool} = $opt{alignsoft} unless defined $opt{target_tool};
$opt{dataset} = "3utr_human" unless defined $opt{dataset};
# Absolute path
$opt{sam} = abs_path $opt{sam} if defined $opt{sam};
$opt{blast} = abs_path $opt{blast} if defined $opt{blast};
$opt{blat} = abs_path $opt{blat} if defined $opt{blat};
$opt{genome} = abs_path $opt{genome};
$opt{filter_gff} = abs_path $opt{filter_gff} if defined $opt{filter_gff};
$opt{filter_fa} = abs_path $opt{filter_fa} if defined $opt{filter_fa};
$opt{conserveseq} = abs_path $opt{conserveseq} if defined $opt{conserveseq};
$opt{target_seq} = abs_path $opt{target_seq} if defined $opt{target_seq};
#Output directory
$opt{output_dir} = $ENV{'PWD'}."/sample" unless defined $opt{output_dir};
$opt{output_dir} = abs_path $opt{output_dir} if defined $opt{output_dir};
`mkdir -p $opt{output_dir}` unless -d $opt{output_dir};
chdir $opt{output_dir};
#Build commond line
my $cmdline = $program;
$cmdline .= " --step $opt{step}" if defined $opt{step};
$cmdline .= " --sam $opt{sam}" if defined $opt{sam};
$cmdline .= " --blast $opt{blast}" if defined $opt{blast};
$cmdline .= " --blat $opt{blat}" if defined $opt{blat};
$cmdline .= " -g $opt{genome}" if defined $opt{genome};
$cmdline .= " -o $opt{output_dir}" if defined $opt{output_dir};
$cmdline .= " -p $opt{prefix}" if defined $opt{prefix};
$cmdline .= " -ss $opt{strand_specific}" if defined $opt{strand_specific};
$cmdline .= " -mm $opt{maxmap}" if defined $opt{maxmap};
$cmdline .= " -m $opt{mismatch}" if defined $opt{mismatch};
$cmdline .= " -i $opt{identity}" if defined $opt{identity};
$cmdline .= " -min $opt{minimum}" if defined $opt{minimum};
$cmdline .= " -max $opt{maximum}" if defined $opt{maximum};
$cmdline .= " -as $opt{alignsoft}" if defined $opt{alignsoft};
$cmdline .= " -os $opt{oversize}" if defined $opt{oversize};
$cmdline .= " -or $opt{overrate}" if defined $opt{overrate};
$cmdline .= " -fg $opt{filter_gff}" if defined $opt{filter_gff};
$cmdline .= " -ff $opt{filter_fa}" if defined $opt{filter_fa};
$cmdline .= " -fr $opt{filter_rate}" if defined $opt{filter_rate};
$cmdline .= " -mc $opt{mincov}" if defined $opt{mincov};
$cmdline .= " -mbp $opt{minbasepair}" if defined $opt{minbasepair};
$cmdline .= " -mbb $opt{maxbasebulge}" if defined $opt{maxbasebulge};
$cmdline .= " -mub $opt{maxunpairbase}" if defined $opt{maxunpairbase};
$cmdline .= " -mor $opt{matoverrate}" if defined $opt{matoverrate};
$cmdline .= " --mfei $opt{mfei}" if defined $opt{mfei};
$cmdline .= " --mfe $opt{mfe}" if defined $opt{mfe};
$cmdline .= " --hairlen $opt{hairlen}" if defined $opt{hairlen};
$cmdline .= " --pairbase $opt{pairbase}" if defined $opt{pairbase};
$cmdline .= " --pairpercent $opt{pairpercent}" if defined $opt{pairpercent};
$cmdline .= " --amfe $opt{amfe}" if defined $opt{amfe};
$cmdline .= " --minapercent $opt{minapercent}" if defined $opt{minapercent};
$cmdline .= " --maxapercent $opt{maxapercent}" if defined $opt{maxapercent};
$cmdline .= " --minupercent $opt{minupercent}" if defined $opt{minupercent};
$cmdline .= " --maxupercent $opt{maxupercent}" if defined $opt{maxupercent};
$cmdline .= " --mingpercent $opt{mingpercent}" if defined $opt{mingpercent};
$cmdline .= " --maxgpercent $opt{maxgpercent}" if defined $opt{maxgpercent};
$cmdline .= " --mincpercent $opt{mincpercent}" if defined $opt{mincpercent};
$cmdline .= " --maxcpercent $opt{maxcpercent}" if defined $opt{maxcpercent};
$cmdline .= " --minaupercent $opt{minaupercent}" if defined $opt{minaupercent};
$cmdline .= " --maxaupercent $opt{maxaupercent}" if defined $opt{maxaupercent};
$cmdline .= " --mingcpercent $opt{mingcpercent}" if defined $opt{mingcpercent};
$cmdline .= " --maxgcpercent $opt{maxgcpercent}" if defined $opt{maxgcpercent};
$cmdline .= " -sa $opt{selfalign}" if defined $opt{selfalign};
$cmdline .= " -si $opt{selfidentity}" if defined $opt{selfidentity};
$cmdline .= " -sor $opt{selfoverrate}" if defined $opt{selfoverrate};
$cmdline .= " --figure $opt{figure}" if defined $opt{figure};
$cmdline .= " -e $opt{expression}" if defined $opt{expression};
$cmdline .= " -cs $opt{conserveseq}" if defined $opt{conserveseq};
$cmdline .= " -tla $opt{triletterabbr}" if defined $opt{triletterabbr};
$cmdline .= " -ts $opt{target_seq}" if defined $opt{target_seq};
$cmdline .= " -tt $opt{target_tool}" if defined $opt{target_tool};
$cmdline .= " -ds $opt{dataset}" if defined $opt{dataset};
open LOG, ">log" or die "Cannot write to log file\n";
print LOG `date`, "\n>>> $program <<<\n\n";
print LOG $cmdline,"\n\n";
my ($time,$chr,$string,%gen,%len,%id,%hash,);
if ($opt{step} eq "cluster" or $opt{step} eq "extract_seq" or defined $opt{filter_fa}) {
#Obtain chromosome sequence and length
print LOG "Obtain chromosome sequence and length.\n";
open (IN,"<$opt{genome}")||die("Cannot read $opt{genome}.\n");
while (<IN>) {
chomp;
if (/^\#/) {
next;
}elsif (/^>(\S+)/) {
if (defined $chr) {
$gen{$chr}=$string;
$len{$chr}=length $string;
$string=undef;
}
$chr=$1;
next;
}
$string.=$_;
}
$gen{$chr}=$string;
$len{$chr}=length $string;
$string=undef;
close IN;
$time=localtime;
print LOG "Chromosome sequence and length are obtained: $time.\n\n";
}
%hash=();
if ($opt{step} eq "cluster") {
#Read the mapping result file
print LOG "Read the mapping result file.\n";
if (defined $opt{blast}) {
system "perl $programdir/EblastN.pl -i $opt{blast} -o $opt{prefix}.eblastn";
system "perl $programdir/eblastn2sam.pl -e $opt{prefix}.eblastn -o $opt{prefix}.sam -s $opt{prefix}.statistics -m $opt{mismatch} -i $opt{identity} -min $opt{minimum} -max $opt{maximum} -mm $opt{maxmap}";
open (IN,"<$opt{prefix}.sam")||die("Cannot read $opt{prefix}.sam.\n");
while (<IN>) {
chomp;
#SRR027895.5259200_1 99 chr1 15443 255 76M = 15623 256 CTCCAGAGGCCTCAGGTCCAGTCTCTAAAAATATCTCAGGAAGCTGCAGTGGCTGACCATTGCCTTGGACCGCTCT BCBCCBBABCBBBBBB=BBAB?BBBBBAABBBBBBBBBBA@BBBABBBB?@AAA>?<702>7:;1:+670:9;<== NM:i:1 NH:i:1
next if(/^\@/);
my @list=split /\t/,$_;
next if ($list[2] eq "*");
my $strand=&flag($list[1]);
my $chr=$list[2];
my $start=$list[3];
my @match=split /([M|I|D|S|H]+)/,$list[5];
my $mis=0;#include indel
my $mis2=0;#in mapped read
my $len=0;#in reference
my $len2=0;#in mapped read
for (my $i=0;$i<@match-1;$i+=2) {
if ($match[$i+1] eq "M") {
$len+=$match[$i];
$len2+=$match[$i];
}elsif ($match[$i+1] eq "D") {
$len+=$match[$i];
$mis+=$match[$i];
}elsif ($match[$i+1] eq "I") {
$mis+=$match[$i];
}
}
if ($_=~/NM\:i\:(\d+)/) {
$mis+=$1;
$mis2=$1;
}
my $end=$start+$len-1;
if (defined $opt{strand_specific}) {
if (!exists $hash{$chr}{$strand}{$start."\t".$end}) {
$hash{$chr}{$strand}{$start."\t".$end}[0]=$len-$mis;
$hash{$chr}{$strand}{$start."\t".$end}[1]++;
$hash{$chr}{$strand}{$start."\t".$end}[2]=$start;
$hash{$chr}{$strand}{$start."\t".$end}[3]++;
}else {
$hash{$chr}{$strand}{$start."\t".$end}[1]++;
$hash{$chr}{$strand}{$start."\t".$end}[2]=$start;
$hash{$chr}{$strand}{$start."\t".$end}[3]++;
if ($len-$mis>$hash{$chr}{$strand}{$start."\t".$end}[0]) {
$hash{$chr}{$strand}{$start."\t".$end}[0]=$len-$mis;
}
}
}else {
if (!exists $hash{$chr}{$start."\t".$end}) {
$hash{$chr}{$start."\t".$end}[0]=$len-$mis;
$hash{$chr}{$start."\t".$end}[1]++;
$hash{$chr}{$start."\t".$end}[2]=$start;
$hash{$chr}{$start."\t".$end}[3]++;
}else {
$hash{$chr}{$start."\t".$end}[1]++;
$hash{$chr}{$start."\t".$end}[2]=$start;
$hash{$chr}{$start."\t".$end}[3]++;
if ($len-$mis>$hash{$chr}{$start."\t".$end}[0]) {
$hash{$chr}{$start."\t".$end}[0]=$len-$mis;
}
}
}
}
close IN;
}elsif (defined $opt{blat}) {
system "perl $programdir/blat2sam.pl -b $opt{blat} -o $opt{prefix}.sam -s $opt{prefix}.statistics -m $opt{mismatch} -i $opt{identity} -min $opt{minimum} -max $opt{maximum} -mm $opt{maxmap}";
open (IN,"<$opt{prefix}.sam")||die("Cannot read $opt{prefix}.sam.\n");
while (<IN>) {
chomp;
#SRR027895.5259200_1 99 chr1 15443 255 76M = 15623 256 CTCCAGAGGCCTCAGGTCCAGTCTCTAAAAATATCTCAGGAAGCTGCAGTGGCTGACCATTGCCTTGGACCGCTCT BCBCCBBABCBBBBBB=BBAB?BBBBBAABBBBBBBBBBA@BBBABBBB?@AAA>?<702>7:;1:+670:9;<== NM:i:1 NH:i:1
next if(/^\@/);
my @list=split /\t/,$_;
next if ($list[2] eq "*");
my $strand=&flag($list[1]);
my $chr=$list[2];
my $start=$list[3];
my @match=split /([M|I|D|S|H]+)/,$list[5];
my $mis=0;#include indel
my $mis2=0;#in mapped read
my $len=0;#in reference
my $len2=0;#in mapped read
for (my $i=0;$i<@match-1;$i+=2) {
if ($match[$i+1] eq "M") {
$len+=$match[$i];
$len2+=$match[$i];
}elsif ($match[$i+1] eq "D") {
$len+=$match[$i];
$mis+=$match[$i];
}elsif ($match[$i+1] eq "I") {
$mis+=$match[$i];
}
}
if ($_=~/NM\:i\:(\d+)/) {
$mis+=$1;
$mis2=$1;
}
my $end=$start+$len-1;
if (defined $opt{strand_specific}) {
if (!exists $hash{$chr}{$strand}{$start."\t".$end}) {
$hash{$chr}{$strand}{$start."\t".$end}[0]=$len-$mis;
$hash{$chr}{$strand}{$start."\t".$end}[1]++;
$hash{$chr}{$strand}{$start."\t".$end}[2]=$start;
$hash{$chr}{$strand}{$start."\t".$end}[3]++;
}else {
$hash{$chr}{$strand}{$start."\t".$end}[1]++;
$hash{$chr}{$strand}{$start."\t".$end}[2]=$start;
$hash{$chr}{$strand}{$start."\t".$end}[3]++;
if ($len-$mis>$hash{$chr}{$strand}{$start."\t".$end}[0]) {
$hash{$chr}{$strand}{$start."\t".$end}[0]=$len-$mis;
}
}
}else {
if (!exists $hash{$chr}{$start."\t".$end}) {
$hash{$chr}{$start."\t".$end}[0]=$len-$mis;
$hash{$chr}{$start."\t".$end}[1]++;
$hash{$chr}{$start."\t".$end}[2]=$start;
$hash{$chr}{$start."\t".$end}[3]++;
}else {
$hash{$chr}{$start."\t".$end}[1]++;
$hash{$chr}{$start."\t".$end}[2]=$start;
$hash{$chr}{$start."\t".$end}[3]++;
if ($len-$mis>$hash{$chr}{$start."\t".$end}[0]) {
$hash{$chr}{$start."\t".$end}[0]=$len-$mis;
}
}
}
}
close IN;
}elsif (defined $opt{sam}) {
open (STAT,">>$opt{prefix}.statistics")||die("Cannot write to $opt{prefix}.statistics.\n");
open (IN,"<$opt{sam}")||die("Cannot read $opt{sam}.\n");
%id=();
while (<IN>) {
chomp;
#SRR027895.5259200_1 99 chr1 15443 255 76M = 15623 256 CTCCAGAGGCCTCAGGTCCAGTCTCTAAAAATATCTCAGGAAGCTGCAGTGGCTGACCATTGCCTTGGACCGCTCT BCBCCBBABCBBBBBB=BBAB?BBBBBAABBBBBBBBBBA@BBBABBBB?@AAA>?<702>7:;1:+670:9;<== NM:i:1 NH:i:1
next if(/^\@/);
my @list=split /\t/,$_;
next if ($list[2] eq "*");
next if ($list[5]=~/S|H/);
if (!defined $id{$list[0]}) {
$id{$list[0]}=0;
}else {
if ($id{$list[0]}>=$opt{maxmap}) {
next;
}
}
my $strand=&flag($list[1]);
my $chr=$list[2];
my $start=$list[3];
my @match=split /([M|I|D|S|H]+)/,$list[5];
my $mis=0;#include indel
my $mis2=0;#in mapped read
my $len=0;#in reference
my $len2=0;#in mapped read
for (my $i=0;$i<@match-1;$i+=2) {
if ($match[$i+1] eq "M") {
$len+=$match[$i];
$len2+=$match[$i];
}elsif ($match[$i+1] eq "D") {
$len+=$match[$i];
$mis+=$match[$i];
}elsif ($match[$i+1] eq "I") {
$mis+=$match[$i];
}
}
if ($_=~/NM\:i\:(\d+)/) {
$mis+=$1;
$mis2=$1;
}
my $end=$start+$len-1;
my $identity=($len2-$mis2)/$len2*100;
if ($len>=$opt{minimum} and $len<=$opt{maximum} and $identity>=$opt{identity} and $mis<=$opt{mismatch}) {
$id{$list[0]}++;
if (defined $opt{strand_specific}) {
if (!exists $hash{$chr}{$strand}{$start."\t".$end}) {
$hash{$chr}{$strand}{$start."\t".$end}[0]=$len-$mis;
$hash{$chr}{$strand}{$start."\t".$end}[1]++;
$hash{$chr}{$strand}{$start."\t".$end}[2]=$start;
$hash{$chr}{$strand}{$start."\t".$end}[3]++;
}else {
$hash{$chr}{$strand}{$start."\t".$end}[1]++;
$hash{$chr}{$strand}{$start."\t".$end}[2]=$start;
$hash{$chr}{$strand}{$start."\t".$end}[3]++;
if ($len-$mis>$hash{$chr}{$strand}{$start."\t".$end}[0]) {
$hash{$chr}{$strand}{$start."\t".$end}[0]=$len-$mis;
}
}
}else {
if (!exists $hash{$chr}{$start."\t".$end}) {
$hash{$chr}{$start."\t".$end}[0]=$len-$mis;
$hash{$chr}{$start."\t".$end}[1]++;
$hash{$chr}{$start."\t".$end}[2]=$start;
$hash{$chr}{$start."\t".$end}[3]++;
}else {
$hash{$chr}{$start."\t".$end}[1]++;
$hash{$chr}{$start."\t".$end}[2]=$start;
$hash{$chr}{$start."\t".$end}[3]++;
if ($len-$mis>$hash{$chr}{$start."\t".$end}[0]) {
$hash{$chr}{$start."\t".$end}[0]=$len-$mis;
}
}
}
}
}
close IN;
my $total_read=0;
my $filtered_read=0;
foreach my $id (keys %id) {
if ($id{$id}>0) {
$filtered_read++;
}
$total_read++;
}
%id=();
print STAT "Total_read\t$total_read\nFiltered_read\t$filtered_read\n\n";
close STAT;
}
$time=localtime;
print LOG "The mapping result has been readed: $time.\n\n";
#Cluster mapping result
print LOG "Cluster mapping result.\n";
foreach my $chr (keys %hash) {
if (defined $opt{strand_specific}) {
foreach my $strand (keys %{$hash{$chr}}) {
my @record=sort {$hash{$chr}{$strand}{$a}[2]<=>$hash{$chr}{$strand}{$b}[2]} keys %{$hash{$chr}{$strand}};
for (my $i=0;$i<@record-1;$i++) {
my ($start1,$end1)=split /\t/,$record[$i];
my ($start2,$end2)=split /\t/,$record[$i+1];
if (($end1>=$start2 and $end1<=$end2) or ($end2>=$start1 and $end2<=$end1)) {
my $start=$start1;
my $end=$end1;
if ($start<$start2) {
$start=$start2;
}
if ($end>$end2) {
$end=$end2;
}
if ($end-$start+1>=$opt{oversize} and (($end-$start+1)/($end1-$start1+1)>=$opt{overrate}/100 or ($end-$start+1)/($end2-$start2+1)>=$opt{overrate}/100)) {
my $match1=$hash{$chr}{$strand}{$record[$i]}[0];
my $read1=$hash{$chr}{$strand}{$record[$i]}[1];
my $match2=$hash{$chr}{$strand}{$record[$i+1]}[0];
my $read2=$hash{$chr}{$strand}{$record[$i+1]}[1];
if ($read1>$read2) {
$hash{$chr}{$strand}{$record[$i]}[0]=$match1;
$hash{$chr}{$strand}{$record[$i]}[1]=$read1;
$hash{$chr}{$strand}{$record[$i]}[2]=$start1;
$hash{$chr}{$strand}{$record[$i]}[3]+=$hash{$chr}{$strand}{$record[$i+1]}[3];
@{$hash{$chr}{$strand}{$record[$i+1]}}=();
delete $hash{$chr}{$strand}{$record[$i+1]};
$record[$i+1]=$record[$i];
}elsif ($read1==$read2) {
if ($match1>=$match2) {
$hash{$chr}{$strand}{$record[$i]}[0]=$match1;
$hash{$chr}{$strand}{$record[$i]}[1]=$read1;
$hash{$chr}{$strand}{$record[$i]}[2]=$start1;
$hash{$chr}{$strand}{$record[$i]}[3]+=$hash{$chr}{$strand}{$record[$i+1]}[3];
@{$hash{$chr}{$strand}{$record[$i+1]}}=();
delete $hash{$chr}{$strand}{$record[$i+1]};
$record[$i+1]=$record[$i];
}else {
$hash{$chr}{$strand}{$record[$i+1]}[0]=$match2;
$hash{$chr}{$strand}{$record[$i+1]}[1]=$read2;
$hash{$chr}{$strand}{$record[$i+1]}[2]=$start2;
$hash{$chr}{$strand}{$record[$i+1]}[3]+=$hash{$chr}{$strand}{$record[$i]}[3];
@{$hash{$chr}{$strand}{$record[$i]}}=();
delete $hash{$chr}{$strand}{$record[$i]};
}
}else {
$hash{$chr}{$strand}{$record[$i+1]}[0]=$match2;
$hash{$chr}{$strand}{$record[$i+1]}[1]=$read2;
$hash{$chr}{$strand}{$record[$i+1]}[2]=$start2;
$hash{$chr}{$strand}{$record[$i+1]}[3]+=$hash{$chr}{$strand}{$record[$i]}[3];
@{$hash{$chr}{$strand}{$record[$i]}}=();
delete $hash{$chr}{$strand}{$record[$i]};
}
}
}
}
}
}else {
my @record=sort {$hash{$chr}{$a}[2]<=>$hash{$chr}{$b}[2]} keys %{$hash{$chr}};
for (my $i=0;$i<@record-1;$i++) {
my ($start1,$end1)=split /\t/,$record[$i];
my ($start2,$end2)=split /\t/,$record[$i+1];
if (($end1>=$start2 and $end1<=$end2) or ($end2>=$start1 and $end2<=$end1)) {
my $start=$start1;
my $end=$end1;
if ($start<$start2) {
$start=$start2;
}
if ($end>$end2) {
$end=$end2;
}
if ($end-$start+1>=$opt{oversize} and (($end-$start+1)/($end1-$start1+1)>=$opt{overrate}/100 or ($end-$start+1)/($end2-$start2+1)>=$opt{overrate}/100)) {
my $match1=$hash{$chr}{$record[$i]}[0];
my $read1=$hash{$chr}{$record[$i]}[1];
my $match2=$hash{$chr}{$record[$i+1]}[0];
my $read2=$hash{$chr}{$record[$i+1]}[1];
if ($read1>$read2) {
$hash{$chr}{$record[$i]}[0]=$match1;
$hash{$chr}{$record[$i]}[1]=$read1;
$hash{$chr}{$record[$i]}[2]=$start1;
$hash{$chr}{$record[$i]}[3]+=$hash{$chr}{$record[$i+1]}[3];
@{$hash{$chr}{$record[$i+1]}}=();
delete $hash{$chr}{$record[$i+1]};
$record[$i+1]=$record[$i];
}elsif ($read1==$read2) {
if ($match1>=$match2) {
$hash{$chr}{$record[$i]}[0]=$match1;
$hash{$chr}{$record[$i]}[1]=$read1;
$hash{$chr}{$record[$i]}[2]=$start1;
$hash{$chr}{$record[$i]}[3]+=$hash{$chr}{$record[$i+1]}[3];
@{$hash{$chr}{$record[$i+1]}}=();
delete $hash{$chr}{$record[$i+1]};
$record[$i+1]=$record[$i];
}else {
$hash{$chr}{$record[$i+1]}[0]=$match2;
$hash{$chr}{$record[$i+1]}[1]=$read2;
$hash{$chr}{$record[$i+1]}[2]=$start2;
$hash{$chr}{$record[$i+1]}[3]+=$hash{$chr}{$record[$i]}[3];
@{$hash{$chr}{$record[$i]}}=();
delete $hash{$chr}{$record[$i]};
}
}else {
$hash{$chr}{$record[$i+1]}[0]=$match2;
$hash{$chr}{$record[$i+1]}[1]=$read2;
$hash{$chr}{$record[$i+1]}[2]=$start2;
$hash{$chr}{$record[$i+1]}[3]+=$hash{$chr}{$record[$i]}[3];
@{$hash{$chr}{$record[$i]}}=();
delete $hash{$chr}{$record[$i]};
}
}
}
}
}
}
$time=localtime;
print LOG "The mapping result has been clustered: $time.\n\n";
#Print cluster result
print LOG "Print the clusters.\n";
open CLUSTER, ">$opt{prefix}\_read.cluster" or die "Cannot write to $opt{prefix}\_read.cluster file.\n";
print CLUSTER "#ID\tQLength\tQStart\tQend\tTStart\tTend\tLength\tScore\tE-value\tOverlap/Total\tIdentity\tSubject_Name\tRead_num\n";
my $id=1;
my $read_num=0;
foreach my $chr (keys %hash) {
if (defined $opt{strand_specific}) {
foreach my $strand (keys %{$hash{$chr}}) {
my @record=sort {$hash{$chr}{$strand}{$a}[2]<=>$hash{$chr}{$strand}{$b}[2]} keys %{$hash{$chr}{$strand}};
for (my $i=0;$i<@record;$i++) {
my ($start,$end)=split /\t/,$record[$i];
my $match=$hash{$chr}{$strand}{$record[$i]}[0];
my $read=$hash{$chr}{$strand}{$record[$i]}[3];
my $len=$end-$start+1;
my $identity=sprintf "%.0f",$match/$len*100;
if ($strand eq "-") {
print CLUSTER "$id\t$len\t1\t$len\t$end\t$start\t$len{$chr}\t\.\t\.\t$match\/$len\t$identity\t$chr\t$read\n";
}else {
print CLUSTER "$id\t$len\t1\t$len\t$start\t$end\t$len{$chr}\t\.\t\.\t$match\/$len\t$identity\t$chr\t$read\n";
}
$id++;
$read_num+=$read;
}
}
}else {
my @record=sort {$hash{$chr}{$a}[2]<=>$hash{$chr}{$b}[2]} keys %{$hash{$chr}};
for (my $i=0;$i<@record;$i++) {
my ($start,$end)=split /\t/,$record[$i];
my $match=$hash{$chr}{$record[$i]}[0];
my $read=$hash{$chr}{$record[$i]}[3];
my $len=$end-$start+1;
my $identity=sprintf "%.0f",$match/$len*100;
print CLUSTER "$id\t$len\t1\t$len\t$start\t$end\t$len{$chr}\t\.\t\.\t$match\/$len\t$identity\t$chr\t$read\n";
$id++;
$read_num+=$read;
}
}
}
close CLUSTER;
%hash=();
$id=$id-1;
open (STAT,">>$opt{prefix}.statistics")||die("Cannot write to $opt{prefix}.statistics.\n");
print STAT "Cluster_number\t$id\n";
print STAT "Cluster_read\t$read_num\n\n";
close STAT;
$time=localtime;
print LOG "The clusters have been printed: $time.\n\n";
}
%hash=();
if ($opt{step} eq "cluster" or $opt{step} eq "filter") {
if (defined $opt{filter_gff}) {
#Read $opt{prefix}_read.cluster file.\n";
print LOG "Read $opt{prefix}\_read.cluster file.\n";
open (IN,"<$opt{prefix}\_read.cluster")||die("Cannot read $opt{prefix}\_read.cluster.\n");
while (<IN>) {
chomp;
#aau-miR160 21 1 21 1137925 1137905 2539026 34.2 0.15 20/21 95 S000014
next if (/^\#/);
my @list=split /\t/,$_;
if (defined $opt{strand_specific}) {
if ($list[5] > $list[4]) {
$hash{$list[11]}{"+"}{"$list[4]\t$list[5]"}[0]=$list[4];
$hash{$list[11]}{"+"}{"$list[4]\t$list[5]"}[1]=$list[12];
$hash{$list[11]}{"+"}{"$list[4]\t$list[5]"}[2]=$_;
}else {
$hash{$list[11]}{"-"}{"$list[5]\t$list[4]"}[0]=$list[5];
$hash{$list[11]}{"-"}{"$list[5]\t$list[4]"}[1]=$list[12];
$hash{$list[11]}{"-"}{"$list[5]\t$list[4]"}[2]=$_;
}
}else {
if ($list[5] > $list[4]) {
$hash{$list[11]}{"$list[4]\t$list[5]"}[0]=$list[4];
$hash{$list[11]}{"$list[4]\t$list[5]"}[1]=$list[12];
$hash{$list[11]}{"$list[4]\t$list[5]"}[2]=$_;
}else {
$hash{$list[11]}{"$list[5]\t$list[4]"}[0]=$list[5];
$hash{$list[11]}{"$list[5]\t$list[4]"}[1]=$list[12];
$hash{$list[11]}{"$list[5]\t$list[4]"}[2]=$_;
}
}
}
close IN;
$time=localtime;
print LOG "The $opt{prefix}_read.cluster file has been readed: $time.\n\n";
#Filter clusters which belong to regions in filter file (gff2 format)
print LOG "Filter clusters which belong to regions in filter file.\n";
my %filter=();
my $connector=undef;
open (IN,"<$opt{filter_gff}")||die("Cannot read $opt{filter_gff}.\n");
while (<IN>) {
chomp;
#S091595 . miRNA 194 211 . - . ACC="MI1"; ID="ath-miR5021";
my @list=split /\t/,$_;
if (!defined $connector) {
if ($list[8]=~/\=/) {
$connector="=";
}else {
$connector=" ";
}
}
$filter{$list[0]}{$list[6]}{"$list[3]\t$list[4]"}[0]=$list[3];
$filter{$list[0]}{$list[6]}{"$list[3]\t$list[4]"}[1]=$_;
}
close IN;
my $out=basename($opt{filter_gff}).".read";
open FILTER, ">$out" or die "Cannot write to $out file.\n";
foreach my $chr (keys %filter) {
foreach my $strand (keys %{$filter{$chr}}) {
my @filter=sort {$filter{$chr}{$strand}{$a}[0]<=>$filter{$chr}{$strand}{$b}[0]} keys %{$filter{$chr}{$strand}};
my @hash=();
if (defined $opt{strand_specific}) {
@hash=sort {$hash{$chr}{$strand}{$a}[0]<=>$hash{$chr}{$strand}{$b}[0]} keys %{$hash{$chr}{$strand}};
}else {
@hash=sort {$hash{$chr}{$a}[0]<=>$hash{$chr}{$b}[0]} keys %{$hash{$chr}};
}
my $h=0;
my $min_overlap_h=undef;
my ($hstart,$hend)=split /\t/,$hash[$h];
for (my $f=0;$f<@filter;$f++) {
my ($fstart,$fend)=split /\t/,$filter[$f];
my $express=0;
FCOM:
if ($hend<$fstart) {
$h++;
if (defined $hash[$h]) {
($hstart,$hend)=split /\t/,$hash[$h];
goto FCOM;
}else {
print FILTER "$filter{$chr}{$strand}{$filter[$f]}[1] EXPRESS$connector\"$express\"\n";
next;
}
}elsif (($hend>=$fstart and $hend<=$fend) or ($fend>=$hstart and $fend<=$hend)) {
$min_overlap_h=$h if (!defined $min_overlap_h);
my $start=$hstart;
my $end=$hend;
if ($start<$fstart) {
$start=$fstart;
}
if ($end>$fend) {
$end=$fend;
}
if (($end-$start+1)/($hend-$hstart+1)>=$opt{filter_rate}/100 or ($end-$start+1)/($fend-$fstart+1)>=$opt{filter_rate}/100) {
if (defined $opt{strand_specific}) {
$express+=$hash{$chr}{$strand}{$hash[$h]}[1];
$hash{$chr}{$strand}{$hash[$h]}[3]=1;
}else {
$express+=$hash{$chr}{$hash[$h]}[1];
$hash{$chr}{$hash[$h]}[3]=1;
}
}
$h++;
if (defined $hash[$h]) {
($hstart,$hend)=split /\t/,$hash[$h];
goto FCOM;
}else {
print FILTER "$filter{$chr}{$strand}{$filter[$f]}[1] EXPRESS$connector\"$express\"\n";
next;
}
}elsif ($hstart>$fend) {
print FILTER "$filter{$chr}{$strand}{$filter[$f]}[1] EXPRESS$connector\"$express\"\n";
$h=$min_overlap_h if (defined $min_overlap_h);
($hstart,$hend)=split /\t/,$hash[$h];
$min_overlap_h=undef;
next;
}
}
}
}
close FILTER;
$time=localtime;
print LOG "The clusters which belong to regions in filter file have been filtered: $time.\n\n";
#Print filtered cluster result
print LOG "Print filtered cluster result.\n";
open CLUSTER, ">$opt{prefix}\_read.cluster_filter" or die "Cannot write to $opt{prefix}\_read.cluster_filter file.\n";
print CLUSTER "#ID\tQLength\tQStart\tQend\tTStart\tTend\tLength\tScore\tE-value\tOverlap/Total\tIdentity\tSubject_Name\tRead_num\n";
my $filtered_cluster=0;
my $filtered_read=0;
foreach my $chr (keys %hash) {
if (defined $opt{strand_specific}) {
foreach my $strand (keys %{$hash{$chr}}) {
my @record=sort {$hash{$chr}{$strand}{$a}[0]<=>$hash{$chr}{$strand}{$b}[0]} keys %{$hash{$chr}{$strand}};
for (my $i=0;$i<@record;$i++) {
if (!defined $hash{$chr}{$strand}{$record[$i]}[3]) {
print CLUSTER "$hash{$chr}{$strand}{$record[$i]}[2]\n";
$filtered_cluster++;
$filtered_read+=$hash{$chr}{$strand}{$record[$i]}[1];
}
@{$hash{$chr}{$strand}{$record[$i]}}=();
delete $hash{$chr}{$strand}{$record[$i]};
}
}
}else {
my @record=sort {$hash{$chr}{$a}[0]<=>$hash{$chr}{$b}[0]} keys %{$hash{$chr}};
for (my $i=0;$i<@record;$i++) {
if (!defined $hash{$chr}{$record[$i]}[3]) {
print CLUSTER "$hash{$chr}{$record[$i]}[2]\n";
$filtered_cluster++;
$filtered_read+=$hash{$chr}{$record[$i]}[1];
}
@{$hash{$chr}{$record[$i]}}=();
delete $hash{$chr}{$record[$i]};
}
}
}
close CLUSTER;
open (STAT,">>$opt{prefix}.statistics")||die("Cannot write to $opt{prefix}.statistics.\n");
print STAT "Cluster_number_after_filter\t$filtered_cluster\n";
print STAT "Cluster_read_after_filter\t$filtered_read\n\n";
close STAT;
%hash=();
$time=localtime;
print LOG "The filtered clusters result have been printed: $time.\n\n";
}elsif (defined $opt{filter_fa}) {
#Read $opt{prefix}_read.cluster file.\n";
print LOG "Read $opt{prefix}\_read.cluster file.\n";
open (OUT,">$opt{prefix}\_read.cluster.fa")||die("Cannot write to $opt{prefix}\_read.cluster.fa file.\n");
open (IN,"<$opt{prefix}_read.cluster")||die("Cannot read $opt{prefix}_read.cluster.\n");
while (<IN>) {
chomp;
#aau-miR160 21 1 21 1137925 1137905 2539026 34.2 0.15 20/21 95 S000014
next if (/^\#/);
my @list=split /\t/,$_;
if ($list[5] > $list[4]) {
my $cluster_start=$list[4];
my $cluster_end=$list[5];
my $cluster_seq=substr ($gen{$list[11]},$cluster_start-1,abs($cluster_end-$cluster_start)+1);
print OUT ">$list[0]\n$cluster_seq\n";
$hash{$list[0]}=$_;
}else {
my $cluster_start=$list[5];
my $cluster_end=$list[4];
my $cluster_seq=substr ($gen{$list[11]},$cluster_start-1,abs($cluster_end-$cluster_start)+1);
$cluster_seq=reverse $cluster_seq;
$cluster_seq=~tr/ACGTacgt/TGCAtgca/;
print OUT ">$list[0]\n$cluster_seq\n";
$hash{$list[0]}=$_;
}
}
close IN;
close OUT;
$time=localtime;
print LOG "The $opt{prefix}_read.cluster file has been readed: $time.\n\n";
#Filter clusters which belong to regions in filter file (gff2 format)
print LOG "Filter clusters which belong to regions in filter file.\n";
if ($opt{alignsoft} eq "blast") {
system "formatdb -i $opt{filter_fa} -p F";
system "blastall -p blastn -d $opt{filter_fa} -i $opt{prefix}\_read.cluster.fa -v 1000 -b 1000 -W 7 -o $opt{prefix}\_read.cluster.blast";
system "perl $programdir/EblastN.pl -i $opt{prefix}\_read.cluster.blast -o $opt{prefix}\_read.cluster.eblastn";
my %filter=();
open (IN,"<$opt{prefix}\_read.cluster.eblastn")||die("Cannot read $opt{prefix}\_read.cluster.eblastn.\n");
while (<IN>) {
next if (/^Query/);
my @list=split(/\t/,$_);
my ($overlap,$total)=split /\//,$list[9];
my $mis=$total-$overlap;
if ($list[10]>=$opt{identity} and $mis<=$opt{mismatch} and (($list[3]-$list[2]+1)/$list[1]>=$opt{filter_rate}/100 or (abs($list[5]-$list[4])+1)/$list[6]>=$opt{filter_rate}/100)) {
my @cluster=split /\t/,$hash{$list[0]};
$filter{$list[11]}+=$cluster[$#cluster];
delete $hash{$list[0]};
}
}
close IN;
my $out=basename($opt{filter_fa})."read";
open FILTER, ">$out" or die "Cannot write to $out file.\n";
open IN, "<$opt{filter_fa}" or die "Cannot read $opt{filter_fa} file.\n";
while (<IN>) {
if (/^>(\S+)/) {
if (defined $filter{$1}) {
print FILTER "$1\t$filter{$1}\n";
}else {
print FILTER "$1\t0\n";
}
}
}
close IN;
close FILTER;
$time=localtime;
print LOG "The clusters which belong to regions in filter file have been filtered: $time.\n\n";
#Print filtered cluster result
print LOG "Print filtered cluster result.\n";
open CLUSTER, ">$opt{prefix}\_read.cluster_filter" or die "Cannot write to $opt{prefix}\_read.cluster_filter file.\n";
print CLUSTER "#ID\tQLength\tQStart\tQend\tTStart\tTend\tLength\tScore\tE-value\tOverlap/Total\tIdentity\tSubject_Name\tRead_num\n";
my $filtered_cluster=0;
my $filtered_read=0;
foreach my $id (sort {$a<=>$b} keys %hash) {
print CLUSTER "$hash{$id}\n";
$filtered_cluster++;
my @list=split /\t/,$hash{$id};
$filtered_read+=$list[$#list];
}
close CLUSTER;
open (STAT,">>$opt{prefix}.statistics")||die("Cannot write to $opt{prefix}.statistics.\n");
print STAT "Cluster_number_after_filter\t$filtered_cluster\n";
print STAT "Cluster_read_after_filter\t$filtered_read\n\n";
close STAT;
%hash=();