I already know the Bash AWK solution and also R code for the question that I am asking. But, my file is so big and with R it takes very long time and I am afraid of AWK's mistake for this computation, so I wonder if there is a Perl or Python solution for my problem.
I have a population allele count data, and it looks like this:
1 0 0 0 0 0 0 0 0 0 1 2 1 0 0 0 0
0 2 0 0 0 0 0 0 0 0 0 4 0 2 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 2 0 0 2 1 0 0 0 0 2 4 0 0 0 2 0
Columns are populations and rows are SNPs. I have two rows per SNP (one row for the number of copies of allele "A" in each population and one row for the number of copies of allele "a").
In the example above, the first and second rows are number of alleles for the SNP1, the third and fourth rows are alleles for SNP 2, and like this until SNP20000000.
I want to calculate population allele frequency per SNP for all populations: frequency of allele A at SNP1 in population 1 = number of copies of allele A in population/total number of A/a gene copies in population and frequency of allele a at SNP1 in population 1 = number of copies of allele a in population/total number of A/a gene copies in the population. This means for each SNP number of copies of each allele should be divided to sum of the number of copies of allele "A" and "a" for each population. This is my desired output:
1 0 0 0 0 0 0 0 0 0 1 0.333333 1 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0.666667 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 0 0 1 1 0 0 0 0 1 1 0 0 0 1 0
As I said, I have an R and Bash solution, but is there a way to do this estimation in Perl or Python?
Here is the Bash solution, but I do not know how to translate it into Perl or Python? I am learning Perl...
awk '{for(i = 1; i <= NF; i++)
tally[i] += $i};
(NR%2) == 1
{for(i=1; i<=NF; i++)
allele1[i] = $i};
(NR%2) == 0
{for(i = 1; i <= NF; i++)
allele2[i] = $i;
for(i = 1; i <= NF; i++)
if(tally[i] == 0)
tally[i] = 1;
for(i = 1; i <= NF; i++)
printf allele1[i]/tally[i]"\t";
printf "\n";
for(i = 1; i <= NF; i++)
printf allele2[i]/tally[i]"\t";
printf "\n";
for(i = 1; i <= NF; i++)
tally[i]=0}' MyData |
sed 's/\t$//g'
awk
solution $\endgroup$