#!/usr/bin/perl
use strict;
use warnings;
=pod
---------------------------------------
this perl script is used to compute tajima's D

Former of tajimaD.tmp.txt is :
chr position #sampled #derived
1 256 12 18
1 12124 14 16

Former of vcf is normal vcf.

Former of population:
accession1
accession2
...

----------------------------------------
=cut

die "\nUsage: compute tajima's D;\ncomands:\nperl $0 vcf.file population tajima.out\n\n" if (@ARGV != 3);

my $snpfile = shift();
my $pop=shift();
my $tajima_outfile = shift();

my $tempfile="tajimaD.tmp.txt";
my $sample = &INPUT($snpfile,$pop,$tempfile);

open AA, $tempfile or die $!;
open BB, ">$tajima_outfile" or die $!;

my $window = 10000;#window site 10 kb
my $bin = 1; # how long two windows is overlapped, 1 means no overlap, 0.8 means 20% is overlap which means 2 kb overlapped with 10 kb window size.
my $cout = int ( 1 / $bin ); #
my $step = $window * $bin; #step size, the step is equal to window size that means no overlap between adjacent windows

#my $sample = 0;
my %filter = ();
my %pi = ();
my %snp = ();

my $chr_name;
my $chrlen = 0;
while ( <AA> ) {
chomp;
my @tt = split;
my @line = @tt;
$chr_name = $line[0];
$chrlen = $tt[1];
# $sample = ($tt[2]+$tt[3]) / 2;
my $k = int ( $tt[1] / $step );
$filter{$k} ++;
$pi{$k} += &pi ( $line[2], $line[3] );
if ( ($line[2]*$line[3]) != 0 ) { $snp{$k}++; }
}
close AA;

print BB "chrname\tposition\tpi\ttajima's.D\tsum.snp\tfilter.site\n";

my $window_num = int ( $chrlen / $step ); #the number of steps
for ( my $i=0; $i<=($window_num-$cout); $i++ ) {
my $sum_pi = 0;
my $sum_snp = 0;
my $filter_site = 0;

my $end = $i + $cout - 1;
for ( my $aa=$i; $aa<=$end; $aa++ ){

if ( !defined($pi{$aa}) ) { $pi{$aa} = 0; }
if ( !defined($snp{$aa}) ) { $snp{$aa} = 0; }
if ( !defined($filter{$aa}) ) { $filter{$aa} = 0; }

$sum_pi += $pi{$aa};
$sum_snp += $snp{$aa};
$filter_site += $filter{$aa};
}

next if ($sample<=1);
my $d = &tajima( $sample, $sum_pi, $sum_snp );
my $id = ($i + 1) * $step;
# my $mean_pi = 0;
my $theta = 0;
# if ( $filter_site ) { $mean_pi = $sum_pi / $filter_site; }
$theta = $sum_pi /$window;
print BB "$chr_name\t$id\t$theta\t$d\t$sum_snp\t$filter_site\n";
}
close BB;
`rm $tempfile`;
sub pi {
my $a = $_[0];
my $b = $_[1];
if ( $a == 0 || $b == 0 ) { return 0; }
my $pi = ( 2 * $a * $b ) / ( ($a+$b) * ($a+$b-1) );
return $pi;
}

sub tajima {
my $n = $_[0];
my $pi = $_[1];
my $t = $_[2];
my $a1;
my $a2;
for ( my $i=1; $i<$n; $i++ ){
$a1 += (1 / $i);
$a2 += (1 / ($i*$i));
}
my $b1 = ($n + 1) / ( 3 * ($n-1) );
my $b2 = 2 * ($n*$n + $n + 3) / (9 * $n * ($n-1));
my $c1 = $b1 - (1 / $a1);
my $c2 = $b2 - ($n + 2) / ($a1 * $n) + $a2 / ($a1*$a1);
my $e1 = $c1 / $a1;
my $e2 = $c2 / ($a1*$a1 + $a2);
if ( $t == 0) { return "NA"; next; }
my $d = ( $pi - ($t/$a1) ) / sqrt ( $e1 * $t + $e2 * $t * ($t-1) );

return $d;
}

sub INPUT{
my $chr=$_[0];
my $pop=$_[1];
my $out=$_[2];
open POP,"$pop";
my %id=();
while(<POP>){
chomp;
my @line=split;
$id{$line[0]}="";
}
close POP;

my $materialnumber=0;
open IN, "$chr";
my %all=();
$all{0}=0;$all{1}=0;
my @array=();
open OUT,">$out";
while(<IN>){
chomp;
if ($_=~/^##/){
}elsif($_=~/^#CHROM/){
my @head=split;
for my $nb (0..$#head){
if(defined $id{$head[$nb]}){
print "$head[$nb]\t$nb\n";
push @array,$nb;
}
}
my $stat=@array;
$materialnumber=$stat;
print "\nTotal material number is: $stat\n\n";
}else{
my @hd=split;
for my $acc (@array){
if($hd[$acc]=~/(\d)\/(\d)/){
$all{$1}++;
$all{$2}++;
}
}
print OUT "$hd[0]\t$hd[1]\t$all{0}\t$all{1}\n";
$all{0}=0;$all{1}=0;
}
}
close IN;
close OUT;

return($materialnumber);
}

