免费注册 查看新帖 |

Chinaunix

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

perl统计替换 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-03-15 11:21 |只看该作者 |倒序浏览
test.zip (5.03 KB, 下载次数: 7)

文件1.txt
Entrez_Gene_Id        Tumor_Sample_Barcode
182        TCGA-02-0043-01A-01W
220        TCGA-02-0089-01A-01W
220        TCGA-02-0028-01A-01W
286        TCGA-02-0083-01A-01W
286        TCGA-02-0028-01A-01W
287        TCGA-02-0015-01A-01W-0318-08
287        TCGA-08-0354-01A-01W-0318-08
287        TCGA-02-0064-01A-01W-0206-08
301        TCGA-02-0028-01A-01W
310        TCGA-02-0083-01A-01W-0206-08
324        TCGA-02-0015-01A-01W-0318-08
472        TCGA-02-0010-01A-01W-0189-08
472        TCGA-02-0114-01A-01W-0206-08
473        TCGA-02-0114-01A-01W
477        TCGA-02-0083-01A-01W
529        TCGA-02-0083-01A-01W-0206-08
——————————————————————————————————

文件2.txt

Term         Genes
hsa05200athways in cancer        1436, 6469, 5579, 6772, 1956, 5578, 2033, 862, 3082, 1029, 2335, 867, 7472, 3320, 2956, 7048, 4233, 5159, 5601, 2064, 5728, 675, 2735, 5156, 1441, 3815, 324, 2322, 4193, 5925, 2737, 5727, 7157, 2260, 5290, 4089, 5294, 5295, 999
hsa04510:Focal adhesion        5579, 1956, 5578, 3082, 2335, 1289, 4233, 5159, 5601, 57144, 2064, 1281, 5728, 5156, 3371, 2321, 85366, 9564, 3791, 5170, 3690, 1301, 3611, 1793, 5290, 1277, 2909, 5294, 5295, 7057
hsa05218:Melanoma        5728, 5156, 1956, 3082, 1029, 4193, 5925, 7157, 2260, 5290, 5294, 5295, 4233, 5159, 999
hsa05213:Endometrial cancer        5290, 2064, 5728, 1956, 2309, 5170, 324, 3611, 5294, 5295, 7157, 999
hsa05214:Glioma        5728, 5579, 5156, 1956, 5578, 1029, 4193, 5925, 7157, 5290, 5294, 5295, 5159
hsa05215rostate cancer        5728, 2064, 5156, 1956, 2033, 5170, 4193, 5925, 7157, 2260, 5290, 3320, 5294, 5295, 5159
hsa05223:Non-small cell lung cancer        5290, 2064, 5579, 5578, 1956, 2309, 1029, 5170, 5925, 5294, 5295, 7157
hsa05212ancreatic cancer        5601, 2064, 675, 6772, 1956, 1029, 5925, 7157, 5290, 4089, 7048, 5294, 5295
hsa05210:Colorectal cancer        5601, 5156, 1956, 324, 7157, 5290, 4089, 2956, 7048, 5294, 5295, 4233, 5159
hsa04070hosphatidylinositol signaling system        5290, 5728, 5579, 5578, 3710, 79837, 5287, 8527, 5288, 5294, 5286, 5295
————————————————————————————————————————————————————————————————————

