免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3885 | 回复: 7
打印 上一主题 下一主题

新手在线求助!比较两个文件符合条件的输出到第三个文件。 [复制链接]

论坛徽章:
3
2015亚冠之武里南联
日期:2015-07-29 12:14:352015年亚冠纪念徽章
日期:2015-08-12 14:28:272015亚冠之德黑兰石油
日期:2015-09-09 17:15:52
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-06-10 15:36 |只看该作者 |倒序浏览
刚接触perl
现在编写程序就会简单的
open()
my %hash
while()
print
close

求高人指点一下面程序!!!先谢过!

问题是:

表1:
chr1        53662616        513
chr1        53662617        524
chr1        53662618        526
chr1        53662619        530
chr1        53662620        530
chr1        53662621        530
chr1        53662622        511
chr3        48895204        3550
chr3        48896063        598
chr3        48896633        1521
chr3        48897059        3438
chr3        48900091        4553

表2:
chr1        CPT2        53662616        53679267
chr2        ACADL        211053685        211089987
chr3        SLC25        48895143        48936227
chr3        ACAD9        128598535        128631450

输出:
chr1        CPT2         XXX   YYY  
chr2        ACADL        XXX YYY  
chr3        SLC25A20   XXX YYY       
chr3        ACAD9        XXX YYY
XXX:
统计表1中符合条件的个数
条件 :表1的第一列和表2的第一列相同,并且表1的第二列在表2的3,4列区间里
YYY:
为符合上述条件的所有表1第三列的加和。

论坛徽章:
3
2015亚冠之武里南联
日期:2015-07-29 12:14:352015年亚冠纪念徽章
日期:2015-08-12 14:28:272015亚冠之德黑兰石油
日期:2015-09-09 17:15:52
2 [报告]
发表于 2015-06-10 15:44 |只看该作者
自己顶一顶

论坛徽章:
3
2015亚冠之武里南联
日期:2015-07-29 12:14:352015年亚冠纪念徽章
日期:2015-08-12 14:28:272015亚冠之德黑兰石油
日期:2015-09-09 17:15:52
3 [报告]
发表于 2015-06-10 16:12 |只看该作者
继续顶

求职 : 软件工程师
论坛徽章:
3
程序设计版块每日发帖之星
日期:2015-10-07 06:20:00程序设计版块每日发帖之星
日期:2015-12-13 06:20:00程序设计版块每日发帖之星
日期:2016-05-05 06:20:00
4 [报告]
发表于 2015-06-10 16:54 |只看该作者
本帖最后由 104359176 于 2015-06-10 17:07 编辑

学生物的弟兄们,Perl 还没让学毕业,就赶鸭子上架干活了,真是同情!

这个要用到这个特性:
  1. Extracting a Range of Lines
  2. #-----------------------------
  3. while (<>) {
  4.     if (/BEGIN PATTERN/ .. /END PATTERN/) {
  5.         # line falls between BEGIN and END in the
  6.         # text, inclusive.
  7.     }
  8. }

  9. while (<>) {
  10.     if ($FIRST_LINE_NUM .. $LAST_LINE_NUM) {
  11.         # operate only between first and last line, inclusive.
  12.     }
  13. }
  14. #-----------------------------
  15. while (<>) {
  16.     if (/BEGIN PATTERN/ ... /END PATTERN/) {
  17.         # line is between BEGIN and END on different lines
  18.     }
  19. }

  20. while (<>) {
  21.     if ($FIRST_LINE_NUM ... $LAST_LINE_NUM) {
  22.         # operate only between first and last line, but not same
  23.     }
  24. }
  25. #-----------------------------
  26. # command-line to print lines 15 through 17 inclusive (see below)
  27. perl -ne 'print if 15 .. 17' datafile

  28. # print out all <XMP> .. </XMP> displays from HTML doc
  29. while (<>) {
  30.     print if m#<XMP>#i .. m#</XMP>#i;
  31. }
  32.    
  33. # same, but as shell command
  34. # perl -ne 'print if m#<XMP>#i .. m#</XMP>#i' document.html
  35. #-----------------------------
  36. # perl -ne 'BEGIN { $top=3; $bottom=5 }  print if $top .. $bottom' /etc/passwd        # previous command FAILS
  37. # perl -ne 'BEGIN { $top=3; $bottom=5 } \
  38. #     print if $. == $top .. $. ==     $bottom' /etc/passwd    # works
  39. # perl -ne 'print if 3 .. 5' /etc/passwd   # also works
  40. #-----------------------------
  41. print if /begin/ .. /end/;
  42. print if /begin/ ... /end/;
  43. #-----------------------------
  44. while (<>) {
  45.     $in_header =   1  .. /^$/;
  46.     $in_body   = /^$/ .. eof();
  47. }
  48. #-----------------------------
  49. %seen = ();
  50. while (<>) {
  51.     next unless /^From:?\s/i .. /^$/;
  52.     while (/([^<>(),;\s]+\@[^<>(),;\s]+)/g) {
  53.         print "$1\n" unless $seen{$1}++;
  54.     }
  55. }
  56. #-----------------------------

复制代码
这是一个教程:
perlexample.chm (347.96 KB, 下载次数: 4)