calculate TajimaD in perl的更多相关文章

  1. perl实现监控linux

    1.使用root用户telnet进入linux系统 2.修改DNS以下两种方法 A.通过setup命令配置dns B.通过在/etc目录下创建resolv.conf文件 3.查看DNS是否配置成功 [ ...

  2. 精通Perl(第2版)

    精通Perl(第2版)(通往Perl大师之路必读经典书籍,体现了一种编程思维,能够帮你解决很多实际的问题) [美]brian d foy(布瑞恩·D·福瓦)著   王兴宇 刘宸宇 译 ISBN 978 ...

  3. perl

    introduction: http://www.yiibai.com/perl/perl_introduction.html functions: http://www.yiibai.com/per ...

  4. perl学习之路3

    Perl编程之路3 标签: perl 列表与数组   Perl里面代表复数的就是列表和数组 列表(list)指的是标量的有序集合, 而数组(array)则是存储列表的变量. 在Perl这两个属于尝尝混 ...

  5. perl学习之路2

    这些主要是从 "小骆驼" 书上粘贴或者摘抄出来的, 个人认为需要记的语法知识 "在某些情况下, 你可能需要在一台机器上写程序, 再传送到另一台机器上运行.这时候, 请使用 ...

  6. perl学习之路1

    一切要从Hollo world开始 公司要用perl....啊, 不会只能自学了, 毕竟是公司啊, 不是学校...公司不学习就滚蛋了...惨惨惨 因为是学习嘛, 感觉开虚拟机比较麻烦所以直接用了个 瘟 ...

  7. perl 切换 dnspod 域名记录

    提供域名,dnspod 账户密码(毕竟dns密码比较重要 不能谁 cat一下都可以看到 需要base64加密),原IP,切换目标IP, #!/bin/perl use warnings; use MI ...

  8. perl 删除过期文件

    #!/usr/bin/perl `find /bak/ >list.txt`; open LIST,"/root/list.txt"; while (<LIST> ...

  9. 通过远程 http API 来控制 lnmp 环境的重启perl脚本

    #!/usr/bin/perl use DBD::mysql; use strict; use warnings; use DBI; use utf8; binmode(STDOUT, ':encod ...

随机推荐

  1. unity室内外VR漫游

    这是一个用Unity 做的VR漫游小场景.

  2. vsftpd详细配置

    vsftpd配置文件详解 1.默认配置: 1>允许匿名用户和本地用户登陆. anonymous_enable=YES local_enable=YES 2>匿名用户使用的登陆名为ftp或a ...

  3. 跟随我在oracle学习php(11)

    数组专题 数组遍历: 1,普通for循环,经常用的数组遍历 var arr = [1,2,0,3,9]; for ( var i = 0; i <arr.length; i++){ consol ...

  4. springmvc核心技术

    目录 异常处理 类型转换器 数据验证 文件上传与下载 拦截器 异常处理 Spring MVC中, 系统的DAO, Service, Controller层出现异常, 均通过throw Exceptio ...

  5. js jquery 正则去空字符

    1.正则去空字符串: var str1=" a b c "; var strtrim=str1.replace(/\s/g,""); 2.js去前后空字符串: ...

  6. 实时输出topk最频繁变动的股价

    网上看到了一道关于bloomburg的面试题,follow 评论的思路 自己试着写了一个HashHeap的实现. 基本思路是维护一个大小为K的最小堆,里面是topK股价变动的公司ID(假设ID是Int ...

  7. SolrCloud7.4(Jetty容器)+mysql oracle 部署与应用

    SolrCloud7.4(Jetty容器)搭建 1.Zookeeper搭建 版本:zookeeper-3.4.10.tar.gz 1.把zookeeper安装包上传到服务器 2.zookeeper解压 ...

  8. Spark Streaming的容错和数据无丢失机制

    spark是迭代式的内存计算框架,具有很好的高可用性.sparkStreaming作为其模块之一,常被用于进行实时的流式计算.实时的流式处理系统必须是7*24运行的,同时可以从各种各样的系统错误中恢复 ...

  9. 第2次作业 -- 熟悉 JUnit 测试

    2.1 Mooctest 使用心得 Mooctest很方便,可以即时测评自己写的测试代码,获得覆盖率和报告,不需要自己安装配置环境 而且安装配置插件的环境也很简单,可以专注于测试本身 2.2 Juni ...

  10. HTML中data-* 属性

    使用 data-* 属性来嵌入自定义数据: <ul><li data-animal-type="bird">Owl</li><li dat ...