目的:
Term        Genes
hsa05200athways in cancer        TCGA-02-0084-01A-01W TCGA-08-0390-01A-01W TCGA-06-0185-01A-01W-0254-08 TCGA-06-0185-01A-01W TCGA-02-0010-01A-01W-0189-08 TCGA-02-0083-01A-01W-0206-08 TCGA-02-0028-01A-01W TCGA-02-0083-01A-01W-0206-08 TCGA-08-0390-01A-01W TCGA-02-0114-01A-01W TCGA-02-0114-01A-01W-0206-08 TCGA-02-0083-01A-01W-0206-08 TCGA-02-0099-01A-01W TCGA-02-0083-01A-01W TCGA-02-0043-01A-01W TCGA-02-0014-01A-01W-0189-08 TCGA-02-0010-01A-01W TCGA-02-0083-01A-01W TCGA-02-0014-01A-01W-0189-08 TCGA-02-0113-01A-01W TCGA-06-0213-01A-01W-0254-08 TCGA-02-0014-01A-01W-0189-08 TCGA-06-0133-01A-02W-0224-08 TCGA-08-0347-01A-01W-0318-08 TCGA-02-0014-01A-01W-0189-08 TCGA-02-0083-01A-01W-0206-08 TCGA-02-0015-01A-01W-0318-08 TCGA-06-0125-01A-01W TCGA-02-0085-01A-01W-0206-08 TCGA-06-0206-01A-01W-0254-08 TCGA-02-0083-01A-01W TCGA-06-0125-01A-01W TCGA-06-0188-01A-01W TCGA-08-0245-01A-01W-0318-08 TCGA-06-0201-01A-01W TCGA-02-0028-01A-01W-0189-08 TCGA-02-0114-01A-01W TCGA-02-0048-01A-01W TCGA-06-0210-01A-01W
hsa05218:Melanoma        TCGA-06-0213-01A-01W-0254-08 TCGA-08-0347-01A-01W-0318-08 TCGA-02-0010-01A-01W-0189-08 TCGA-08-0390-01A-01W TCGA-02-0114-01A-01W TCGA-02-0085-01A-01W-0206-08 TCGA-06-0206-01A-01W-0254-08 TCGA-06-0188-01A-01W TCGA-08-0245-01A-01W-0318-08 TCGA-06-0201-01A-01W TCGA-02-0114-01A-01W TCGA-02-0048-01A-01W TCGA-02-0010-01A-01W TCGA-02-0083-01A-01W TCGA-06-0210-01A-01W                                  


我写的代码如下
  1. #!usr/bin/perl
  2. use strict;
  3. use warnings;


  4. open (IN1, $ARGV[0]) or die $!;
  5. open (IN2, $ARGV[1]) or die $!;
  6. open (OUT, ">$ARGV[2]") or die $!;

  7. my %h;
  8. while(<IN1>){
  9.         chomp;
  10.         my @m=split(/\t/,$_);
  11.         $h{$m[0]}=$m[1];           
  12. }                                                     
  13. while(<IN2>)                                          
  14. {                                                     
  15. chomp;                                                
  16. my @a=split(/\t/,$_);
  17. my @m=split(", ",$a[1]);
  18. for (0..$#m){
  19.         $m[$_]=$h{$m[$_]} if exists($h{$m[$_]});
  20.         }
  21.         print OUT "$a[0]\t@m\n";
  22.         }
复制代码
————————————————————————————————

错误:
1. 文件1.txt中,第一列是有重复的,结果中只有对应的一个。
例如:286        TCGA-02-0083-01A-01W
286        TCGA-02-0028-01A-01W
而结果只是对应了第一个,第二个没有展示。
2. 结果中有重复的
例如:hsa05200athways in cancer中出现了两次:
TCGA-02-0014-01A-01W-0189-08
TCGA-02-0014-01A-01W-0189-08

请教,

这个perl应该怎么写?




论坛徽章:
0
2 [报告]
发表于 2013-03-15 13:21 |只看该作者
...............................

论坛徽章:
0
3 [报告]
发表于 2013-03-15 14:12 |只看该作者
本帖最后由 kk861123 于 2013-03-15 18:15 编辑
  1. #!usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Data::Dumper;

  5. open (IN1, $ARGV[0]) or die $!;
  6. open (IN2, $ARGV[1]) or die $!;
  7. open (OUT, ">$ARGV[2]") or die $!;


  8. my %h;
  9. my $title1 = <IN1>;
  10. while(<IN1>){
  11.     chomp;
  12.     my @m=split(/\t/,$_);
  13.     #$h{$m[0]}=$m[1];      
  14.     push @{ $h{$m[0]} }, $m[1];
  15. }            

  16. my $title2 = <IN2>;         
  17. while(<IN2>)                                          
  18. {                                                     
  19.     my ($sTerm, $sGenes) = split(/\t/,$_, 2);
  20.     my @aGenes = split /\s*,\s*/ => $sGenes;
  21.     my ( @barcode, %hUniq );
  22.     for my $g (@aGenes) {
  23.         next unless exists $h{$g};
  24.         map { push @barcode, $_ } grep { !$hUniq{$_}++ } @{ $h{$g} };
  25.     }
  26.     print OUT "$sTerm\t@barcode\n";
  27.     print "$_ => $hUniq{$_}\n" for @barcode;  # new add 2013.18:15
  28. }