求职 : 软件工程师
论坛徽章:
3
程序设计版块每日发帖之星
日期:2015-10-07 06:20:00程序设计版块每日发帖之星
日期:2015-12-13 06:20:00程序设计版块每日发帖之星
日期:2016-05-05 06:20:00
5 [报告]
发表于 2015-06-10 18:03 |只看该作者
  1. #!perl

  2. use 5.016;
  3. # 这个模块是为了把多个外置文本放在一个文件中
  4. use Inline::Files;
  5. use List::Util qw(reduce sum);

  6. # 用内嵌文本是为了演示,实际中请使用外置文本
  7. # my @table_one = read_file $file;
  8. # 并加载 File::Slurp 模块
  9. my $table_one = table_array(<TABLEONE>);
  10. my $table_two = table_array(<TABLETWO>);

  11. foreach my $line (@$table_two) {
  12.   my $item = $line->[0];
  13.   my $from = $line->[2];
  14.   my $to = $line->[3];
  15.   my ($xxx, $yyy) = filter_data($table_one, $item, $from, $to);
  16.   say "$item $line->[1] $xxx $yyy";
  17. }

  18. sub filter_data {
  19.   my ($data, $item, $from, $to) = @_;
  20.   my @result;
  21.   foreach my $line (@$data) {
  22.     next unless $line->[0] eq $item;
  23.     if ( +$line->[1] >= $from and +$line->[1] <= $to ) {
  24.       # say $line->[2];
  25.       push @result, $line->[2];
  26.     }
  27.   }
  28.   my $xxx = scalar @result;
  29.   # my $yyy = reduce { $a + $b } @result;
  30.   my $yyy = sum @result;
  31.   return ($xxx, $yyy);
  32. }

  33. sub table_array {
  34.   my @table = @_;
  35.   my @array;
  36.   foreach my $line (@table) {
  37.     next if $line =~ /^$/;
  38.     push @array, [ split(/\s+/, $line) ];
  39.   }
  40.   return [ @array ];
  41. }

  42. __TABLEONE__
  43. chr1        53662616        513
  44. chr1        53662617        524
  45. chr1        53662618        526
  46. chr1        53662619        530
  47. chr1        53662620        530
  48. chr1        53662621        530
  49. chr1        53662622        511
  50. chr3        48895204        3550
  51. chr3        48896063        598
  52. chr3        48896633        1521
  53. chr3        48897059        3438
  54. chr3        48900091        4553

  55. __TABLETWO__
  56. chr1        CPT2        53662616        53679267
  57. chr2        ACADL       211053685       211089987
  58. chr3        SLC25       48895143        48936227
  59. chr3        ACAD9       128598535       128631450
复制代码
output:
  1. chr1 CPT2 7 3664
  2. chr2 ACADL 0
  3. chr3 SLC25 5 13660
  4. chr3 ACAD9 0
复制代码

论坛徽章:
3
2015亚冠之武里南联
日期:2015-07-29 12:14:352015年亚冠纪念徽章
日期:2015-08-12 14:28:272015亚冠之德黑兰石油
日期:2015-09-09 17:15:52
6 [报告]
发表于 2015-06-11 09:37 |只看该作者
真不知道如何感谢你了, !刚用了几天perl,确实很好用,可惜我写的太烂了,只能边做边学了,再次谢谢啦!回复 5# 104359176


   

论坛徽章:
307
程序设计版块每周发帖之星
日期:2016-04-08 00:41:33操作系统版块每日发帖之星
日期:2015-09-02 06:20:00每日论坛发贴之星
日期:2015-09-02 06:20:00程序设计版块每日发帖之星
日期:2015-09-04 06:20:00每日论坛发贴之星
日期:2015-09-04 06:20:00每周论坛发贴之星
日期:2015-09-06 22:22:00程序设计版块每日发帖之星
日期:2015-09-09 06:20:00程序设计版块每日发帖之星
日期:2015-09-19 06:20:00程序设计版块每日发帖之星
日期:2015-09-20 06:20:00每日论坛发贴之星
日期:2015-09-20 06:20:00程序设计版块每日发帖之星
日期:2015-09-22 06:20:00程序设计版块每日发帖之星
日期:2015-09-24 06:20:00
7 [报告]
发表于 2016-09-15 11:24 |只看该作者
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;

  4. my (%hStat, %hData);
  5. while (<>){
  6.     my @aList = split;
  7.     my $id = splice (@aList, 0, 1);
  8.     if (@ARGV){
  9.         push (@{$hStat{$id}}, [@aList]);
  10.         @{$hData{$id}{$aList[0]}}{"Cnt", "Sum"} = (0) x 2;
  11.         next;
  12.     }
  13.     my $ra = $hStat{$id};
  14.     next unless ($ra);
  15.     for (@$ra){
  16.         if ($_->[1] <= $aList[0] and $_->[-1] >= $aList[0]){
  17.             $hData{$id}{$_->[0]}{"Cnt"}++;
  18.             $hData{$id}{$_->[0]}{"Sum"} += $aList[-1];
  19.         }
  20.     }
  21. }

  22. foreach my $id (sort keys %hData){
  23.     print join ("\t", $id, $_, @{$hData{$id}{$_}}{"Cnt", "Sum"}), "\n" for keys %{$hData{$id}};
  24. }
复制代码

perl abc.pl b a
--------------------------------------------------------
chr1    CPT2    7       3664
chr2    ACADL   0       0
chr3    SLC25   5       13660
chr3    ACAD9   0       0

论坛徽章:
0
8 [报告]
发表于 2016-09-16 07:30 |只看该作者
问一下,不是太明白,每条统计的都是针对同一条染色体而言的吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP