搜索
查看: 842|回复: 1

求助用Perl脚本统计等位基因表内各染色体等位基因分布

[复制链接]

2

主题

2

帖子

155

积分

注册会员

Rank: 2

积分
155
发表于 2022-10-27 15:06:51 | 显示全部楼层 |阅读模式
输入文件内容:
染色体  基因起始位置  序列编号
chr1        8235679        tig00016442               
chr1        8236417        tig00016442        tig00000934
chr2        8291504        tig00016587        tig00000934       
chr2        8421019        tig00016587        tig00000934        tig00016442
chr3        8491339        tig00016442               
chr4        8599071        tig00016587        tig00000934        tig00016442
.......
chr12 13897        tig00016827        tig00000925        tig00017521
每一行含义:如果有一个tig序列编号,在该染色体该位置上有一个等位基因序列;如果有两个tig序列编号,在该染色体该位置上有两个等位基因序列;如果有三个tig序列编号,在该染色体该位置上有三个等位基因序列;

结果要求:统计每条染色体上,含有多少个 一个等位基因(1-allele)、两个等位基因(2-allele)、三个等位基因(3-allele)的位置。
示例数据结果
chr   1-allele 2-allele 3-allele
1      1            1         0
2      0            1         1
3      1            0         0
4      0            0          1
......
12    0             0          1
回复

使用道具 举报

0

主题

1

帖子

26

积分

新手上路

Rank: 1

积分
26
发表于 2023-2-4 13:20:14 | 显示全部楼层
#!/usr/bin/perl -w
use strict;
die usage() unless @ARGV==2;
my %count;
my $max=0;
open Allele,$ARGV[0] or die;
while(<Allele>){
   chomp;
   my @allele=split;
   my $chr=shift @allele;
   shift @allele;
   my $number =@allele;
   ++$count{$chr}->{$number};
   $max=$number if $number>$max;
}
open Count,">$ARGV[1]" or die;
print Count "chr";
foreach(1..$max){
   print Count "\t$_-allele";
}
print Count "\n";
foreach my $chr(sort keys %count){
   print Count "$chr";
   foreach my $number(1..$max){
      if(exists $count{$chr}->{$number}){
         print Count "\t$count{$chr}->{$number}";
      }
      else{
         print Count "\t0";
      }
   }
   print Count "\n";
}

sub usage{
   my $die=<<DIE;
   usage: perl $0 <Allele> <Count>
DIE
}
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|生信技能树 ( 粤ICP备15016384号  

GMT+8, 2023-3-25 13:20 , Processed in 0.114038 second(s), 31 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.