annotate Admixture.pl @ 7:c3ebf600f975 draft default tip

planemo upload
author dereeper
date Wed, 13 Apr 2016 08:07:24 -0400
parents 3f66f32dc5d9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
1 #!/usr/bin/perl
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
2
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
3 use strict;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
4 use Getopt::Long;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
5 use File::Basename;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
6
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
7 my $usage = qq~Usage:$0 <args> [<opts>]
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
8 where <args> are:
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
9 -i, --input <input basename (for PED, FAM, BIM)>
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
10 -o, --output <output>
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
11 -k, --kmin <K min. int>
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
12 -m, --maxK <K max. int>
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
13 -d, --directory <temporary directory>
5
97c9c8daa3c3 planemo upload
dereeper
parents: 4
diff changeset
14 -t, --threshold <threshold admixture proportion for group assignation>
4
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
15 ~;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
16 $usage .= "\n";
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
17
5
97c9c8daa3c3 planemo upload
dereeper
parents: 4
diff changeset
18 my ($input,$output,$kmin,$kmax,$directory,$threshold);
4
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
19
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
20
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
21 GetOptions(
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
22 "input=s" => \$input,
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
23 "output=s" => \$output,
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
24 "kmin=s" => \$kmin,
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
25 "maxK=s" => \$kmax,
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
26 "directory=s" => \$directory,
5
97c9c8daa3c3 planemo upload
dereeper
parents: 4
diff changeset
27 "threshold" => \$threshold,
4
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
28 );
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
29
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
30
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
31 die $usage
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
32 if ( !$input || !$output || !$kmin || !$kmax || !$directory );
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
33
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
34 if ($kmin =~/^(\d+)\s*$/){
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
35 $kmin = $1;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
36 }
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
37 else{
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
38 die "Error: kmin must be an integer\n";
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
39 }
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
40 if ($kmax =~/^(\d+)\s*$/){
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
41 $kmax = $1;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
42 }
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
43 else{
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
44 die "Error: kmax must be an integer\n";
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
45 }
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
46
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
47
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
48
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
49 open(my $P2,"$input.fam");
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
50 my $n = 0;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
51 my $ind_num = 0;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
52 my @individus;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
53 while(<$P2>)
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
54 {
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
55 my ($ind,$other) = split(/ /,$_);
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
56 push(@individus,$ind);
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
57 }
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
58 close($P2);
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
59
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
60 my $basename = basename($input);
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
61
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
62 ###################################
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
63 # launch admixture for different K
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
64 ###################################
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
65 my %errors;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
66 for (my $k = $kmin; $k <= $kmax; $k++)
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
67 {
6
3f66f32dc5d9 planemo upload
dereeper
parents: 5
diff changeset
68 system("admixture --cv $input.bed $k >>$directory/log.$k 2>&1");
4
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
69 my $cv_error_line = `grep -h CV $directory/log.$k`;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
70 if ($cv_error_line =~/: (\d+\.*\d*)$/)
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
71 {
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
72 $errors{$1} = $k;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
73 }
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
74 system("cat $directory/log.$k >>$directory/logs");
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
75 system("echo '\n\n====================================\n\n' >>$directory/logs");
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
76
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
77 open(my $O2,">$basename.$k.final.Q");
5
97c9c8daa3c3 planemo upload
dereeper
parents: 4
diff changeset
78 open(my $O3,">$directory/groups.$k");
4
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
79 open(my $O,"$basename.$k.Q");
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
80 my %hash_groupes;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
81 my %hash_indv;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
82 my %group_of_ind;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
83 my $i = 0;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
84 while (<$O>){
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
85 $i++;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
86 my $line = $_;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
87 $line =~s/\n//g;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
88 $line =~s/\r//g;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
89 my @infos = split(/\s+/,$line);
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
90 my $group = "admix";
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
91 my $ind = $individus[$i];
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
92 for (my $j = 0; $j <$k; $j++){
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
93 my $val = $infos[$j];
5
97c9c8daa3c3 planemo upload
dereeper
parents: 4
diff changeset
94 if ($val > ($threshold/100)){$group = "Q$j";}
4
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
95 }
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
96 if ($ind){
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
97 $hash_indv{$ind} = join(" ",@infos);
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
98 $hash_groupes{$group}{"ind"} .= ",".$ind;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
99 $group_of_ind{$ind} = $group;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
100 }
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
101 }
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
102 close($O);
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
103
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
104 foreach my $group(sort keys(%hash_groupes)){
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
105 my @inds = split(",",$hash_groupes{$group}{"ind"});
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
106 foreach my $ind(@inds){
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
107 if ($ind =~/\w+/){
5
97c9c8daa3c3 planemo upload
dereeper
parents: 4
diff changeset
108 print $O3 "$ind;$group\n";
4
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
109 print $O2 $ind." ".$hash_indv{$ind}. "\n";
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
110 }
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
111 }
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
112 }
5
97c9c8daa3c3 planemo upload
dereeper
parents: 4
diff changeset
113 close($O3);
4
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
114 close($O2);
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
115
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
116 system("cat $basename.$k.final.Q >>$directory/outputs.Q");
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
117 system("echo '\n\n====================================\n\n' >>$directory/outputs.Q");
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
118 system("cat $basename.$k.P >>$directory/outputs.P");
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
119 system("echo '\n\n====================================\n\n' >>$directory/outputs.P");
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
120 }
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
121
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
122 my @sorted_errors = sort {$a<=>$b} keys(%errors);
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
123 my $best_K = $errors{@sorted_errors[0]};
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
124
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
125
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
126 open(BEST1,"$basename.$best_K.final.Q");
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
127 open(BEST2,">$directory/output");
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
128 print BEST2 "<Covariate>\n";
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
129 print BEST2 "<Trait>";
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
130 for (my $j=1;$j<=$best_K;$j++)
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
131 {
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
132 print BEST2 " Q" . $j;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
133 }
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
134 print BEST2 "\n";
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
135 my $i = 0;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
136 while(<BEST1>)
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
137 {
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
138 my $line = $_;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
139 $line =~s/ /\t/g;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
140 print BEST2 $line;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
141 $i++;
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
142 }
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
143 close(BEST1);
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
144 close(BEST2);
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
145
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
146 system("cp -rf $directory/log.$best_K $directory/log");
5
97c9c8daa3c3 planemo upload
dereeper
parents: 4
diff changeset
147 system("cp -rf $directory/groups.$best_K $directory/groups");
4
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
148
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
149
58df6910f1c3 planemo upload
dereeper
parents:
diff changeset
150