查找SNP在gen列表中的位置

发布于 2024-11-30 12:07:41 字数 986 浏览 1 评论 0原文

我有 SNP 数据和基因列表数据。当我与基因列表进行比较时,我正在寻找基因列表数据中 SNP cotain 的位置。例如:

  1. SNP 数据:

    Pos_start pos_end 
    14185 14185      
    …………   
    
  2. 基因列表数据:

    5"side(pos_start) 3"sile(pos_end)
      1 1527      
      1920 1777 
      ………… 
    
  3. 结果:在基因列表的 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:

  1. The SNP data :

    Pos_start pos_end 
    14185     14185      
    ....      .....   
    
  2. The gen list data:

    5"side(pos_start)  3"sile(pos_end)
      1                  1527      
      1920               1777 
      ....               ..... 
    
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜扑 2024-12-07 12:07:41

首先,您的排序子程序不会对您传递的值进行操作。它应该类似于

sub num_last {
    my ($num_a, $num_b);
    my ($a,$b) = @_;
    ....
}

Than,如果字符串从数字开始,您实际上只能得到字符串中的第一个数字。最好添加跳过所有前导空格,以防万一。

($num_a) = $a =~ /^\s*(\d+)/;
($num_b) = $b =~ /^\s*(\d+)/;

\d+ 相当于 [0-9]+,但短两个字符:)。大括号强制 list context,因此 $num_a$num_b 接收第一个匹配组的内容:(\d+)

然后,您不需要 <=> 运算符,因为 $num_a$num_b 应该是字符串,因此您可以简化您的操作条件:

if (!$num_a)
    return -1;
if (!$num_b)   
    return 1;
return $a cmp $b;

不确定,但可能就像return $a cmp $b一样简单,但我不确定空var是否在字符串方面小于非空字符串,并且指尖没有perl。因此,最终的 num_last 函数:

sub num_last{
    my ($num_a, $num_b);
    my ($a,$b) = @_;

    ($num_a) = $a =~ /^\s*(\d+)/;
    ($num_b) = $b =~ /^\s*(\d+)/;

    if (!$num_a)
        return -1;
    if (!$num_b)   
        return 1;
    return $a cmp $b;
}

如果需要反向排序,只需将 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

sub num_last {
    my ($num_a, $num_b);
    my ($a,$b) = @_;
    ....
}

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.

($num_a) = $a =~ /^\s*(\d+)/;
($num_b) = $b =~ /^\s*(\d+)/;

\d+ is equivalent to [0-9]+, but two chars shorter :). Braces force list 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:

if (!$num_a)
    return -1;
if (!$num_b)   
    return 1;
return $a cmp $b;

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:

sub num_last{
    my ($num_a, $num_b);
    my ($a,$b) = @_;

    ($num_a) = $a =~ /^\s*(\d+)/;
    ($num_b) = $b =~ /^\s*(\d+)/;

    if (!$num_a)
        return -1;
    if (!$num_b)   
        return 1;
    return $a cmp $b;
}

If you need reverse sort, just replace my ($a,$b) = @_; with my ($b,$a) = @_;

And, I've written it without any compiler help, so there might be some minor errors in it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文