免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: aids260
打印 上一主题 下一主题

求个perl程序,生物信息方面的! [复制链接]

论坛徽章:
0
11 [报告]
发表于 2011-03-26 10:17 |只看该作者
回复 9# meihuaqi


    第六列是哈希的关键字吗??

论坛徽章:
0
12 [报告]
发表于 2011-03-26 10:53 |只看该作者
本帖最后由 conn2011 于 2011-03-26 11:00 编辑
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use Data::Dumper;

  4. my %in;
  5. open(IN, "file2path") || die "Cannot open file int.txt\n";
  6. while(<IN>){
  7.             chomp;
  8.             my @line = split(' ',$_);
  9.             $in{$line[1]}{'text'} = $line[3];
  10.             push @{$in{$line[1]}{'value'}},$line[5];
  11.         }
  12. close IN;

  13. my %info;
  14. open(INFO,"file1path") || die "Cannot open file info.txt\n";
  15. while(<INFO>){
  16.             chomp;
  17.             my @line = split(' ',$_);
  18.             $info{$line[0]} = $line[2];
  19.         }
  20. close INFO;

  21. foreach my $key (keys %in){
  22.             my $sum = 0;
  23.             foreach my $value (@{$in{$key}{'value'}}){
  24.                         $sum += $value;
  25.                     }
  26.             $in{$key}{'avg'} = $sum/scalar(@{$in{$key}{'value'}});
  27.             $in{$key}{'time'} = scalar(@{$in{$key}{'value'}});
  28.             my $tot = $info{$key} ? $info{$key} : 0;
  29.             my $margin = $tot - $in{$key}{'avg'};
  30.             printf "+ %s\t%s\t%10s\t%10s\t%s\n",($key,$in{$key}{'text'},$in{$key}{'avg'},$margin,$in{$key}{'time'});
  31.         }
复制代码

论坛徽章:
0
13 [报告]
发表于 2011-03-26 10:59 |只看该作者
求perl 程序!

