如何从Perl中的连续行中检索值?

时间:2018-10-26 05:22:22

标签: regex string perl

我在下面有一个称为data.txt的数据,我想从此数据中检索四个列。首先,我要检索退化组类别,然后是p值,然后是Query:之前和之后的文本。因此结果应如下所示(仅显示第一行):

Degardome Category: 3  Degradome p-value: 0.0195958324320822   3' UGACGUUUCAGUUCCCAGUAU 5' Seq_3694_200

data.txt:

5' CCGGUAAGGUUAUGGGUCAUG 3' Transcript: Supercontig_2.8_1446328:1451-1471 Slice Site:1462
      |o||o||o| |||||||o
3' UGACGUUUCAGUUCCCAGUAU 5' Query: Seq_3694_200

SiteID: Supercontig_2.8_1446328:1462
MFE of perfect match: -36.10
MFE of this site: -23.60
MFEratio: 0.653739612188366
Allen et al. score: 7.5
Paired Regions (query5'-query3',transcript3'-transcript5')
    1-8,1471-1464
    10-18,1462-1454
Unpaired Regions (query5'-query3',transcript3'-transcript5')
    9-9,1463-1463   SIL: Symmetric internal loop
    19-21,1453-1451 UP3: Unpaired region at 3' of query

Degradome data file: /media/owner/newdrive/phasing/degradome/_degradome.20171210/bbduk_trimmed/merged_HV2.fasta_dd.txt
Degardome Category: 3
Degradome p-value: 0.0195958324320822
T-Plot file: T-plots-IGR/Seq_3694_200_Supercontig_2.8_1446328_1462_TPlot.pdf

Position    Reads   Category
1462    4   3   <<<<<<<<<<
2949    7   3
4179    517 0
---------------------------------------------------
---------------------------------------------------

5' GGUGAGGAGGGGGGUUUG-GUC 3' Transcript: Supercontig_2.8_1511075:1311-1331 Slice Site:1323
    | |||||oo||| |||o |||
3' AC-CUCCUUUCCCGAAAUACAG 5' Query: Seq_2299_664

SiteID: Supercontig_2.8_1511075:1323
MFE of perfect match: -37.90
MFE of this site: -25.30
MFEratio: 0.66754617414248
Allen et al. score: 8
Paired Regions (query5'-query3',transcript3'-transcript5')
    1-3,1331-1329
    5-8,1328-1325
    10-19,1323-1314
    20-20,1312-1312
Unpaired Regions (query5'-query3',transcript3'-transcript5')
    4-4,x-x BULq: Bulge on query side
    9-9,1324-1324   SIL: Symmetric internal loop
    x-x,1313-1313   BULt: Bulge on transcript side
    21-21,1311-1311 UP3: Unpaired region at 3' of query

Degradome data file: /media/owner/newdrive/phasing/degradome/_degradome.20171210/bbduk_trimmed/merged_HV2.fasta_dd.txt
Degardome Category: 4
Degradome p-value: 0.013385336399181

我尝试针对值之前和之后执行此操作,然后不断出现错误。抱歉,我是perl的新手,非常感谢您的帮助。这是我尝试过的一些代码:

#!/usr/bin/perl
use warnings;
use strict;
use LWP::Simple;
use Modern::Perl;

my word = "Query:";

my $filename = $ARGV[0];
open(INPUT_FILE, $filename);
while (<<>>) {
chomp;
my ($before, $after) = m/(.+)(?:\t\Q$word\E:\t)(.+)/i;
say "word: $word\tbefore: $before\tafter: $after";
}

1 个答案:

答案 0 :(得分:3)

由于每个部分都需要直接的数据,并且两个部分和数据都清楚地标了出来,所以唯一的问题是要使用哪种数据结构。假设您只希望使用从每个部分收集的值的行,那么简单的数组应该没问题。

众所周知,感兴趣的短语Query:然后Degardome Category: N然后p-value对于样本中显示的上下文和位置是唯一的。

use warnings;
use strict;
use feature 'say';

my $file = shift || die "Usage $0 file\n";

open my $fh, '<', $file  or die "Can't open $file: $!";

my (@res, @query, $category, $pvalue);

while (<$fh>) {
    next if not /\S/;

    if (/(.*?)\s+Query:\s+(.*)/) {
        @query = ($1, $2);
        next;
    }   

    if (/^\s*(Degardome Category:\s+[0-9]+)/) {
        $category = $1; 
    }   
    elsif (/^\s*(Degradome p-value:\s+[0-9.]+)/) {
        $pvalue = $1; 
        push @res, [$category, $pvalue, @query];
    }   
}

say "@$_" for @res;

p-value:行检测到节的结尾,这时我们向@res添加一个arrayref,并捕获到该点的所有需要​​的值。

正则表达式始终取决于样本中数据的属性。如果我的某些假设不正确,请检查并调整。

即使只是简单地将捕获组添加到上述正则表达式中(并将这些捕获保存到其他数据结构中),也可以从数据中更精确地探查细节。