calculate TajimaD in perl
#!/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的更多相关文章
- perl实现监控linux
1.使用root用户telnet进入linux系统 2.修改DNS以下两种方法 A.通过setup命令配置dns B.通过在/etc目录下创建resolv.conf文件 3.查看DNS是否配置成功 [ ...
- 精通Perl(第2版)
精通Perl(第2版)(通往Perl大师之路必读经典书籍,体现了一种编程思维,能够帮你解决很多实际的问题) [美]brian d foy(布瑞恩·D·福瓦)著 王兴宇 刘宸宇 译 ISBN 978 ...
- perl
introduction: http://www.yiibai.com/perl/perl_introduction.html functions: http://www.yiibai.com/per ...
- perl学习之路3
Perl编程之路3 标签: perl 列表与数组 Perl里面代表复数的就是列表和数组 列表(list)指的是标量的有序集合, 而数组(array)则是存储列表的变量. 在Perl这两个属于尝尝混 ...
- perl学习之路2
这些主要是从 "小骆驼" 书上粘贴或者摘抄出来的, 个人认为需要记的语法知识 "在某些情况下, 你可能需要在一台机器上写程序, 再传送到另一台机器上运行.这时候, 请使用 ...
- perl学习之路1
一切要从Hollo world开始 公司要用perl....啊, 不会只能自学了, 毕竟是公司啊, 不是学校...公司不学习就滚蛋了...惨惨惨 因为是学习嘛, 感觉开虚拟机比较麻烦所以直接用了个 瘟 ...
- perl 切换 dnspod 域名记录
提供域名,dnspod 账户密码(毕竟dns密码比较重要 不能谁 cat一下都可以看到 需要base64加密),原IP,切换目标IP, #!/bin/perl use warnings; use MI ...
- perl 删除过期文件
#!/usr/bin/perl `find /bak/ >list.txt`; open LIST,"/root/list.txt"; while (<LIST> ...
- 通过远程 http API 来控制 lnmp 环境的重启perl脚本
#!/usr/bin/perl use DBD::mysql; use strict; use warnings; use DBI; use utf8; binmode(STDOUT, ':encod ...
随机推荐
- Python3调用Hadoop的API
前言: 上一篇文章 我学习使用pandas进行简单的数据分析,但是各位...... Pandas处理.分析不了TB级别数据的大数据,于是再看看Hadoop. 另附上人心不足蛇吞象 对故事一的感悟: ...
- python数据结构与算法之问题求解实例
关于问题求解,书中有一个实际的案例. 上图是一个交叉路口的模型,现在问题是,怎么安排红绿灯才可以保证相应的行驶路线互不交错. 第一步,就是把问题弄清楚. 怎么能让每一条行驶路线不冲突呢? 其实,就是给 ...
- .net项目dll内嵌加载
1.将dll文件作为嵌入资源添加到项目: 2.程序入口增加以下代码: public partial class App : Application { private void Application ...
- 一个简单的windows勒索软件分析
根据分析,此病毒是一个勒索软件,通过修改登录用户密码,留下勒索QQ号码向用户索要金钱. 它调用了Kernel32.dll里的WinExec来执行更改用户密码的cmd命令,密码为107289,更改完密码 ...
- linux网络编程基础--(转自网络)
转自 http://www.cnblogs.com/MyLove-Summer/p/5215287.html Linux下的网络编程指的是socket套接字编程,入门比较简单. 1. socket套接 ...
- openssl 检测链路完整
D:\openssl\bin>openssl s_client -connect www.xxxx.com:443
- vBox Arch UEFI LVM安装
Table of Contents 介绍 配置 基础 VirtualBox配置 安装准备 基础 分区 格式化 挂载 安装 选择镜像 安装基本系统 配置 fstab chroot 一些配置 lvm2 网 ...
- dev控件 xtraTabbedMdiManager 如何将关闭子窗体改为收回主窗体内
前言 本文主要讲解 xtraTabbedMdiManager 如何将关闭子窗体改为收回主窗体内,顺便附上Float(浮动)的时候使窗体最大化,及指定只能某一个子窗体能浮动放大. 下面进入正题. 一.首 ...
- P5016 龙虎斗 题解
这题真是*到家了QAQ 我在考场上调了将近75min,总算过了大样例. 首先,我们可以简化这一题,这道题的本质就是让我们找出一个点p2,往那个点上面加上s2个单位的重量,使得以m为中的两边的权值和的差 ...
- 理解UDP协议的首部校验和校验和
reference: https://blog.csdn.net/qiuchangyong/article/details/79945630 https://seanwangjs.github.io/ ...