论坛徽章:
0
14 [报告]
发表于 2011-03-26 11:25 |只看该作者
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;


  4. my %total;
  5. my %file_1;

  6. while (<DATA>)
  7. {
  8.         my @row = split /\s+/;
  9.         if ($row[0] =~ m/\+/)
  10.         {
  11.                 $total{$row[1]}{item}++;
  12.                 $total{$row[1]}{total} += $row[5];
  13.                 $total{$row[1]}{3} = $row[3];
  14.         }
  15.         elsif ($row[0] =~ m/^B/)
  16.         {
  17.                 $file_1{$row[1]}=$row[2];
  18.         }
  19. }


  20. =head
  21. 如果平均数要求取整,则在求 $avg 时使用 int 函数
  22. =cut

  23. foreach my $out (sort keys %total)
  24. {
  25.         my $avg = $total{$out}{total} / $total{$out}{item};
  26.         my $poor;
  27.         if (exists $file_1{$out} && $file_1{$out} > 0)
  28.         {
  29.                 $poor = $file_1{$out} - $avg;
  30.         }
  31.         else
  32.         {
  33.                 $poor = 0;
  34.         }
  35.         print "+\t$out\t$total{$out}{3}\t$avg\t$poor\t$total{$out}{item}\n";
  36. }



  37. __DATA__
  38. +        BGIBMGA000062-TA         FWDP30_FL5_P09.seq        nscaf1071        1318628        1239395        1240559
  39. +        BGIBMGA000062-TA         MFBP02_F_H18.seq        nscaf1071        1318628        1239668        1243526
  40. +        BGIBMGA000064-TA         fdpeP10_F_J11.seq        nscaf1071        1318628        1313192        1316067
  41. +        BGIBMGA000064-TA         MFBP04_F_N06.seq        nscaf1071        1318628        1313197        1316106
  42. +        BGIBMGA000128-TA         FWDP26_FL5_O06.seq        nscaf1108        3459965        1275912        1278501
  43. +        BGIBMGA000129-TA         FWDP02_FL5_F17.seq        nscaf1108        3459965        1283141        1319375
  44. +        BGIBMGA000129-TA         FWDP02_FL5_O19.seq        nscaf1108        3459965        1282859        1319327
  45. +        BGIBMGA000129-TA         FWDP10_FL5_A23.seq        nscaf1108        3459965        1283141        1319395
  46. +        BGIBMGA000129-TA         FWDP31_FL5_I15.seq        nscaf1108        3459965        1283115        1319315
  47. +        BGIBMGA000129-TA         MFBP15_F_I08.seq        nscaf1108        3459965        1283141        1319403
  48. +        BGIBMGA000140-TA         FWDP04_FL5_M21.seq        nscaf1108        3459965        1773552        1776967
  49. +        BGIBMGA000140-TA         FWDP25_FL5_J23.seq        nscaf1108        3459965        1785487        1791348
  50. +        BGIBMGA000154-TA         FWDP03_FL5_B14.seq        nscaf1108        3459965        2355076        2357513
  51. +        BGIBMGA000154-TA         fdpeP09_F_E13.seq        nscaf1108        3459965        2355077        2357478
  52. +        BGIBMGA000155-TA         fdpeP13_F_N09.seq        nscaf1108        3459965        2429759        2436013
  53. +        BGIBMGA000201-TA         MFBP02_F_D11.seq        nscaf1299        357319        349886        350726
  54. +        BGIBMGA000202-TA         MFBP13_F_J17.seq        nscaf1375        11050        7946        8756
  55. +        BGIBMGA000360-TA         fdpeP16_F_J16.seq        nscaf1681        5888100        45406        46743
  56. +        BGIBMGA000360-TA         fdpeP07_F_A13.seq        nscaf1681        5888100        45375        46693
  57. +        BGIBMGA000362-TA         fdpeP08_F_F04.seq        nscaf1681        5888100        90916        94336
  58. BGIBMGA000058-TA           nscaf1071         1083932        1085028
  59. BGIBMGA000059-TA           nscaf1071         1144201        1144993
  60. BGIBMGA000060-TA           nscaf1071         1194454        1203962
  61. BGIBMGA000061-TA           nscaf1071         1233907        1234573
  62. BGIBMGA000062-TA           nscaf1071         1239819        1245122
  63. BGIBMGA000063-TA           nscaf1071         1293757        1306270
  64. BGIBMGA000064-TA           nscaf1071         1313232        1316001
  65. BGIBMGA000065-TA           nscaf1087         2451        9117
  66. BGIBMGA000066-TA           nscaf109         1549        4332
  67. BGIBMGA000067-TA           nscaf1108         3401547        3415278
  68. BGIBMGA000068-TA           nscaf1108         2724200        2728350
  69. BGIBMGA000069-TA           nscaf1108         2546311        2574462
  70. BGIBMGA000070-TA           nscaf1108         2533757        2534560
  71. BGIBMGA000071-TA           nscaf1108         2527802        2528488
  72. BGIBMGA000072-TA           nscaf1108         2442934        2452908
  73. BGIBMGA000073-TA           nscaf1108         2382927        2390096
  74. BGIBMGA000074-TA           nscaf1108         2370811        2374956
  75. BGIBMGA000075-TA           nscaf1108         2350982        2353168
  76. BGIBMGA000076-TA           nscaf1108         2305769        2312842
  77. BGIBMGA000077-TA           nscaf1108         2238841        2239515
  78. BGIBMGA000078-TA           nscaf1108         2207199        2212812
  79. BGIBMGA000079-TA           nscaf1108         2140463        2140972

复制代码
第一次回答有分数的问题, 不知道是不是我拿分呢?

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
15 [报告]
发表于 2011-03-26 11:38 |只看该作者
回复 1# aids260


    try:
  1. perl -lane 'if(/^\+/){$a{$F[1]}+=$F[5];$b{$F[1]}+=1}if(/^BGIB/){$c{$F[0]}=$F[2];$d{$F[0]}=$F[1]}END{for(keys %a){$x=$a{$_}/$b{$_};$y=$c{$_}-$x;if($d{$_}){print "$_, $d{$_}, $x, $y, $b{$_}"}}} ' f1 f2
  2. BGIBMGA000062-TA, nscaf1071, 1239531.5, 287.5, 2
  3. BGIBMGA000064-TA, nscaf1071, 1313194.5, 37.5, 2
复制代码

