查找SNP在gen列表中的位置
我有 SNP 数据和基因列表数据。当我与基因列表进行比较时,我正在寻找基因列表数据中 SNP cotain 的位置。例如:
SNP 数据:
Pos_start pos_end 14185 14185 …………
基因列表数据:
5"side(pos_start) 3"sile(pos_end) 1 1527 1920 1777 …………
结果:在基因列表的 16185 位置包含在 SNP 的位置 14185 中。
下面是我的代码,但在对数字进行排序时遇到一些问题。
#!/usr/bin/perl -w
open(POS1,"<posi1.txt"); (I collect two data and save with posi1.txt)
@posi1=<POS1>;
open(list,">list.txt");
@list1=@posi1;
@list2= sort num_last (@list1);
$list2 = join( '', @list2);
print $list2;
print list $list2."\n\n";
close(list);
sub num_last {
my ($num_a, $num_b);
$num_a=$a=~ /^[0-9]/;
$num_b=$b=~ /^[0-9]/;
if ($num_a && $num_b){
return $a<=>$b;
} elsif ($num_a){
return 1;
} elsif ($num_b){
return -1;
} else {
return $a cmp $b;
}
}
如果您能给一些指点,我将不胜感激。
I have SNP data and gen list data. I am looking for the position of SNP cotain in the gen list data when I compare with gen list. For example:
The SNP data :
Pos_start pos_end 14185 14185 .... .....
The gen list data:
5"side(pos_start) 3"sile(pos_end) 1 1527 1920 1777 .... .....
the result: in the position 14185 of SNP contain at the 16185 position of the gen list.
Below is my code but it has some problem in sort the number.
#!/usr/bin/perl -w
open(POS1,"<posi1.txt"); (I collect two data and save with posi1.txt)
@posi1=<POS1>;
open(list,">list.txt");
@list1=@posi1;
@list2= sort num_last (@list1);
$list2 = join( '', @list2);
print $list2;
print list $list2."\n\n";
close(list);
sub num_last {
my ($num_a, $num_b);
$num_a=$a=~ /^[0-9]/;
$num_b=$b=~ /^[0-9]/;
if ($num_a && $num_b){
return $a<=>$b;
} elsif ($num_a){
return 1;
} elsif ($num_b){
return -1;
} else {
return $a cmp $b;
}
}
I would appreciate if you could give some pointers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您的排序子程序不会对您传递的值进行操作。它应该类似于
Than,如果字符串从数字开始,您实际上只能得到字符串中的第一个数字。最好添加跳过所有前导空格,以防万一。
\d+
相当于[0-9]+
,但短两个字符:)。大括号强制list context
,因此$num_a
和$num_b
接收第一个匹配组的内容:(\d+)
。然后,您不需要
<=>
运算符,因为$num_a
和$num_b
应该是字符串,因此您可以简化您的操作条件:不确定,但可能就像
return $a cmp $b
一样简单,但我不确定空var是否在字符串方面小于非空字符串,并且指尖没有perl。因此,最终的 num_last 函数:如果需要反向排序,只需将 my ($a,$b) = @_; 替换为 my ($b,$a) = @_;< /code>
而且,我在没有任何编译器帮助的情况下编写了它,因此其中可能存在一些小错误。
First of all, your sort sub does not operate on values you pass. It should be something like
Than, you are really getting only first digit in a string if the string starts from digit. It's better add skipping all leading whitespaces, just in case.
\d+
is equivalent to[0-9]+
, but two chars shorter :). Braces forcelist context
so,$num_a
and$num_b
receives content of first matched group:(\d+)
.Than, you don't need
<=>
opertor, as$num_a
and$num_b
should be strings, so you can simplify your condition to:Not sure, but it might be as simple as
return $a cmp $b
, but I'm not sure if empty var is stringwise lesser than non-empty string and no perl at fingertips. So, final num_last function:If you need reverse sort, just replace
my ($a,$b) = @_;
withmy ($b,$a) = @_;
And, I've written it without any compiler help, so there might be some minor errors in it.