复制代码
回复 2# xingzhou823


   

论坛徽章:
0
4 [报告]
发表于 2013-03-15 14:57 |只看该作者
回复 3# kk861123

谢谢,我运行了一下,直接就出结果了。并且没有重复的。

请问,我现在顺便想统计对应样品的个数,能不能在刚才的代码中直接修改,得到?


   

论坛徽章:
0
5 [报告]
发表于 2013-03-15 18:16 |只看该作者
回复 4# xingzhou823


    已修正,看3楼代码

论坛徽章:
0
6 [报告]
发表于 2013-03-18 09:21 |只看该作者
回复 4# xingzhou823

不好意思,代码我看了,是我没说清楚。不是统计原始数据1中的样品数。

我想统计的是得到的结果中,例如:某一行:

hsa05214:Glioma        TCGA-02-0052-01A-01W TCGA-02-0027-01A-01W TCGA-02-0001-01C-01W TCGA-06-0644-01A-02W TCGA-08-0348-01A-01W TCGA-06-0125-01A-01W TCGA-06-0206-01A-01W TCGA-06-0188-01A-01W TCGA-06-0168-01A-01W TCGA-08-0345-01A-01W TCGA-12-0618-01A-01W TCGA-12-0620-01A-01W TCGA-06-0184-01A-01W TCGA-06-0208-01A-01W TCGA-06-0190-01A-01W TCGA-02-0014-01A-01W TCGA-02-0055-01A-01W TCGA-06-0187-01A-01W TCGA-06-0195-01B-01W TCGA-02-0079-01A-01W TCGA-02-0021-01A-01W TCGA-06-0166-01A-01W TCGA-02-0038-01A-01W TCGA-02-0007-01A-01W TCGA-02-0113-01A-01W TCGA-02-0075-01A-01W TCGA-08-0389-01A-01W TCGA-06-0124-01A-01W TCGA-06-0184-01A-01W-0254-08 TCGA-08-0359-01A-01W-0318-08 TCGA-06-0208-01A-01W-0254-08 TCGA-06-0190-01A-01W-0254-08 TCGA-02-0014-01A-01W-0189-08 TCGA-02-0055-01A-01W-0189-08 TCGA-06-0187-01A-01W-0254-08 TCGA-06-0195-01B-01W-0254-08 TCGA-02-0079-01A-01W-0323-08 TCGA-06-0166-01A-01W-0254-08 TCGA-02-0038-01A-01W-0189-08 TCGA-02-0007-01A-01W-0189-08 TCGA-02-0075-01A-01W-0206-08 TCGA-06-0138-01A-01W TCGA-06-0176-01A-02W TCGA-08-0380-01A-01W TCGA-06-0138-01A-01W-0254-08 TCGA-06-0176-01A-02W-0254-08 TCGA-08-0380-01A-01W-0318-08 TCGA-02-0015-01A-01W-0318-08 TCGA-02-0099-01A-01W-0206-08 TCGA-02-0004-01A-01W-0318-08 TCGA-08-0352-01A-01W-0323-08 TCGA-06-0213-01A-01W-0254-08 TCGA-06-0185-01A-01W-0254-08 TCGA-06-0174-01A-01W TCGA-08-0347-01A-01W TCGA-08-0245-01A-01W-0318-08 TCGA-08-0347-01A-01W-0318-08 TCGA-02-0003-01A-01W TCGA-08-0390-01A-01W TCGA-06-0154-01A-02W TCGA-08-0246-01A-01W TCGA-02-0089-01A-01W TCGA-02-0016-01A-01W TCGA-02-0023-01B-01W TCGA-02-0054-01A-01W TCGA-08-0355-01A-01W TCGA-06-0127-01A-01W TCGA-02-0046-01A-01W TCGA-06-0185-01A-01W TCGA-02-0010-01A-01W TCGA-06-0237-01A-02W-0254-08 TCGA-08-0390-01A-01W-0318-08 TCGA-02-0021-01A-01W-0189-08 TCGA-08-0246-01A-01W-0318-08 TCGA-02-0089-01A-01W-0206-08 TCGA-06-0148-01A-01W-0224-08 TCGA-06-0148-01A-03W-0254-08 TCGA-06-0157-01A-01W-0254-08 TCGA-06-0127-01A-01W-0323-08 TCGA-08-0349-01A-01W-0323-08 TCGA-02-0116-01A-01W-0206-08 TCGA-02-0046-01A-01W-0189-08 TCGA-06-0125-01A-01W-0224-08 TCGA-12-0616-01A-01W-0323-08 TCGA-02-0003-01A-01W-0189-08 TCGA-02-0010-01A-01W-0189-08 TCGA-02-0083-01A-01W-0206-08 TCGA-02-0083-01A-01W TCGA-02-0114-01A-01W TCGA-02-0085-01A-01W-0206-08 TCGA-06-0213-01A-01W TCGA-12-0619-01A-01W TCGA-02-0033-01A-01W TCGA-02-0052-01A-01W-0189-08 TCGA-06-0206-01A-01W-0254-08 TCGA-06-0221-01A-01W TCGA-02-0080-01A-01W TCGA-02-0058-01A-01W TCGA-02-0034-01A-01W TCGA-06-0241-01A-02W TCGA-02-0074-01A-01W TCGA-08-0351-01A-01W TCGA-08-0359-01A-01W TCGA-06-0237-01A-02W TCGA-08-0245-01A-01W TCGA-02-0037-01A-01W TCGA-02-0011-01B-01W TCGA-02-0048-01A-01W TCGA-06-0130-01A-01W TCGA-02-0084-01A-01W TCGA-08-0360-01A-01W TCGA-02-0025-01A-01W TCGA-08-0353-01A-01W TCGA-08-0373-01A-01W-0323-08 TCGA-02-0114-01A-01W-0206-08 TCGA-02-0080-01A-01W-0206-08 TCGA-02-0084-01A-01W-0323-08 TCGA-02-0058-01A-01W-0189-08 TCGA-06-0197-01A-02W-0254-08 TCGA-06-0150-01A-01W-0254-08 TCGA-02-0048-01A-01W-0318-08 TCGA-02-0026-01B-01W-0318-08 TCGA-02-0074-01A-01W-0206-08 TCGA-06-0644-01A-02W-0323-08 TCGA-08-0351-01A-01W-0318-08 TCGA-06-0159-01A-01W-0254-08 TCGA-02-0037-01A-01W-0189-08 TCGA-02-0011-01B-01W-0189-08 TCGA-12-0619-01A-01W-0323-08 TCGA-08-0360-01A-01W-0318-08 TCGA-02-0024-01B-01W TCGA-02-0024-01B-01W-0189-08 TCGA-02-0054-01A-01W-0189-08 TCGA-02-0116-01A-01W TCGA-02-0047-01A-01W TCGA-08-0375-01A-01W TCGA-06-0210-01A-01W TCGA-06-0171-01A-02W TCGA-06-0201-01A-01W TCGA-06-0129-01A-01W TCGA-02-0028-01A-01W TCGA-06-0178-01A-01W TCGA-08-0350-01A-01W TCGA-06-0133-01A-02W
——————————————————————————————————————————————————
这一行hsa05214:Glioma中包含的 样品数为144个。



   

论坛徽章:
7
戌狗
日期:2013-12-15 20:43:38技术图书徽章
日期:2014-03-05 01:33:12技术图书徽章
日期:2014-03-15 20:31:17未羊
日期:2014-03-25 23:48:20丑牛
日期:2014-04-07 22:37:44巳蛇
日期:2014-04-11 21:58:0915-16赛季CBA联赛之青岛
日期:2016-03-17 20:36:13
7 [报告]
发表于 2013-03-19 07:04 |只看该作者
看了问题。
没有知道问题。{:3_188:}{:3_188:}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP