annotate MDSbasedOnIBSmatrix.pl @ 13:734a3572c1d6 draft

Uploaded
author dereeper
date Tue, 08 Jan 2019 08:45:34 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
1 #!/usr/bin/perl
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
2
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
3 use strict;
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
4 use Getopt::Long;
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
5 use Bio::SeqIO;
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
6
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
7 my $PLINK_EXE= "plink";
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
8
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
9 my $usage = qq~Usage:$0 <args> [<opts>]
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
10 where <args> are:
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
11 -i, --in <input>
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
12 -o, --out <output>
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
13 ~;
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
14 $usage .= "\n";
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
15
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
16 my ($in,$out);
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
17
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
18
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
19 GetOptions(
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
20 "in=s" => \$in,
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
21 "out=s" => \$out
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
22 );
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
23
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
24 die $usage
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
25 if ( !$in || !$out);
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
26
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
27
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
28 my $plink_command = $PLINK_EXE . " --vcf $in --cluster --allow-extra-chr --matrix --mds-plot 3 --out $out >>$in.plink.log 2>&1";
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
29 system($plink_command);
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
30
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
31 my @individuals = ();
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
32
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
33 my %populations;
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
34 if (-e "$in.individual_info.txt")
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
35 {
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
36 open(my $I,"$in.individual_info.txt");
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
37 while(<$I>)
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
38 {
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
39 my $line = $_;
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
40 $line =~s/\n//g;
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
41 $line =~s/\r//g;
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
42 my ($ind,$pop) = split(/;/,$line);
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
43 $populations{$ind} = $pop;
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
44 }
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
45 close($I);
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
46 }
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
47
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
48
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
49 my $line_ind = `grep CHROM $in`;
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
50 $line_ind =~s/\n//g;$line_ind =~s/\r//g;
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
51 my @tab = split(/\t/,$line_ind);
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
52 for (my $i = 9; $i <= $#tab; $i++){
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
53 push(@individuals,$tab[$i]);
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
54 }
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
55
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
56 open(my $OUT,">$out.mds_plot.txt");
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
57 my $go = 0;
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
58 print $OUT "Pop sample val1 val2 val3\n";
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
59 open(my $O,"$out.mds");
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
60 my $numline = 0;
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
61 while(<$O>)
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
62 {
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
63 if ($go)
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
64 {
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
65 my $line = $_;
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
66 $line =~s/\n//g;
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
67 $line =~s/\r//g;
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
68 my @i = split(/\s+/,$line);
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
69 if ($line =~/^ /)
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
70 {
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
71 #my $ind = $i[1];
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
72 my $ind = $individuals[$numline];
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
73 my $pop = "Pop1";
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
74 #if ($ind=~/^d/){$pop="Pop2";}
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
75 if ($populations{$ind})
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
76 {
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
77 $pop = $populations{$ind};
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
78 }
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
79 print $OUT "$pop $ind ".$i[4]." ".$i[5]." ".$i[6]."\n";
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
80 }
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
81 if ($line =~/^\w/)
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
82 {
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
83 #my $ind = $i[0];
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
84 my $ind = $individuals[$numline];
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
85 my $pop = "Pop1";
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
86 #if ($ind=~/^d/){$pop="Pop2";}
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
87 if ($populations{$ind})
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
88 {
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
89 $pop = $populations{$ind};
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
90 }
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
91 print $OUT "$pop $ind ".$i[3]." ".$i[4]." ".$i[5]."\n";
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
92 }
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
93 $numline++;
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
94 }
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
95 if (/C1/){$go = 1;}
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
96 }
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
97 close($O);
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
98 close($OUT);
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
99
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
100
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
101 my $j = 0;
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
102 open(my $IBS,">$out.ibs_matrix.txt");
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
103 print $IBS "Individuals " . join("\t",@individuals)."\n";
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
104 open(my $O2,"$out.mibs");
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
105 while(<$O2>)
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
106 {
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
107 my $line = $_;
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
108 $line =~s/\n//g;
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
109 $line =~s/\r//g;
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
110 my @i = split(/\s+/,$line);
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
111 print $IBS $individuals[$j]. " ". join("\t",@i)."\n";
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
112 $j++;
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
113 }
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
114 close($O2);
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
115 close($IBS);
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
116
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
117
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
118
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
119
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
120
734a3572c1d6 Uploaded
dereeper
parents:
diff changeset
121