This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plinktomap.pl
executable file
·1068 lines (997 loc) · 44.8 KB
/
plinktomap.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
# $Revision: 0.21 $
# $Date: 2021/02/16 $
# $Id: plinktomap.pl $
# $Author: Michael Bekaert $
#
# RAD-tags to Genetic Map (radmap)
# Copyright (C) 2016-2021 Bekaert M <michael.bekaert@stir.ac.uk>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# POD documentation - main docs before the code
=head1 NAME
RAD-tags to Genetic Map (radmap)
=head1 SYNOPSIS
# Command line help
..:: RAD-tags to Genetic Map ::..
Usage: ./genetic_mapper.pl [options]
# LepMap pre-processing
--lepmap MANDATORY
or
--lepmap3 MANDATORY
--ped MANDATORY
--meta MANDATORY
STDOUT > LepMap (linkage) input file
# Pre-processing
--snpassoc MANDATORY
or
--arff MANDATORY
or
--ade MANDATORY
--plink MANDATORY
--ped MANDATORY
--meta MANDATORY
--fst OPTIONAL
--threshold OPTIONAL
--map OPTIONAL
--gmap OPTIONAL
--female OPTIONAL
STDOUT > SNPassoc input file
STDERR 2> Genetic Map
# Post GWAS processing
--genetic MANDATORY
--extra MANDATORY (at least once)
--markers OPTIONAL
--fasta OPTIONAL
--pos OPTIONAL
--lod OPTIONAL
STDOUT > Genetic Map
# Export marker and genotype for publication (Table S)
--plink MANDATORY
--ped MANDATORY
--markers OPTIONAL
--fasta OPTIONAL
--pos OPTIONAL
--table MANDATORY
# Manual editing
--ped MANDATORY
--genetic MANDATORY
--edit MANDATORY
STDOUT > Mappable markers list
Options
--meta <pedigree file>
The pedigree file consists of on columns 1-4+. The columns are separated by
tabs. The columns 1-4 are individual name, father, mother and sex; the next
columns are for extra phenotypes: phenotype_1 to phenotype_n. The phenotypes
are not required, but will be helpful for the GWAS analysis.
sample father mother sex phenotype_1
F1_C2_070 P0_sir P0_dam M 30
F1_C2_120 P0_sir P0_dam F 1
P0_dam 0 0 F -
P0_sir 0 0 M -
--plink <plink map file>
PLINK MAP file path with full haplotypes.
--ped <plink ped file>
PLINK PED file path with full haplotypes.
--map <mappable markers list>
Mappable markers list as generated by LepMap "SeparateChromosomes" and
"JoinSingles" functions.
--gmap <ordered markers file>
Ordered genetic map generated by LepMap.
--genetic <genetic map file>
Genetic Map as generated by R/SNPAssoc pre-processing step.
--markers <batch_<n>.catalog.tags.tsv file>
Path to STACKS catalog.tag file, including marker sequences and positions.
(incompatible with --fasta)
--fasta <uniq.full.fasta file>
Path to dDocent uniq.full.fasta file, including marker sequences. (incompatible
with --markers)
--fst <xxx.phistats.tsv>
Fst values for each marker (requires --threshold)
--hwe <xxx..hapstats.tsv>
HWE values for each marker (requires --fst)
--threshold
Fst threshold for selection (requires --fst)
--extra
GWAS results generated by R/SNPassoc (csv format) to be added to the genetic map.
--lod
Convert the P-value of the GWAS results to LOD (LOD=-log(P-value)/log(10)).
--pos
Provide the marker chromosome/contig and location. (requires --markers)
--female
Select the female only map rather than male or average mapping. (requires --gmap)
--snpassoc
Enable R/SNPAssoc pre-processing.
--arff
Enable Weka ARFF format.
--ade
Enable R/ade pre-processing.
--lepmap
Enable LepMap pre-processing.
--table
Enable export for publication for markers and genotypes.
--edit
Enable Manual editing mode.
--select
Provide the list of id to select
=head1 DESCRIPTION
Perl script for the analyse RAD-tags and generate the Genetic Map with GWAS. The script
handles the multiple file conversions. PLINK _classic_ file L<PED|http://pngu.mgh.harvard.edu/~purcell/plink/data.shtml#ped>
and L<MAP|http://pngu.mgh.harvard.edu/~purcell/plink/data.shtml#map> are required as well as a pedigree file.
=head1 FEEDBACK
User feedback is an integral part of the evolution of this modules. Send your comments
and suggestions preferably to author.
=head1 AUTHOR
B<Michael Bekaert> (michael.bekaert@stir.ac.uk)
The latest version of genetic_mapper.pl is available at
https://github.com/pseudogene/radmap
=head1 LICENSE
Copyright 2016-2021 - Michael Bekaert
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see L<http://www.gnu.org/licenses/>.
=cut
use strict;
use warnings;
use Getopt::Long;
#----------------------------------------------------------
our ($VERSION) = 0.21;
#----------------------------------------------------------
my ($threads, $threshold, $female, $tableS, $arff, $remap, $lepmap, $lepmap3, $ade, $snpassoc, $edit, $loc, $lod, $plink, $ped, $parentage, $map, $genmap, $genetic, $markers, $fasta, $hwe_file, $fst_file, $selectlist) = (10, 0.01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
my @extra;
GetOptions(
'plink:s' => \$plink,
'ped:s' => \$ped,
'meta:s' => \$parentage,
'map:s' => \$map,
'gmap:s' => \$genmap,
'genetic:s' => \$genetic,
'extra:s' => \@extra,
'female!' => \$female,
'lepmap!' => \$lepmap,
'lepmap3!' => \$lepmap3,
'pos!' => \$loc,
'lod!' => \$lod,
'edit!' => \$edit,
'ade!' => \$ade,
'snpassoc!' => \$snpassoc,
'table!' => \$tableS,
'markers:s' => \$markers,
'fasta:s' => \$fasta,
'threshold:f' => \$threshold,
'fst:s' => \$fst_file,
'hwe:s' => \$hwe_file,
'select:s' => \$selectlist,
'arff!' => \$arff,
'remap!' => \$remap
);
my %parents_table;
my $count_meta = 0;
if (defined $parentage && -r $parentage && open(my $in, q{<}, $parentage))
{
#
#sample father mother Sex [..]
#
while (<$in>)
{
next if (m/^#/);
chomp;
my @data = split m/\t/;
if (scalar @data >= 4 && defined $data[0] && defined $data[1] && defined $data[2] && defined $data[3])
{
$count_meta = scalar @data - 4 if ($count_meta < scalar @data - 4);
@{$parents_table{$data[0]}} = @data;
}
}
close $in;
}
my %selected;
if (defined $selectlist && -r $selectlist && open(my $in, q{<}, $selectlist))
{
while (<$in>)
{
next if (m/^#/);
chomp;
my @tmp = split m/\t/x;
$selected{$tmp[0]} = $tmp[0] if (exists $tmp[0]);
}
close $in;
}
if (defined $fst_file && -r $fst_file && (open my $IN, q{<}, $fst_file))
{
my %fst;
while (<$IN>)
{
next if (m/^#/);
chomp;
my @tmp = split m/\t/x;
if (scalar @tmp >= 10) {
if (defined $selectlist && exists($selected{$tmp[0]}) && $tmp[6] < $threshold) { delete $selected{$tmp[0]}; }
elsif (!defined $selectlist && $tmp[6] >= $threshold) { $fst{$tmp[0]} = $tmp[6]; }
}
}
close $IN;
if (%fst) {
$selectlist = $fst_file;
%selected = %fst
}
}
if (defined $hwe_file && -r $hwe_file && (open my $IN, q{<}, $hwe_file))
{
my %hwe;
while (<$IN>)
{
next if (m/^#/);
chomp;
my @tmp = split m/\t/x;
if (scalar @tmp >= 14) {
if (defined $selectlist && exists($selected{$tmp[0]}) && $tmp[12] > 0.05) { delete $selected{$tmp[0]}; }
elsif (!defined $selectlist && exists($hwe{$tmp[0]}) && $tmp[12] > 0.05) { delete $hwe{$tmp[0]}; }
elsif (!defined $selectlist && $tmp[12] <= 0.05) { $hwe{$tmp[0]} = $tmp[12]; }
}
}
close $IN;
if (%hwe) {
$selectlist = $hwe_file;
%selected = %hwe
}
}
#To LepMap
if (scalar keys %parents_table > 0 && ($lepmap || $lepmap3) && defined $ped && -r $ped && open(my $in, q{<}, $ped))
{
my %table = (A => 1, C => 2, G => 3, T => 4, N => 0, 0 => 0);
my $last = 0;
# PEB
# # Stacks v1.42; PLINK v1.07; October 06, 2016
# C7 F1_dam_C7 0 0 0 0 G T A A G T A G G ...
# LINKAGE
# #java Filtering data=C07_LSalAtl2sD140.linkage.txt dataTolerance=0.001
# C7 P0_sir_C7 0 0 1 0 1 1 0 0 1 2 1 2 1 2 1 2 ...
# C7 F1_dam_C7 P0_sir_C7 P0_dam_C7 2 0 1 2 0 0 2 2 1 2 1 1 1 ...
while (<$in>)
{
next if (m/^#/);
chomp;
my @data = split m/\t/;
if (scalar @data > 8 && defined $data[0] && defined $data[1] && exists $parents_table{$data[1]})
{
if ($lepmap3)
{
if (!exists $table{$data[1]}) { $table{$data[1]} = ++$last; }
if (!exists $table{$parents_table{$data[1]}[1]}) { $table{$parents_table{$data[1]}[1]} = ++$last; }
if (!exists $table{$parents_table{$data[1]}[2]}) { $table{$parents_table{$data[1]}[2]} = ++$last; }
print $data[0], "\t", $table{$data[1]}, "\t", $table{$parents_table{$data[1]}[1]}, "\t", $table{$parents_table{$data[1]}[2]}, "\t", ($parents_table{$data[1]}[3] =~ /^F/i ? q{2} : ($parents_table{$data[1]}[3] =~ /^M/i ? q{1} : q{0})),
"\t0";
for (my $i = 6 ; $i < scalar @data ; $i = $i + 2)
{
print "\t", (defined $data[$i] && exists $table{$data[$i]} ? $table{$data[$i]} : q{0});
print q{ }, (defined $data[$i + 1] && exists $table{$data[$i + 1]} ? $table{$data[$i + 1]} : q{0});
}
}
else
{
print $data[0], "\t", $data[1], "\t", $parents_table{$data[1]}[1], "\t", $parents_table{$data[1]}[2], "\t", ($parents_table{$data[1]}[3] =~ /^F/i ? q{2} : ($parents_table{$data[1]}[3] =~ /^M/i ? q{1} : q{0})), "\t0";
for my $i (6 .. (scalar @data) - 1) { print "\t", (defined $data[$i] && exists $table{$data[$i]} ? $table{$data[$i]} : q{0}); }
}
print "\n";
}
}
close $in;
}
#To SNPAssoc
elsif (scalar keys %parents_table > 0 && ($snpassoc || $ade || $arff) && defined $ped && -r $ped && defined $plink && -r $plink && open($in, q{<}, $plink))
{
my (@list_marker, @mask_marker);
# PEB
# # Stacks v1.42; PLINK v1.07; October 06, 2016
# C7 F1_dam_C7 0 0 0 0 G T A A G T A G G ...
# plink MAP
# # Stacks v1.42; PLINK v1.07; October 06, 2016
# LSalAtl2s1 19757_13 0 4466
# LSalAtl2s1 19756_74 0 4550
# LSalAtl2s1 19491_4 0 106094
# LSalAtl2s1 19492_81 0 106518
# LSalAtl2s1 19498_31 0 118987
# LSalAtl2s1 19749_27 0 381049
# LEPMAP MAP
# #java SeparateChromosomes data=C07_LSalAtl2sD140_f.linkage.txt lodLimit=6.5 sizeLimit=2
# 0
# 6
# 6
# 6
# 0
# 0
# SNPAssoc
# id Sex Surviving 33 40 60 120 136 157 180
# F2_C7_073 Female 2 A/A - A/B A/B A/B A/B
# F2_C7_074 Female 2 A/A - B/B A/B A/B A/A
while (<$in>)
{
next if (m/^#/);
chomp;
my @data = split m/\t/;
if (scalar @data >= 2 && defined $data[0] && defined $data[1]) { push @list_marker, $data[1]; push @mask_marker, 1 if (defined $genmap || defined $map || defined $selectlist); }
}
close $in;
if (scalar @list_marker > 0 && defined $genmap && -r $genmap && open($in, q{<}, $genmap))
{
my $lg;
# genmap
# #java OrderMarkers map=mapLOD5_js.txt data=C07_LSalAtl2sD140_f.linkage.txt sexAveraged=1 useKosambi=1
# #*** LG = 1 likelihood = -5811.6314 with alpha penalty = -5811.6314
# #marker_number male_position female_position ( error_estimate )[ duplicate* OR phases] C7
# 1886 0.000 0.000 ( 0 ) 11
# 2789 0.270 0.270 ( 0 ) 11
# 2749 2.568 2.568 ( 0.2398 ) 10
# 4119 3.455 3.455 ( 0 ) -1
print {*STDERR} "Marker\tLG\tPosition\n";
while (<$in>)
{
if (m/^(#|\*)/)
{
if (m/LG = (\d+(\.\d+)?)/) { $lg = $1; }
}
else
{
chomp;
my @data = split m/\t/;
if (scalar @data > 3 && defined $data[0] && defined $data[1] && defined $data[2] && exists $list_marker[$data[0] - 1] && defined $lg)
{
undef $mask_marker[$data[0] - 1];
print {*STDERR} $list_marker[$data[0] - 1], "\t", $lg, "\t", $data[($female ? 2 : 1)], "\n";
}
}
}
close $in;
}
elsif (scalar @list_marker > 0 && defined $map && -r $map && open($in, q{<}, $map))
{
my $i = 0;
while (<$in>)
{
next if (m/^#/);
chomp;
if ($_ ne '0') { undef $mask_marker[$i]; }
$i++;
}
close $in;
}
if (defined $selectlist) {
for my $j (0 .. (scalar @list_marker) - 1) {
my $tmp = $list_marker[$j];
if ($tmp =~ m/^(\d+)_\d+/) { $tmp = $1; }
elsif ($tmp =~ m/^(dDocent.*):\d+/) { $tmp = $1; }
if (exists $selected{$tmp}) {
undef $mask_marker[$j];
}
}
}
#else { undef @mask_marker; }
if (scalar @list_marker > 0 && open($in, q{<}, $ped))
{
if ($ade)
{
my @group;
my %atcg = (A => 1, C => 2, G => 3, T => 4, N => 0, 0 => 0);
print "Samples";
for my $j (0 .. (scalar @list_marker) - 1) { print "\t", $list_marker[$j] if (!defined $mask_marker[$j]); }
print "\n";
while (<$in>)
{
next if (m/^#/);
chomp;
my @data = split m/\t/;
if (scalar @data > 8 && defined $data[0] && defined $data[1] && exists $parents_table{$data[1]} && exists $parents_table{$data[1]}[4])
{
print $data[1];
push @group, $parents_table{$data[1]}[4];
my $i = 6;
for my $j (0 .. (scalar @list_marker) - 1) {
print "\t", (defined $data[$i + $j * 2] && $data[$i + $j * 2] ne q{0} && defined $data[$i + $j * 2 + 1] && $data[$i + $j * 2 + 1] ne q{0} ? $atcg{$data[$i + $j * 2]} . $atcg{$data[$i + $j * 2 + 1]} : " ")
if (!defined $mask_marker[$j]);
}
print "\n";
}
}
print {*STDERR} 'pop <- c(\'', join("','",@group),"');\n";
} elsif ($arff) {
print {*STDOUT} "\@RELATION default\n\n\@ATTRIBUTE id STRING\n\@ATTRIBUTE sex {F,M}\n";
for my $j (1 .. $count_meta) { print {*STDOUT} "\@ATTRIBUTE phenotype_$j {}\n"; }
for my $j (0 .. (scalar @list_marker) - 1) { print {*STDOUT} '@ATTRIBUTE ', $list_marker[$j], " {AA,AC,AG,AT,CC,CG,CT,GG,GT,TT}\n", if (!defined $mask_marker[$j]); }
print {*STDOUT} "\n\@DATA\n";
while (<$in>)
{
next if (m/^#/);
chomp;
my @data = split m/\t/;
if (scalar @data > 8 && defined $data[0] && defined $data[1] && exists $parents_table{$data[1]} && $parents_table{$data[1]}[3] =~ /^(M|F)/i)
{
print {*STDOUT} $data[1], q{,}, ($parents_table{$data[1]}[3] =~ /^F/i ? q{F} : ($parents_table{$data[1]}[3] =~ /^M/i ? q{M} : q{?}));
for my $j (1 .. $count_meta) { print {*STDOUT} q{,} , (exists $parents_table{$data[1]}[3 + $j] ? $parents_table{$data[1]}[3 + $j] : q{?}) }
my $i = 6;
for my $j (0 .. (scalar @list_marker) - 1) {
print {*STDOUT} q{,}, (defined $data[$i + $j * 2] && $data[$i + $j * 2] ne q{0} && defined $data[$i + $j * 2 + 1] && $data[$i + $j * 2 + 1] ne q{0} ? $data[$i + $j * 2] . $data[$i + $j * 2 + 1] : q{?})
if (!defined $mask_marker[$j]);
}
print "\n";
}
}
} else {
print "id\tsex";
for my $j (1 .. $count_meta) { print "\tphenotype_$j", }
for my $j (0 .. (scalar @list_marker) - 1) { print "\t", $list_marker[$j] if (!defined $mask_marker[$j]); }
print "\n";
while (<$in>)
{
next if (m/^#/);
chomp;
my @data = split m/\t/;
if (scalar @data > 8 && defined $data[0] && defined $data[1] && exists $parents_table{$data[1]} && $parents_table{$data[1]}[3] =~ /^(M|F)/i)
{
print $data[1], "\t", ($parents_table{$data[1]}[3] =~ /^F/i ? 'Female' : 'Male');
for my $j (1 .. $count_meta) { print "\t", (exists $parents_table{$data[1]}[3 + $j] ? $parents_table{$data[1]}[3 + $j] : q{}) }
my $i = 6;
for my $j (0 .. (scalar @list_marker) - 1) {
print "\t", (defined $data[$i + $j * 2] && $data[$i + $j * 2] ne q{0} && defined $data[$i + $j * 2 + 1] && $data[$i + $j * 2 + 1] ne q{0} ? $data[$i + $j * 2] . q{/} . $data[$i + $j * 2 + 1] : q{-})
if (!defined $mask_marker[$j]);
}
print "\n";
}
}
}
close $in;
}
}
elsif ($remap && scalar @extra > 0 && defined $genetic && -r $genetic && open($in, q{<}, $genetic))
{
my (%list_markers, %list2_markers, %list_sequences);
open(my $seqfile, q{>}, '/tmp/blast_' . $genetic . '.fasta');
while (<$in>)
{
next if (m/^(#|Marker)/);
chomp;
my @data = split m/\t/;
if (scalar @data >= 4 && defined $data[0] && defined $data[1] && defined $data[2] && defined $data[-1])
{
@{$list_markers{$data[0]}} = @data;
print {$seqfile} '>', $data[0], "\n", $data[-1], "\n";
}
}
close $seqfile;
close $in;
foreach my $infile (@extra)
{
if (defined $infile && -r $infile && open(my $in2, q{<}, $infile))
{
#Extra
# "","comments","codominant"
# "X67793_33",NA,0.02511912
while (<$in2>)
{
chomp;
my @data = split m/,/;
if (scalar @data >= 3 && defined $data[0] && defined $data[2] && $data[2] ne 'NA')
{
if ($data[0] =~ m/X([\d\w\_\.]+)/) { $list2_markers{$1} = 1; }
elsif ($data[0] =~ m/(dDocent.*)\.(\d+)/) { $list2_markers{$1 . q{:} . $2} = 1; }
}
}
close $in2;
}
}
my %tmp2_list;
for my $item (keys %list2_markers)
{
if ($item =~ m/^(\d+)_\d+/) { $tmp2_list{$1} = $item; $tmp2_list{$item} = $1; }
elsif ($item =~ m/^(dDocent.*):\d+/) { $tmp2_list{$1} = $item; $tmp2_list{$item} = $1; }
else { $tmp2_list{$item} = $item; }
}
open(my $queryfile, q{>}, '/tmp/query_' . $genetic . '.fasta');
if (defined $markers && -r $markers)
{
my %unique;
if (defined $ped && -r $ped && defined $plink && -r $plink && open($in, q{<}, $plink))
{
my @list_full;
while (<$in>)
{
next if (m/^#/);
chomp;
my @data = split m/\t/;
if (scalar @data >= 2 && defined $data[0] && defined $data[1]) { push @list_full, $data[1]; }
}
close $in;
}
if (open($in, q{<}, $markers))
{
while (<$in>)
{
next if (m/^#/);
chomp;
my @data = split m/\t/;
if (scalar @data > 9 && defined $data[1] && defined $data[5] && exists $tmp2_list{$data[1]}) { print {$queryfile} '>', $tmp2_list{$data[1]}, "\n", $data[5], "\n"; }
# if (scalar @data > 9 && defined $data[2] && defined $data[9] && exists $tmp2_list{$data[2]}) { print {$queryfile} '>', $tmp2_list{$data[2]}, "\n", $data[9], "\n"; }
}
close $in;
}
}
elsif (defined $fasta && -r $fasta)
{
if (open($in, q{<}, $fasta))
{
my ($seq, $header) = (q{});
while (<$in>)
{
next if /^\s*$/;
chomp;
if (/^>/)
{ # fasta header line
my $h = $_;
$h =~ s/^>//;
if (defined $header && length $seq > 10)
{
print {$queryfile} '>', $tmp2_list{$header}, "\n", $seq, "\n" if (exists $tmp2_list{$header});
$header = $h;
$seq = q{};
}
else { $header = $h }
}
else
{
s/\W+//;
$seq .= $_;
}
}
if (defined $header && length $seq > 10) { print {$queryfile} '>', $tmp2_list{$header}, "\n", $seq, "\n" if (exists $tmp2_list{$header}); }
close $in;
}
}
close $queryfile;
if (system('makeblastdb -dbtype nucl -in "' . '/tmp/blast_' . $genetic . '.fasta' . '" -parse_seqids -hash_index -out "' . '/tmp/blast_' . $genetic . '_db" >/dev/null') != 0)
{
print {*STDERR} "ERROR: makeblastdb failed to execute: $!\n";
system('rm -f /tmp/query_' . $genetic . '.* /tmp/blast_' . $genetic . '*');
exit 10;
}
if (
system(
'blastn'
. ' -query '
. '/tmp/query_'
. $genetic
. '.fasta'
. ' -db "'
. '/tmp/blast_'
. $genetic
. '_db" -task blastn -dust yes -outfmt "6 std qlen slen" -max_target_seqs 1'
. (int($threads) > 1 ? ' -num_threads ' . int($threads) : q{})
. ' -out /tmp/query_'
. $genetic
. '.blast'
) != 0
)
{
print {*STDERR} "ERROR: blastn failed to execute: $!\n";
system('rm -f /tmp/query_' . $genetic . '.* /tmp/blast_' . $genetic . '*');
exit 10;
}
my %remap;
if (open($in, q{<}, '/tmp/query_' . $genetic . '.blast'))
{
while (<$in>)
{
next if (m/^#/);
chomp;
my @data = split m/\t/;
if (scalar @data >= 14 && defined $data[0] && defined $data[1] && defined $data[2] && defined $data[3] && defined $data[12] && defined $data[13])
{
# NEED CLEAN UP
if (int($data[2]) >= 90 && (int($data[3]) == int($data[12]) || int($data[3]) == int($data[13]) || int($data[3]) >= 99))
{
$remap{$data[0]} = $list_markers{$data[1]};
print {*STDERR} $data[0], "\t", $list_markers{$data[1]}[0], "\n";
}
}
}
close $in;
}
system('rm -f /tmp/query_' . $genetic . '.* /tmp/blast_' . $genetic . '*');
print {*STDOUT} "Marker\tLG\tPosition\n";
for my $item (keys %list2_markers) { print {*STDOUT} $item, "\t", (exists $remap{$item} ? $remap{$item}[1] . "\t" . $remap{$item}[2] : "Unk\t0"), "\n"; }
}
elsif ($tableS && defined $ped && -r $ped && defined $plink && -r $plink && open($in, q{<}, $plink))
{
my (@list_marker, @samples, %unique, %list_markers, %list_sequences );
# PEB
# # Stacks v1.42; PLINK v1.07; October 06, 2016
# C7 F1_dam_C7 0 0 0 0 G T A A G T A G G ...
# plink MAP
# # Stacks v1.42; PLINK v1.07; October 06, 2016
# LSalAtl2s1 19757_13 0 4466
# LSalAtl2s1 19756_74 0 4550
# LSalAtl2s1 19491_4 0 106094
# LSalAtl2s1 19492_81 0 106518
# LSalAtl2s1 19498_31 0 118987
# LSalAtl2s1 19749_27 0 381049
while (<$in>)
{
next if (m/^#/);
chomp;
my @data = split m/\t/;
if (scalar @data >= 2 && defined $data[0] && defined $data[1]) { @{$list_markers{$data[1]}} = @data; push @list_marker,$data[1]; }
}
close $in;
my %tmp_list;
for my $item (keys %list_markers)
{
if ($item =~ m/^(\d+)_\d+/) { $tmp_list{$1} = $item; $tmp_list{$item} = $1; }
elsif ($item =~ m/^(dDocent.*):\d+/) { $tmp_list{$1} = $item; $tmp_list{$item} = $1; }
else { $tmp_list{$item} = $item; }
}
if (scalar keys %list_markers > 0 && open($in, q{<}, $ped))
{
while (<$in>)
{
next if (m/^#/);
chomp;
my @data = split m/\t/;
if (scalar @data > 8)
{
push @samples, $data[1];
for my $j (0 .. (scalar @list_marker) - 1)
{
if (defined $list_marker[$j] && exists $list_markers{$list_marker[$j]})
{
if (!exists $unique{$list_marker[$j]}) { $unique{$list_marker[$j]} = q{} }
$unique{$list_marker[$j]} .= (defined $data[6 + $j * 2] && $data[6 + $j * 2] ne q{0} && defined $data[6 + $j * 2 + 1] && $data[6 + $j * 2 + 1] ne q{0} ? $data[6 + $j * 2] . $data[6 + $j * 2 + 1] : q{}) . "\t";
}
}
}
}
close $in;
}
if (defined $markers && -r $markers)
{
if (open($in, q{<}, $markers))
{
# ## cstacks version 1.42; catalog generated on 2016-09-22 22:24:20
# 0 2 1 LSalAtl2s1000 1022 - consensus 0 263_8... CATGTTTATGTATCATTTGTACTATTATAAAACTGAAATATATATTTTTATGTTTTTGTAAAAATGTTTAATTTATTATCTATAACCATTCCTATTCGCC 0 0 0 0
# 0 2 2 LSalAtl2s1000 132767 + consensus 0 263_13... CATGGTAAATTCGTGGTTTACACTATCATTGTCAGACAAAATTGTTGTGAGTACTATCATCTTGAAGCAATGTCGATGCAAGCAATAAGATTGTAAGTAA 0 0 0 0
# # cstacks version 2.3e; catalog generated on 2019-05-20 16:12:34
# 0 1 consensus 0 148_1... CATGCATGTTACTTAAGGGTAGTTTCAGAGGAGCAAGTGGCACATCCCTCCCTCTGCATTTTCAAATGACTGTTGTTGATTTTATTAAAACAAATTCTCCAAATTAAAGTGTAAAATCTTGGTAACCTTTGGAAGTAAAGT 0 0 0
my $seqpos = 5;
my $seqid = 1;
my $line = <$in>;
if ($line =~ m/version 1/) {
$seqpos = 9;
$seqid = 2;
}
while (<$in>)
{
next if (m/^#/);
chomp;
my @data = split m/\t/;
if (scalar @data > $seqpos && defined $data[$seqid] && defined $data[$seqpos] && exists $tmp_list{$data[$seqid]})
{
my $snploc;
if ($tmp_list{$data[$seqid]} =~ m/\_(\d+)/) { $snploc = $1; }
elsif ($tmp_list{$data[$seqid]} =~ m/dDocent.*\.(\d+)/) { $snploc = $1; }
my $tmp = $unique{$tmp_list{$data[$seqid]}};
$tmp =~ s/(.)(?=.*?\1)//g;
$tmp =~ s/\W//g;
my $seq = (defined $tmp && defined $snploc ? substr($data[$seqpos], 0, $snploc) . q{[} . $tmp . q{]} . substr($data[$seqpos], $snploc + 1) : $data[$seqpos]);
# to be check for v2+
$list_sequences{$tmp_list{$data[$seqid]}} = (int($loc) > 0 && defined $data[3] && defined $data[4] ? $data[3] . "\t" . $data[4] . "\t" . $seq : $seq);
}
}
close $in;
}
}
elsif (defined $fasta && -r $fasta)
{
if (open($in, q{<}, $fasta))
{
# >dDocent_Contig_12
# TGCAGAAAACACTCTCTCCCCAGACGGGTTTTGATAGAGTAGAACTCCGTCTCGATAGAAAGCAAAGTTGTTATATATATAGTAATAACTAGAGGGATTANNNNNNNNNNCATGTTTAATTTTAAAACATTTTCACACAACCTTAGATGGCTTTTATATTTAATATTCTATTCGAAATTTAAAAGATTTTGTAGCGGTGGATATTTTTGT
my ($seq, $header) = (q{});
while (<$in>)
{
next if /^\s*$/;
chomp;
if (/^>/)
{ # fasta header line
my $h = $_;
$h =~ s/^>//;
if (defined $header && length $seq > 10)
{
if ($header =~ /^(\d+) pos=([^:]+):(\d+):(.) NS=/) {
my ($a, $b, $c, $d) = ($1, $2, $3, $4);
if (exists $tmp_list{$a}) {
my $snploc;
if ($tmp_list{$a} =~ m/\_(\d+)/) { $snploc = $1; }
elsif ($tmp_list{$a} =~ m/dDocent.*\.(\d+)/) { $snploc = $1; }
my $tmp = $unique{$tmp_list{$a}};
$tmp =~ s/(.)(?=.*?\1)//g;
$tmp =~ s/\W//g;
my $seq = (defined $snploc ? substr($seq, 0, $snploc) . q{[} . $tmp . q{]} . substr($seq, $snploc + 1) : $seq);
$list_sequences{$tmp_list{$a}} = $b . "\t" . $c . "\t" . $d . "\t" . $seq;
}
} else {
$list_sequences{$header} = $seq if (exists $tmp_list{$header});
}
$header = $h;
$seq = q{};
}
else { $header = $h }
}
else
{
s/\W+//;
$seq .= $_;
}
}
if (defined $header && length $seq > 10) { if ($header =~ /^(\d+) pos=([^:]+):(\d+):(.) NS=/) {
my ($a, $b, $c, $d) = ($1, $2, $3, $4);
if (exists $tmp_list{$a}) {
my $snploc;
if ($tmp_list{$a} =~ m/\_(\d+)/) { $snploc = $1; }
elsif ($tmp_list{$a} =~ m/dDocent.*\.(\d+)/) { $snploc = $1; }
my $tmp = $unique{$tmp_list{$a}};
$tmp =~ s/(.)(?=.*?\1)//g;
$tmp =~ s/\W//g;
my $seq = (defined $snploc ? substr($seq, 0, $snploc) . q{[} . $tmp . q{]} . substr($seq, $snploc + 1) : $seq);
$list_sequences{$tmp_list{$a}} = $b . "\t" . $c . "\t" . $d . "\t" . $seq;
}
} else {
$list_sequences{$header} = $seq if (exists $tmp_list{$header});
} }
close $in;
}
}
print {*STDOUT} "Marker\t", join("\t", @samples), (defined $markers && -r $markers ? "\t" . 'Details' : (defined $fasta && -r $fasta ? "\t" . 'Marker' : q{})), "\n";
for my $item (keys %list_markers) {
if(!%selected || exists $selected{$item}){
print {*STDOUT} $item, "\t", substr($unique{$item},0,length($unique{$item}) -1), (exists $list_sequences{$item} ? "\t" . $list_sequences{$item} : (exists $tmp_list{$item} && exists $list_sequences{$tmp_list{$item}} ? "\t" . $list_sequences{$tmp_list{$item}} : q{})), "\n";
}
}
}
elsif (scalar @extra > 0 && defined $genetic && -r $genetic && open($in, q{<}, $genetic))
{
my (%list_markers, %list_sequences);
# genetic
# Marker LG Position
# 67793_33 1 0.000
# 65135_20 1 1.561
# 47811_36 1 4.114
# 47815_44 1 4.114
# 11332_62 1 6.270
# 47683_83 1 6.537
while (<$in>)
{
next if (m/^(#|Marker)/);
chomp;
my @data = split m/\t/;
if (scalar @data >= 2 && defined $data[0] && defined $data[1] && defined $data[2]) { @{$list_markers{$data[0]}} = @data; }
}
close $in;
foreach my $infile (@extra)
{
if (defined $infile && -r $infile && open(my $in2, q{<}, $infile))
{
my %tmp_list;
#Extra
# "","comments","codominant"
# "X67793_33",NA,0.02511912
while (<$in2>)
{
chomp;
my @data = split m/,/;
if (scalar @data >= 3 && defined $data[0] && defined $data[2] && $data[2] ne 'NA')
{
if ($data[0] =~ m/X([\d\w\_\.]+)/) { $tmp_list{$1} = $data[2]; }
elsif ($data[0] =~ m/(dDocent.*)\.(\d+)/) { $tmp_list{$1 . q{:} . $2} = $data[2]; }
}
}
close $in2;
if (scalar keys %tmp_list > 0)
{
for my $item (keys %list_markers) { push @{$list_markers{$item}}, (exists $tmp_list{$item} ? ($lod ? -log($tmp_list{$item}) / log(10) : $tmp_list{$item}) : q{-}); }
}
}
}
my %tmp_list;
for my $item (keys %list_markers)
{
if ($item =~ m/^(\d+)_\d+/) { $tmp_list{$1} = $item; $tmp_list{$item} = $1; }
elsif ($item =~ m/^(dDocent.*):\d+/) { $tmp_list{$1} = $item; $tmp_list{$item} = $1; }
else { $tmp_list{$item} = $item; }
}
my %unique;
if (defined $ped && -r $ped && defined $plink && -r $plink && open($in, q{<}, $plink))
{
my @list_full;
while (<$in>)
{
next if (m/^#/);
chomp;
my @data = split m/\t/;
if (scalar @data >= 2 && defined $data[0] && defined $data[1]) { push @list_full, $data[1]; }
}
close $in;
if (scalar @list_full > 0 && open($in, q{<}, $ped))
{
while (<$in>)
{
next if (m/^#/);
chomp;
my @data = split m/\t/;
if (scalar @data > 8)
{
for my $j (0 .. (scalar @list_full) - 1)
{
if (defined $list_full[$j] && exists $list_markers{$list_full[$j]})
{
if (!exists $unique{$list_full[$j]}) { $unique{$list_full[$j]} = q{} }
$unique{$list_full[$j]} .= (defined $data[6 + $j * 2] && $data[6 + $j * 2] ne q{0} && defined $data[6 + $j * 2 + 1] && $data[6 + $j * 2 + 1] ne q{0} ? $data[6 + $j * 2] . $data[6 + $j * 2 + 1] : q{});
}
}
}
}
close $in;
}
}
if (defined $markers && -r $markers)
{
if (open($in, q{<}, $markers))
{
# ## cstacks version 1.48; catalog generated on 2018-09-22 22:24:20
# 0 2 1 LSalAtl2s1000 1022 - consensus 0 263_8... CATGTTTATGTATCATTTGTACTATTATAAAACTGAAATATATATTTTTATGTTTTTGTAAAAATGTTTAATTTATTATCTATAACCATTCCTATTCGCC 0 0 0 0
# 0 2 2 LSalAtl2s1000 132767 + consensus 0 263_13... CATGGTAAATTCGTGGTTTACACTATCATTGTCAGACAAAATTGTTGTGAGTACTATCATCTTGAAGCAATGTCGATGCAAGCAATAAGATTGTAAGTAA 0 0 0 0
# # cstacks version 2.3e; catalog generated on 2019-05-20 16:12:34
# 0 1 consensus 0 148_1... CATGCATGTTACTTAAGGGTAGTTTCAGAGGAGCAAGTGGCACATCCCTCCCTCTGCATTTTCAAATGACTGTTGTTGATTTTATTAAAACAAATTCTCCAAATTAAAGTGTAAAATCTTGGTAACCTTTGGAAGTAAAGT 0 0 0
my $seqpos = 5;
my $seqid = 1;
my $line = <$in>;
if ($line =~ m/version 1/) {
$seqpos = 9;
$seqid = 2;
}
while (<$in>)
{
next if (m/^#/);
chomp;
my @data = split m/\t/;
if (scalar @data > $seqpos && defined $data[$seqid] && defined $data[$seqpos] && exists $tmp_list{$data[$seqid]})
{
my $snploc;
if ($tmp_list{$data[$seqid]} =~ m/\_(\d+)/) { $snploc = $1; }
elsif ($tmp_list{$data[$seqid]} =~ m/dDocent.*\.(\d+)/) { $snploc = $1; }
$unique{$tmp_list{$data[$seqid]}} =~ s/(.)(?=.*?\1)//g;
my $seq = (exists $unique{$tmp_list{$data[$seqid]}} && defined $snploc ? substr($data[$seqpos], 0, $snploc) . q{[} . $unique{$tmp_list{$data[$seqid]}} . q{]} . substr($data[$seqpos], $snploc + 1) : $data[$seqpos]);
# to be check for v2+
$list_sequences{$tmp_list{$data[$seqid]}} = (int($loc) > 0 && defined $data[3] && defined $data[4] ? $data[3] . "\t" . $data[4] . "\t" . $seq : $seq);
}
}
close $in;
}
}
elsif (defined $fasta && -r $fasta)
{
if (open($in, q{<}, $fasta))
{
# >dDocent_Contig_12
# TGCAGAAAACACTCTCTCCCCAGACGGGTTTTGATAGAGTAGAACTCCGTCTCGATAGAAAGCAAAGTTGTTATATATATAGTAATAACTAGAGGGATTANNNNNNNNNNCATGTTTAATTTTAAAACATTTTCACACAACCTTAGATGGCTTTTATATTTAATATTCTATTCGAAATTTAAAAGATTTTGTAGCGGTGGATATTTTTGT
my ($seq, $header) = (q{});
while (<$in>)
{
next if /^\s*$/;
chomp;
if (/^>/)
{ # fasta header line
my $h = $_;
$h =~ s/^>//;
if (defined $header && length $seq > 10)
{
if ($header =~ /^(\d+) pos=([^:]+):(\d+):(.) NS=/) {
my ($a, $b, $c, $d) = ($1, $2, $3, $4);
if (exists $tmp_list{$a}) {
my $snploc;
if ($tmp_list{$a} =~ m/\_(\d+)/) { $snploc = $1; }
elsif ($tmp_list{$a} =~ m/dDocent.*\.(\d+)/) { $snploc = $1; }
my $tmp = $unique{$tmp_list{$a}};
$tmp =~ s/(.)(?=.*?\1)//g;
$tmp =~ s/\W//g;
my $seq = (defined $snploc ? substr($seq, 0, $snploc) . q{[} . $tmp . q{]} . substr($seq, $snploc + 1) : $seq);
$list_sequences{$tmp_list{$a}} = $b . "\t" . $c . "\t" . $d . "\t" . $seq;
}
} else {
$list_sequences{$header} = $seq if (exists $tmp_list{$header});
}
$header = $h;
$seq = q{};
}
else { $header = $h }
}