- 论坛徽章:
- 1
|
本帖最后由 飞越地平线 于 2013-09-04 14:43 编辑
回复 7# jason680
如下,写的不好看。
目录B下的文件名称是像bed_chr_1.bed, bed_chr_2.bed这样的。- #usr/bin/perl
- use warnings;
- use strict;
- my ($dir, $dir2);
- my %fdir;
- my %fh;
- $dir = "G:\\Data";
- $dir2 = "G:\\排序后文件";
- my $test = "G:\\test";
- $dir = $test;
- my @file = glob "$dir\\*";
- my @bed = glob "$dir2\\*";
- #哈希%fdir存储键值对(名称,路径)。哈希%fh存储键值对(名称,文件句柄)。
- map { my $name = $_; $name =~ s/.*bed_(.+)_(.+)\.bed/$1$2/; $fdir{$name} = $_;} @bed;
- map {open $fh{$_},"<",$fdir{$_} or die "Can't open file $_!\n"; } keys %fdir;
-
- for my $in(@file){
- open IN,"<","$in" or die "Can't open $in!\n";
- my @array;
- map {chomp;next if(/^\s*$/);push @array,[split('\t',$_)]} <IN>;
- close IN;
- open OUT,">","$in.snp";
- for my $gene(@array){ #@array记录目录A下的文件(文件不大)
- print OUT $gene->[3]; # $gene->[3]为每行的名称;根据$gene->[0]来确定要打开目录B下的具体哪个文件;($gene->[1],$gene->[2])就是($min,$max)。
- next unless ($gene->[0] =~ /chr/g);
- next unless(exists $fh{$gene->[0]});
- my $filehandle = $fh{$gene->[0]};
- seek $filehandle,0,0;
- my $num=0;
- while(<$filehandle>){ #文件句柄$filehandle为目录B下文件的文件句柄。
- next if index($_,$gene->[0])== -1;
- chomp;
- my @array2 = split '\t',$_;
- last if ($gene->[2] < $array2[2]);
- $num++ if ($gene->[0] eq $array2[0] and $array2[2]>=$gene->[1]); #这里统计符合条件的行数。
- }
- print OUT "\t".$num."\n";
- }
-
- close OUT;
- }
复制代码 |
|