-
Notifications
You must be signed in to change notification settings - Fork 4
/
SSRMMD.pl
2000 lines (1608 loc) · 65.1 KB
/
SSRMMD.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/env perl
#date : 2020-05-29
#author1
#name : Xiang-jian Gou
#QQ : 862137261
#email : xjgou@stu.sicau.edu.cn
#author2
#name : Hao-ran Shi
#QQ : 542561234
#email : 542561234@qq.com
#load modules
use strict;
use warnings;
use 5.010;
use Cwd qw/getcwd/;
use File::Basename qw/basename/;
use File::Spec;
use Storable qw/store retrieve/;
use threads;
use Getopt::Long;
#clear buffer
$| = 1;
#get current working directory
my $cwd = getcwd;
#record version information
my $VERSION = 'SSRMMD v1.0';
#set default options
my $poly = 0; #0 or 1
my $outDirName = 'SSRMMDOUT';
my $threads_num = 1;
my $fasta1_name = '';
my $fasta2_name = '';
my $miningMethod = 0; #0 or 1
my $motifs = '1=10,2=7,3=6,4=5,5=4,6=4';
my $minSsrLen = 10;
my $maxSsrLen = 10000;
my $fs_len = 100;
my $stat_file = 0; #0 or 1
my $conserMap = 'NO'; #NO NW LD
my $fsReduceFold = 0.05;
my $mismatch = 0; #0, 1 or 2
my $distanceThreshold = 0.05;
my $identityThreshold = 0.95;
my $mapScore = '1,-1,-2';
my $genome1_name = '';
my $genome2_name = '';
my $checkUniMethod = 0; #0 is time-saving, 1 is memory-saving
my $checkUniSize = 10; #10Mb
my $inter_file = 0; #0 or 1
my $runLog;
my $version;
my $help;
#get options from command line
GetOptions(
'poly=i' => \$poly,
'outDir=s' => \$outDirName,
'thread=i' => \$threads_num,
'fasta1|f1=s' => \$fasta1_name,
'fasta2|f2=s' => \$fasta2_name,
'excav=i' => \$miningMethod,
'motifs=s' => \$motifs,
'minLen|n=i' => \$minSsrLen,
'maxLen|x=i' => \$maxSsrLen,
'length=i' => \$fs_len,
'stats|ss=i' => \$stat_file,
'method=s' => \$conserMap,
'reduce=f' => \$fsReduceFold,
'mismatch|ms=i' => \$mismatch,
'distance=f' => \$distanceThreshold,
'identity=f' => \$identityThreshold,
'score=s' => \$mapScore,
'genome1|g1=s' => \$genome1_name,
'genome2|g2=s' => \$genome2_name,
'uniStyle|st=i' => \$checkUniMethod,
'uniSize|si=i' => \$checkUniSize,
'all=i' => \$inter_file,
'workLog=s' => \$runLog,
'version+' => \$version,
'help+' => \$help,
);
#describe program information
my $usage = <<__GUIDE__;
###############################################################################
Name:
SSRMMD - Simple Sequence Repeat Molecular Marker Developer
Author:
Xiangjian Gou (xjgou\@stu.sicau.edu.cn), Haoran Shi (542561234\@qq.com)
Function:
Mining perfect SSR loci and candidate polymorphic SSRs.
Usage:
perl SSRMMD.pl option1 <value1> option2 <value2> ... optionN <valueN>
Options:
<Here are some basic options>:
-p | -poly <INT> : specify how to work of SSRMMD. (default: 0)
0 : only mining perfect SSR loci
1 : further mining polymorphic SSRs
-o | -outDir <STR> : specify a directory for storing output file, create
directory if it doesn't exist. (default: SSRMMDOUT)
-t | -thread <INT> : the number of threads for running. (default: 1)
<Here are some options of mining perfect SSR loci>:
-f1 | -fasta1 <STR> : a FASTA file for mining SSR loci. (must be provided)
-f2 | -fasta2 <STR> : another FASTA file when plan to mine polymorphic SSRs.
-e | -excav <INT> : specify a method for mining SSR loci. (default: 0)
0 : mining SSR by integrated regular expression
1 : mining SSR by simple regular expression
Note: integrated regular expression mean that traversal
each sequence only once, no matter how many motifs are
set (option '-mo'). It usually has faster computational
speed, but sometimes it misses (extremely low probabil-
ity) a few of compound SSRs.
-mo | -motifs <STR> : threshold of motif. (default: 1=10,2=7,3=6,4=5,5=4,6=4)
left of equal : length of motif
right of equal : the minimum number of repeat
-n | -minLen <INT> : the minimum length of SSR. (default: 10)
-x | -maxLen <INT> : the maximum length of SSR. (default: 10000)
-l | -length <INT> : length of SSR flanking sequences. (default: 100)
Note: if option '-p' = 1, flanking sequences will be
used to check for conservativeness and uniqueness.
-ss | -stats <INT> : whether to output SSR statistics file. (default: 0)
0 : not output
1 : yes output
<Here are some options of checking flanking sequences conservativeness>:
-me | -method <STR> : Algorithm for exactly checking flanking sequences
conservativeness. (default: NO)
NO : only simple check by HASH
LD : global alignment by Levenshtein Distance
NW : global alignment by Needleman-Wunsch
Note: 'NO' mean flanking sequences are perfectly
conservative, while 'LD' and 'NW' allow mismatch
or indel in flanking sequences.
-r | -reduce <FLOAT> : conservativeness pre-alignment by using X% flanking
sequences near SSR. (default: 0.05 [X% = 5%])
Note: -r only make sense when option '-me' = LD/NW.
-ms | -mismatch <INT> : set the maximum number of mismatch base during
pre-alignment. (default: 0)
0 : no mismatch
1 : one mismatch
2 : two mismatch
Note: we assume that flanking sequences near SSR
are highly conservative.
-d | -distance <FLOAT> : if option '-me' = LD, set threshold of Levenshtein
Distance. (default: 0.05 [5%])
Note: the smaller the Levenshtein Distance, the
higher the sequence identity.
-i | -identity <FLOAT> : if option '-me' = NW, set threshold of sequence
identity calculated by Needleman-Wunsch algori-
thm. (default: 0.95 [95%])
-sc | -score <STR> : mapping score of NW algorithm. (default: 1,-1,-2)
Note: here, 1 = match, -1 = mismatch, -2 = indel
<Here are some options of checking flanking sequences uniqueness>:
-g1 | -genome1 <STR> : genome file of fasta1 for checking flanking sequences
uniqueness in genome-scale. (default: fasta1 file)
-g2 | -genome2 <STR> : genome file of fasta2 for checking flanking sequences
uniqueness in genome-scale. (default: fasta2 file)
-st | -uniStyle <INT> : specify a run style to check uniqueness. (default: 0)
0 : run in a time-saving manner
1 : run in a memory-saving manner
-si | -uniSize <INT> : if option '-st' = 1, set data size of each uniqueness
check. (default: 10 [10Mb])
Note : the smaller value, the smaller memory used.
<Here are some other options>:
-a | -all <INT> : whether to output intermediate file. (default: 0)
0 : not output
1 : yes output (be used to debug)
-w | -workLog <STR> : create a file to record run log. (default: STDOUT)
-v | -version : show the version information.
-h | -help : show the help information.
Example:
1. Mining perfect SSR loci :
perl SSRMMD.pl -f1 example.fa -t 2
2. Mining polymorphic SSRs :
perl SSRMMD.pl -f1 example1.fa -f2 example2.fa -p 1 -t 2 -me NW
###############################################################################
__GUIDE__
#get motifs information
my $molen_minum = getMotifInfo($motifs);
#get mapping score
my ($match, $unmatch, $space) = $mapScore =~ /\A(-?\d+),(-?\d+),(-?\d+)\z/;
#check the options
die "$VERSION\n" if $version;
die $usage if $help;
die "Error: option '-p' only be set to 0 or 1 !\n" if $poly != 0 and $poly != 1;
die "Error: option '-t' must be an integer bigger than 0 !\n" if $threads_num <= 0 or $threads_num =~ /\.\d*[1-9]+/;
die "Error: option '-f1' must be provided !\n" if ! $fasta1_name;
die "Error: if option '-p' be set to 1, option '-f2' must be provided !\n" if $poly == 1 and ! $fasta2_name;
die "Error: option '-e' only be set to 0 or 1 !\n" if $miningMethod != 0 and $miningMethod != 1;
die "Error: option '-n' must be an integer bigger than 0 !\n" if $minSsrLen <= 0 or $minSsrLen =~ /\.\d*[1-9]+/;
die "Error: option '-x' must be an integer bigger than 0 !\n" if $maxSsrLen <= 0 or $maxSsrLen =~ /\.\d*[1-9]+/;
die "Error: option '-l' must be an integer bigger than 0 !\n" if $fs_len <= 0 or $fs_len =~ /\.\d*[1-9]+/;
die "Error: option '-ss' only be set to 0 or 1 !\n" if $stat_file != 0 and $stat_file != 1;
die "Error: option '-me' only be set to 'NO' or 'LD' or 'NW' !\n" if $conserMap ne 'NO' and $conserMap ne 'LD' and $conserMap ne 'NW';
die "Error: option '-r' only be set to 0-0.5 !\n" if $fsReduceFold <= 0 or $fsReduceFold > 0.5;
die "Error: option '-ms' only be set to 0, 1 or 2 !\n" if $mismatch != 0 and $mismatch != 1 and $mismatch != 2;
die "Error: option '-d' only be set to 0-0.5 !\n" if $distanceThreshold < 0 or $distanceThreshold > 0.5;
die "Error: option '-i' only be set to 0.5-1 !\n" if $identityThreshold < 0.5 or $identityThreshold > 1;
die "Error: option '-sc' have a problem !\n" if (! defined($match)) or (! defined($unmatch)) or (! defined($space));
die "Error: option '-st' only be set to 0 or 1 !\n" if $checkUniMethod != 0 and $checkUniMethod != 1;
die "Error: option '-si' must be an integer bigger than 1 (1Mb) !\n" if $checkUniSize < 1 or $checkUniSize =~ /\.\d*[1-9]+/;
die "Error: option '-a' only be set to 0 or 1 !\n" if $inter_file != 0 and $inter_file != 1;
die "Error: if option '-d' be set to 0, the author suggests you that set option '-me' to 'NO' !\n" if $distanceThreshold == 0 and $conserMap ne 'NO';
die "Error: if option '-i' be set to 1, the author suggests you that set option '-me' to 'NO' !\n" if $identityThreshold == 1 and $conserMap ne 'NO';
die "Error: if option '-p' be set to 1, the value of option '-f1' and option '-f2' cannot be the same !\n" if $poly == 1 and $fasta1_name eq $fasta2_name;
#specify conservativeness threshold
my $conserThreshold = $conserMap eq 'LD' ? $distanceThreshold : $identityThreshold;
#specify genome file
$genome1_name = $fasta1_name if ! $genome1_name;
$genome2_name = $fasta2_name if $fasta2_name and ! $genome2_name;
#get base name of fasta file
my $fa_base_name1 = basename $fasta1_name;
my $fa_base_name2 = basename $fasta2_name if $fasta2_name;
#create output directory
mkdir $outDirName, 0755 or die "Error: can't create directory '$outDirName' : $!" if ! -d $outDirName;
chdir $outDirName or die "Error: can't cd to directory '$outDirName' : $!";
#create log file
my $log;
open $log, '>', $runLog or die "Error: can't generate $runLog : $!" if defined $runLog;
my $logMark = defined $runLog ? $runLog : 'STDOUT';
my $optionSet = <<__OPTION__;
Parameter settings:
<Here are some basic options>:
-p $poly
-o $outDirName
-t $threads_num
<Here are some options of mining perfect SSR loci>:
-f1 $fasta1_name
-f2 $fasta2_name
-e $miningMethod
-mo $motifs
-n $minSsrLen
-x $maxSsrLen
-l $fs_len
-ss $stat_file
<Here are some options of checking flanking sequences conservativeness>:
-me $conserMap
-r $fsReduceFold
-ms $mismatch
-d $distanceThreshold
-i $identityThreshold
-sc $mapScore
<Here are some options of checking flanking sequences uniqueness>:
-g1 $genome1_name
-g2 $genome2_name
-st $checkUniMethod
-si $checkUniSize
<Here are some other options>:
-a $inter_file
-w $logMark
Run step record:
__OPTION__
#write options information to logfile
writeLogInfo($log, $optionSet);
#get real size of check uniqueness of flanking sequences
$checkUniSize *= 1_000_000;
################################################################################
# start main program ... #
################################################################################
#Module1 : mining perfect SSR loci
if ($poly == 0) {
#record run step (start)
my $localTime1 = localtime;
writeLogInfo($log, " Start to mine SSR loci for fasta1 : $localTime1\n");
#1 deal fasta file
my ($tmp_dir_name1, $final_threads_num, $id_len) = dealSeqs($fasta1_name, $outDirName, $cwd, $threads_num);
#2 mining SSR loci
my $all_SSRs = multithreadsMiningSsr($miningMethod, $molen_minum, $tmp_dir_name1, $final_threads_num, $fs_len, $minSsrLen, $maxSsrLen);
#3 output SSR and statistics information
my $fa_base_name = basename $fasta1_name;
printSsrInfo($fa_base_name, '.SSRs', $all_SSRs);
some_simple_statistics($fa_base_name, $all_SSRs, $id_len, $molen_minum) if $stat_file;
undef $all_SSRs;
undef $id_len;
#record run step (end)
my $localTime2 = localtime;
writeLogInfo($log, " Run finish : $localTime2\n");
}
#Module2 : developing candidate polymorphic SSRs
else {
my @retrieve_files; #store some tmp information
#step1 : mining SSRs loci (fasta1 and fasta2)
#record run step
my $localTime1_1 = localtime;
writeLogInfo($log, " Start to mine SSR loci for fasta1 : $localTime1_1\n");
#deal fasta1 file
my ($tmp_dir_name1_1, $final_threads_num1, $id_len1) = dealSeqs($fasta1_name, $outDirName, $cwd, $threads_num);
push @retrieve_files, $id_len1;
#mining SSR loci of fasta1
my $all_SSRs1 = multithreadsMiningSsr($miningMethod, $molen_minum, $tmp_dir_name1_1, $final_threads_num1, $fs_len, $minSsrLen, $maxSsrLen);
#output SSR and statistics information of fasta1
my $fa_base_name11 = basename $fasta1_name;
printSsrInfo($fa_base_name11, '.SSRs', $all_SSRs1);
some_simple_statistics($fa_base_name11, $all_SSRs1, $id_len1, $molen_minum) if $stat_file;
undef $id_len1;
#store SSR information file1 to tmp file
push @retrieve_files, storeToFile($fa_base_name11, 'ssr', $all_SSRs1);
undef $all_SSRs1;
#record run step
my $localTime1_2 = localtime;
writeLogInfo($log, " Start to mine SSR loci for fasta2 : $localTime1_2\n");
#deal fasta2 file
my ($tmp_dir_name1_2, $final_threads_num2, $id_len2) = dealSeqs($fasta2_name, $outDirName, $cwd, $threads_num);
push @retrieve_files, $id_len2;
#mining SSR loci of fasta2
my $all_SSRs2 = multithreadsMiningSsr($miningMethod, $molen_minum, $tmp_dir_name1_2, $final_threads_num2, $fs_len, $minSsrLen, $maxSsrLen);
#output SSR and statistics information of fasta2
my $fa_base_name22 = basename $fasta2_name;
printSsrInfo($fa_base_name22, '.SSRs', $all_SSRs2);
some_simple_statistics($fa_base_name22, $all_SSRs2, $id_len2, $molen_minum) if $stat_file;
undef $id_len2;
#store SSR information file2 to tmp file
push @retrieve_files, storeToFile($fa_base_name22, 'ssr', $all_SSRs2);
undef $all_SSRs2;
#step2 : keep all flanking sequence conservative SSRs
#record run step
my $localTime2 = localtime;
writeLogInfo($log, " Start to check conservativeness of flanking sequences : $localTime2\n");
#deal fasta1 to exclude non-unique SSR by myself
my $all_SSRs_add_fs1 = retrieveFromFile($retrieve_files[1]);
my $fs_count1 = generate_fs_list($all_SSRs_add_fs1);
my $fs_uni_SSRs1 = keep_fs_uniq_SSRs($fs_count1, $all_SSRs_add_fs1);
undef $fs_count1;
undef $all_SSRs_add_fs1;
#deal fasta2 to exclude non-unique SSR by myself
my $all_SSRs_add_fs2 = retrieveFromFile($retrieve_files[3]);
my $fs_count2 = generate_fs_list($all_SSRs_add_fs2);
my $fs_uni_SSRs2 = keep_fs_uniq_SSRs($fs_count2, $all_SSRs_add_fs2);
undef $fs_count2;
undef $all_SSRs_add_fs2;
#simple conservativeness check by HASH
my ($fs_con_SSRs1, $fs_con_SSRs2, $con_compared) = keep_fs_cons_SSRs($fs_uni_SSRs1, $fs_uni_SSRs2);
#exactly conservativeness check by LD or NW
if ($conserMap ne 'NO') {
#1 store conservativeness information of compare to tmp file
my $map_cmp_file = storeToFile($fa_base_name1, 'mapping-cmp', $con_compared);
undef $con_compared;
#2 get rest pre-unique SSRs of fasta1 from all unique sets1
my $restUniSSRs1 = getRestSSRsFromUniSet($fs_uni_SSRs1, $fs_con_SSRs1, $fsReduceFold);
undef $fs_uni_SSRs1;
my $map_con_file1 = storeToFile($fa_base_name1, 'mapping', $fs_con_SSRs1);
undef $fs_con_SSRs1;
#3 get rest pre-unique SSRs of fasta2 from all unique sets2
my $restUniSSRs2 = getRestSSRsFromUniSet($fs_uni_SSRs2, $fs_con_SSRs2, $fsReduceFold);
undef $fs_uni_SSRs2;
my $map_con_file2 = storeToFile($fa_base_name2, 'mapping', $fs_con_SSRs2);
undef $fs_con_SSRs2;
#4 pre-alignment for checking conservativeness by short sequence
my $common = prealign_highly_conservativeness($restUniSSRs1, $restUniSSRs2, $mismatch);
undef $restUniSSRs1;
undef $restUniSSRs2;
#5 split information to tmp file
my $base_name = basename $fasta1_name;
my $tmp_dir_name2 = splitRestSSRsToTempFiles($common, $threads_num, $base_name);
undef $common;
#6 start to conservativeness mapping by LD or NW
my ($con_SSRs1, $con_SSRs2, $compare) = multiThreadsMapFsToFindConSSR($tmp_dir_name2, $conserThreshold, $conserMap);
#7 get conservativeness fasta1 file
$fs_con_SSRs1 = retrieveFromFile($map_con_file1);
$fs_con_SSRs1 = mergeScalar($fs_con_SSRs1, $con_SSRs1);
undef $con_SSRs1;
#8 get conservativeness fasta2 file
$fs_con_SSRs2 = retrieveFromFile($map_con_file2);
$fs_con_SSRs2 = mergeScalar($fs_con_SSRs2, $con_SSRs2);
undef $con_SSRs2;
#9 get conservativeness compare file
$con_compared = retrieveFromFile($map_cmp_file);
$con_compared = mergeScalar($con_compared, $compare);
undef $compare;
}
#free memory
undef $fs_uni_SSRs1;
undef $fs_uni_SSRs2;
#output conservativeness file of fasta1 for debug
printMiddleSsrInfo($fa_base_name1, '.conservative', $fs_con_SSRs1) if $inter_file;
#output conservativeness file of fasta2 for debug
printMiddleSsrInfo($fa_base_name2, '.conservative', $fs_con_SSRs2) if $inter_file;
#store conservativeness information of fasta1 to tmp file
my $fs_con_SSRs_file1 = storeToFile($fa_base_name1, 'con', $fs_con_SSRs1);
undef $fs_con_SSRs1;
#store conservativeness information of fasta2 to tmp file
my $fs_con_SSRs_file2 = storeToFile($fa_base_name2, 'con', $fs_con_SSRs2);
undef $fs_con_SSRs2;
#store conservativeness information of compare to tmp file
my $con_compare_file = storeToFile("$fa_base_name1-$fa_base_name2", 'cmp', $con_compared);
undef $con_compared;
#step3 : keep all unique SSRs
#record run step
my $localTime3 = localtime;
writeLogInfo($log, " Start to check uniqueness of flanking sequences for fasta1 : $localTime3\n");
#deal fasta1 to check uniqueness of flanking sequences
my $tmp_dir_name3_1 = splitFastaInfoToTmpFile($genome1_name, $outDirName, $cwd, $threads_num, $retrieve_files[0]);
undef $retrieve_files[0];
my $fs_uniq_SSRs1 = multithreadsKeepUniqueSsr($tmp_dir_name3_1, $fs_con_SSRs_file1, $fs_len, $checkUniMethod, $checkUniSize);
undef $tmp_dir_name3_1;
undef $fs_con_SSRs_file1;
#output uniqueness file of fasta1 for debug
printMiddleSsrInfo($fa_base_name1, '.unique', $fs_uniq_SSRs1) if $inter_file;
#store uniqueness information of fasta1 to tmp file
my $uniq_SSRs_file1 = storeToFile($fa_base_name1, 'uni', $fs_uniq_SSRs1);
undef $fs_uniq_SSRs1;
#record run step
my $localTime4 = localtime;
writeLogInfo($log, " Start to check uniqueness of flanking sequences for fasta2 : $localTime4\n");
#deal fasta2 to check uniqueness of flanking sequences
my $tmp_dir_name3_2 = splitFastaInfoToTmpFile($genome2_name, $outDirName, $cwd, $threads_num, $retrieve_files[2]);
undef $retrieve_files[2];
my $fs_uniq_SSRs2 = multithreadsKeepUniqueSsr($tmp_dir_name3_2, $fs_con_SSRs_file2, $fs_len, $checkUniMethod, $checkUniSize);
undef $tmp_dir_name3_2;
undef $fs_con_SSRs_file2;
#output uniqueness file of fasta2 for debug
printMiddleSsrInfo($fa_base_name2, '.unique', $fs_uniq_SSRs2) if $inter_file;
#retrieve uniqueness information of fasta1
$fs_uniq_SSRs1 = retrieveFromFile($uniq_SSRs_file1);
#retrieve uniqueness information of compare
$con_compared = retrieveFromFile($con_compare_file);
#step4 : output a table of candidate polymorphic SSRs
#record run step
my $localTime5 = localtime;
writeLogInfo($log, " Start to do polymorphism check : $localTime5\n");
#final compare and polymorphism check to distinguish polymorphic and monorphic SSRs
final_comparison_table($fs_uniq_SSRs1, $fs_uniq_SSRs2, $con_compared, $fa_base_name1, $fa_base_name2, $conserMap);
#record run step
my $localTime6 = localtime;
writeLogInfo($log, " Run finish : $localTime6\n");
}
################################################################################
# end main program !!! #
################################################################################
#Here are all subroutines are used by this program:
#01 writeLogInfo
# print run information to logfile.
#02 storeToFile
# store information (a reference) to tmp file, and return a file name.
#03 retrieveFromFile
# retrieve information from tmp file, and return the information (a reference).
#04 getSeqsLength
# get length (roughly) of each sequence, and return a hash reference.
#05 dealSeqs
# deal fasta file and store sequence information to tmp file.
#06 readSeqs
# read fasta file and get sequence information, return a hash reference.
#07 average_allocation_algorithm
# allocate the sequence evenly to different threads by length, and return a two-dimensional array.
#08 getMotifInfo
# store the motif length and minimum number of repeat into a hash, and return a hash reference.
#09 is_false_motif
# judge if motif is false, 1 = false, 0 = true (example: 'ATAT', 'AAAA' and 'GAAGAA' is false).
#10 miningSsrBySimple
# mining SSR loci by simple regular expression, and return a hash reference.
#11 miningSsrByIntegrate
# mining SSR loci by integrated regular expression, and return a hash reference.
#12 multithreadsMiningSsr
# use multi-threads to mine SSRs and their information, and return a hash reference.
#13 some_simple_statistics
# output some simple statistics about all SSRs loci.
#14 printSsrInfo
# output a statistical file that contains SSRs information.
#15 generate_fs_list
# return a hash reference that key is the flanking sequence, value is the frequency of existence.
#16 keep_fs_uniq_SSRs
# return a hash reference that contains the unique SSRs of flanking sequences.
#17 keep_fs_cons_SSRs
# return three hash references that contain conservative SSRs in each file and conservative SSRs statistical table, respectively.
#18 getRestSSRsFromUniSet
# exclude SSR that already is conservative SSR, and return a hash reference.
#19 prealign_highly_conservativeness
# pre-alignment by highly conservativeness, and return a hash reference.
#20 mergeScalar
# merge two scalar into a single scalar, and return a hash reference.
#21 mapFsToFindConSSR
# mapping two seqs for finding conservative SSRs, and return a array reference.
#22 multiThreadsMapFsToFindConSSR
# use multithreads to map two seqs for finding conservative SSRs, and return three hash references.
#23 splitRestSSRsToTempFiles
# split hash into smaller hashes, and put these smaller hashes in temporary files.
#24 Levenshtein_Distance
# global alignment by using levenshtein distance.
#25 Needleman_Wunsch
# global alignment by using Needleman-Wunsch algorithm
#26 printMiddleSsrInfo
# output a statistical file that contains conservative or unique SSRs (for debug).
#27 splitFastaInfoToTmpFile
# split fasta information to tmp file, and return the directory name created.
#28 getFsMatchCount
# return a array reference that contains match count of each flanking sequence compared with genome sequence.
#29 sldingWindows
# check uniqueness of flanking sequences by slding window.
#30 multithreadsKeepUniqueSsr
# use multithreads to keep the unique SSRs(the flanking sequence is unique) in the genome, and return a hash reference.
#31 final_comparison_table
# output a statistical table that contains candidate polymorphic SSRs.
#=====<Here are basic subs>=====
#function1 : print run information to logfile.
sub writeLogInfo {
my ($handle, $info) = @_;
if ($handle) {
print $handle $info;
}
else {
print $info;
}
}
#function2 : store information (a reference) to tmp file, and return a file name.
sub storeToFile {
my ($file, $suffix, $info) = @_;
my ($sec, $min, $hour, $day, $mon, $year, undef, undef, undef) = localtime;
$mon += 1;
$year += 1900;
my $time = "$year$mon$day-$hour$min$sec";
my $name = "$file.$time.$suffix.tmp";
unlink $name if -e $name;
store $info, $name;
undef $info;
return $name;
}
#function3 : retrieve information from tmp file, and return the information (a reference).
sub retrieveFromFile {
my $file = shift;
die "Error: no find file '$file' : $!" unless -e $file;
my $info = retrieve $file;
unlink $file;
undef $file;
return $info;
}
#=====<Here are subs that used to mine SSR loci>=====
#function4 : get length (roughly) of each sequence, and return a hash reference.
sub getSeqsLength {
my ($fileName, $dirName, $cwd) = @_;
chdir $cwd or die "Error: can't cd to directory '$cwd' : $!";
open my $in, '<', $fileName or die "Error: can't open file '$fileName' : $!";
chdir $dirName or die "Error: can't cd to directory '$dirName' : $!";
my %seqs;
my $name;
while (<$in>) {
if (/\A>/) {
s/[\r\n]+//; #filter CR/LF at the end
s/\A>//; #filter > at the start
s/\s/_/g; #replace all spaces by '_'
$name = $_;
}
else {
$seqs{$name} += length; #the length of sequence is not real (because include CR/LF).
}
}
close $in;
die "Error: the format of file '$fileName' may be incorrect !\n" if keys %seqs == 0;
return \%seqs;
}
#function5 : deal fasta file and store sequence information to tmp file.
sub dealSeqs {
my ($fileName, $dirName, $cwd, $threadsNum) = @_;
my ($sec, $min, $hour, $day, $mon, $year, undef, undef, undef) = localtime;
$mon += 1;
$year += 1900;
my $time = "$year$mon$day-$hour$min$sec";
my $baseName = basename $fileName;
my $tmp_dir_name1 = $baseName."_$time.SSRMMD_tmp1";
if (-e $tmp_dir_name1) {
chdir $tmp_dir_name1;
unlink glob '*';
chdir '..';
rmdir $tmp_dir_name1;
}
mkdir $tmp_dir_name1, 0755 or die "Error: can't create directory '$tmp_dir_name1' : $!";
chdir $cwd or die "Error: can't cd to directory '$cwd' : $!";
open my $in, '<', $fileName or die "Error: can't open file '$fileName' : $!";
chdir $dirName or die "Error: can't cd to directory '$dirName' : $!";
my $name = <$in>;
die "Error: the format of file '$fileName' may be incorrect !\n" if $name !~ /\A>/;
$name =~ s/[\r\n]+//;
$name =~ s/\A>//;
$name =~ s/\s/_/g; #replace spaces by '_'
my $seqLength;
my $final_threads_num = 1;
if ($threadsNum == 1) {
$seqLength = readSeqs($in, $name, $tmp_dir_name1, 'NA');
}
elsif ($threadsNum > 1) {
my $lengths = getSeqsLength($fileName, $dirName, $cwd);
my $seqSum = scalar keys %$lengths;
$final_threads_num = ($threadsNum >= $seqSum) ? $seqSum : $threadsNum;
my @id_to_thread = average_allocation_algorithm($final_threads_num, %$lengths);
undef $lengths;
my %seqPos;
foreach my $i (0 .. $#id_to_thread) {
foreach my $j (1 .. $#{$id_to_thread[$i]}) {
$seqPos{ $id_to_thread[$i][$j] } = $i+1;
}
}
undef @id_to_thread;
$seqLength = readSeqs($in, $name, $tmp_dir_name1, \%seqPos);
undef %seqPos;
}
else {
die "Error: the program have a bug (option '-t' = $threadsNum)!\n";
}
close $in;
return $tmp_dir_name1, $final_threads_num, $seqLength;
}
#function6 : read fasta file and get sequence information, return a hash reference.
sub readSeqs {
my ($in, $name, $tmp_dir_name1, $seqPos) = @_;
my (%seqLength, %seqs, %seqTotal);
my $count = 1;
my $thread = ref($seqPos) eq 'HASH' ? $seqPos->{$name} : 1;
while (<$in>) {
s/[\r\n]+//;
if (/\A>/) {
if ($seqTotal{$thread} >= 10_000_000) { #each file must be >= 10Mb
my $fullName = File::Spec->catfile($tmp_dir_name1, "$thread-$count");
store $seqs{$thread}, $fullName;
foreach my $id (keys %{$seqs{$thread}}) {
$seqLength{$id} = length $seqs{$thread}{$id};
}
delete $seqs{$thread};
delete $seqTotal{$thread};
$count++;
}
$name = $_;
$name =~ s/\A>//;
$name =~ s/\s/_/g;
$thread = ref($seqPos) eq 'HASH' ? $seqPos->{$name} : 1;
}
else {
$_ = uc;
s/[^A-Z]//g;
$seqs{$thread}{$name} .= $_;
$seqTotal{$thread} += length;
}
}
foreach my $i (keys %seqs) {
my $fullName = File::Spec->catfile($tmp_dir_name1, "$i-$count");
store $seqs{$i}, $fullName;
foreach my $id (keys %{$seqs{$i}}) {
$seqLength{$id} = length $seqs{$i}{$id};
}
delete $seqs{$i};
delete $seqTotal{$i};
$count++;
}
undef %seqTotal;
undef %seqs;
undef $seqPos;
return \%seqLength;
}
#function7 : allocate the sequence evenly to different threads by length, and return a two-dimensional array.
sub average_allocation_algorithm {
my ($threads_num, %id_len) = @_;
my @id = sort { $id_len{$b} <=> $id_len{$a} } keys %id_len;
my @id_to_thread;
if ($threads_num == 1) {
$id_to_thread[0][0] = 0; #this 0 isn't important and can be changed to any value
push @{$id_to_thread[0]}, @id;
}
else{
foreach my $i (0 .. $threads_num - 1) {
$id_to_thread[$i][0] = $id_len{$id[$i]};
$id_to_thread[$i][1] = $id[$i];
}
foreach my $i ($threads_num .. $#id) {
@id_to_thread = sort { $b->[0] <=> $a->[0] } @id_to_thread if $id_to_thread[-1][0] > $id_to_thread[-2][0];
$id_to_thread[-1][0] += $id_len{$id[$i]};
push @{$id_to_thread[-1]}, $id[$i];
}