关于Mantel Test

算地理距离与遗传距离相关性,扯到了Mantel Test,那些中文文献里面全说用一个老软件:TFPGA v1.3(目前我已下下来了,正准备凑数据去算下),心里有些排斥过于老的软件,于是想看R里面谁能算。

初步Google发现了ade4这个包,已尝试,貌似都得转换成dist类型。

又在这里:http://blog.sina.com.cn/s/blog_4b678be40100o464.html看到了ape这个包,还没试;

另外又瞅到vegan包里面貌似也有相关函数。

可以试试多方验证。

#======================================================

format4tfpga.pl

#!/usr/bin/perl
use strict;
use warnings;
# 准备数据供TFPGA v1.3使用
# 另外,生成地理距离、遗传距离杂合矩阵

# files
my $f1 = 'gen.txt'; # 遗传距离矩阵
my $f2 = 'geo.txt'; # 地理距离矩阵
my $out = 'for_tfpga_0.txt';
my $geo_gen = 'geo_gen_matrix.txt'; # 附带生成杂和矩阵

my $str = '***';

open F1, '<', $f1 or die "Can't open '$f1': $!";
open F2, '<', $f2 or die "Can't open '$f2': $!";
open OUT, '>', $out or die "Can't write to '$out'";
open GG, '>', $geo_gen or die "Can't write to '$geo_gen'";

# matrix
# %mat1 => genic distances; %mat2 => geographic distances
my (%mat1, %mat2);
my $m = 0;
while (<F1>) {
	next if /^\s+$/;
	$m++;
	my $n = 0;
	for my $x (split) {
		$n++;
		$mat1{$m}{$n} = $x;
	}
}
close F1;

$m = 0;
while (<F2>) {
	next if /^\s+$/;
	$m++;
	my $n = 0;
	for my $x (split) {
		$n++;
		$mat2{$m}{$n} = $x / 1000;
	}
}
close F2;

for my $i (1 .. ($m - 1)) {
	for my $j (($i + 1) .. $m) {
		print OUT "$i,$j, $mat1{$i}{$j},$mat2{$i}{$j}\n";
	}
}
print OUT "0,0, 0,0\n";
close OUT;

for my $i (1 .. $m) {
	my @out;
	if ($i == 1) {
		@out = ($str, @{$mat2{$i}}{2 .. $m});
	} elsif ($i == $m) {
		@out = (@{$mat1{$i}}{1 .. ($m-1)}, $str);
	} else {
		@out = (@{ $mat1{$i} }{1 .. ($i-1)}, $str, @{ $mat2{$i} }{ ($i+1 ) .. $m });
	}
	my $line = join "\t", @out;
	print GG "$line\n";
}
close GG;

print "Done!\n";

system 'pause';



你可能感兴趣的:(perl,r,Mantel_Test,TFPGA)