论坛徽章:
0
16 [报告]
发表于 2011-03-26 13:20 |只看该作者
跪求,求生物信息perl高手!

论坛徽章:
0
17 [报告]
发表于 2011-03-26 14:43 |只看该作者
本帖最后由 aef25u 于 2011-03-27 09:33 编辑

注:one.txt的数据为楼主贴出的第一份数据。
注意:使用本代码时,请做好two.txt数据的备份,因本代码会往two.txt覆盖写入新数据。
  1. use Tie::File;
  2. use Fcntl 'O_RDONLY';

  3. #one.txt read-only :mode => O_RDONLY
  4. tie @arrOne, 'Tie::File', 'one.txt' ,autochomp => 1,mode => O_RDONLY or die "Problem tying @array: $!";
  5. tie @arrTwo, 'Tie::File', 'two.txt' ,autochomp => 1  or die "Problem tying @array: $!";

  6. my %One_3col,my %One_2col;
  7. foreach my $strOne (@arrOne){
  8. my @oneArr=split("[ \t]+",$strOne);
  9. $One_2col{$oneArr[0]}=$oneArr[1];
  10. $One_3col{$oneArr[0]}=$oneArr[2];
  11. }

  12. my %hashSum,%hashNum,%hashAvg;
  13. foreach my $strTwo (@arrTwo){
  14. my @twoArr=split("[ \t]+",$strTwo);
  15. $hashSum{$twoArr[1]}+=$twoArr[5];
  16. $hashNum{$twoArr[1]}++;
  17. $hashAvg{$twoArr[1]}=int($hashSum{$twoArr[1]}/$hashNum{$twoArr[1]});
  18. }


  19. my %hasResult,$subNum;
  20. foreach my $key (keys %hashAvg){
  21. if (exists $One_3col{$key}){
  22. $subNum=$One_3col{$key}-$hashAvg{$key};
  23. }
  24. $hasResult{$key}="+       $key       $One_2col{$key}       $hashAvg{$key}       $subNum       $hashNum{$key}";
  25. }

  26. @arrTwo=();
  27. my $i=0;
  28. for my $key (keys %hasResult){
  29. $arrTwo[$i]=$hasResult{$key};
  30. $i++;
  31. }

  32. untie @arrOne;
  33. untie @arrTwo;
复制代码

论坛徽章:
0
18 [报告]
发表于 2011-03-26 15:17 |只看该作者
到底是看第一个文件中的基因名出现次数还是第二个文件中的???
回复 1# aids260

论坛徽章:
0
19 [报告]
发表于 2011-03-26 15:24 |只看该作者
本帖最后由 aef25u 于 2011-03-26 15:33 编辑

回复 17# dgtnk

很明显是两文件中同时存在的基因名在第二个文件中出现的次数,第一个文件的基因名是唯一的。楼主问题描述不清。
看楼主的需求确实很伤脑细胞....

到我blob来看吧,我发了博文,重新整理了下楼主需求的描述
http://blog.chinaunix.net/space. ... =blog&id=191423

论坛徽章:
0
20 [报告]
发表于 2011-03-27 00:25 |只看该作者
回复 16# aef25u


    哥们,程序我用了下后,我file2都清零了!   还有,file2的不能以第6列的值为关键字,第二列位置定义哈希,以为第六列的值有相等的。你能再调试下程序吗??
  1. +        BGIBMGA000648-TA         MFBP22_F_C21.seq        nscaf1690        7983491        2927332        2942206
  2. +        BGIBMGA000648-TA         MFBP22_F_M19.seq        nscaf1690        7983491        2927328        2942181
  3. +        BGIBMGA000648-TA         MFBP22_F_O19.seq        nscaf1690        7983491        2927328        2942149
  4. +        BGIBMGA000648-TA         MFBP21_F_E08.seq        nscaf1690        7983491        2927328        2942222
  5. +        BGIBMGA000648-TA         MFBP21_F_F16.seq        nscaf1690        7983491        2927328        2942207
  6. +        BGIBMGA000648-TA         MFBP23_F_M16.seq        nscaf1690        7983491        2927328        2942190
  7. +        BGIBMGA000648-TA         MFBP26_F_G19.seq        nscaf1690        7983491        2927328        2942178
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP