哈希中的基因表达数据

时间:2013-02-06 03:34:06

标签: regex perl

我有两个数据文件:一个包含基因表达数据,另一个包含基因组注释数据。我必须比较一个文件的第1列和第2列中的值,如果1> 2然后输出该行以及在注释数据文件的同一行找到的refseq id。

到目前为止,我已经打开了两个文件进行阅读:

#!usr/bin/perl
use strict;
use warnings;

open (my $deg, "<", "/data/deg/DEG_list.txt") or die $!;
open (my $af "<", "/data/deg/Affy_annotation.txt") or die $!;

# I want to store data in hash

my %data;
while (my $records = <$deg>) {
  chomp($records);

  # the first line is labels so we want to skip this
  if($records =~ /^A-Z/) {
    next;
  else {
    my @columns = split("/\s/", $records);

    if ($columns[2] > $columns[1]) {
      print $records;
    }
  }
}

我想在每次发生时打印该行,但我还想打印在其他数据文件中找到的基因id。我不知道如何做到这一点,加上我现在的代码不起作用,因为它不只是打印线。

2 个答案:

答案 0 :(得分:1)

除了你在这里和那里丢失的括号,你的问题可能是你的正则表达式

if($records =~ /^A-Z/) {

这会查找以此文字字符串开头的行,例如A-Zfoobar,而不是像你可能想到的那样,以大写字母开头的任何字符串。你可能想要:

if($records =~ /^[A-Z]/) {

方括号表示内部范围的字符类。

您还应该知道split /\s/, ...分裂在单个空格上,这可能不是您想要的,因为它为您拥有的每个额外空格创建空字段。除非您明确想要拆分单个空格,否则您可能需要

split ' ', $records;

将分割多个连续的空格,并剥离前导空格。

答案 1 :(得分:0)

代码中的两个主要问题

if($records =~ /^A-Z/) ...

如果你想检测一行开头的字母,你最好

if($records =~ /^[a-z]/i) ... starting with any letter
if($records =~ /^[A-Z]/) ...  starting with big letter

并在

my @columns = split("/\s/", $records);

正则表达式在这里是一个字符串...(自引用后),要有正则表达式删除引号

my @columns = split(/\s/, $records);

但是如果你想分割字段,即使有多个空格,也可以使用

my @columns = split(/\s+/, $records);

代替。