annotate MDSplot/MDSbasedOnIBSmatrix.pl @ 19:a0a95688cf17 draft

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