annotate Admixture.pl @ 6:3f66f32dc5d9 draft

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