【问题标题】:How to calculate frequency of characters in a FASTA file in Perl如何在 Perl 中计算 FASTA 文件中字符的频率
【发布时间】:2014-03-23 12:27:47
【问题描述】:

我正在尝试从 FASTA 格式的文件中计算字符串中某些字符的百分比。所以文件看起来像这样;

>label
sequence
>label
sequence
>label
sequence

我正在尝试从“序列”字符串中计算特定字符(例如 G)的百分比。 在计算完之后(我已经能够做到),我试图打印一个句子,上面写着:“G 在(例如)标签 1 中的百分比是(例如)53%”。

所以我的问题真的是,我如何对序列字符串进行计算,然后通过上面的标签在其对应的输出中命名每个字符串?

到目前为止,我的代码计算出百分比,但我无法识别它。

#!/usr/bin/perl 
use strict; 

# opens file
my $infile = "Lab1_seq.fasta.txt";
open INFILE, $infile or die "$infile: $!\n";

# reads each line
while (my $line = <INFILE>){ 
    chomp $line;

    #creates an array
    my @seq = split (/>/, $line);

    # Calculates percent
    if ($line !~ />/){
        my $G = ($line =~ tr/G//);
        my $C = ($line =~ tr/C//);
        my $total = $G + $C;
        my $length = length($line);
        my $percent = ($total / $length) * 100;

        #prints the percentage of G's and C's for label is x%
        print "The percentage of G's and C's for @seq[1] is $percent\n";
    }
    else{

    }
}

close INFILE

当我真的试图让它也说出与序列对应的每个标签的名称时,它会吐出这个输出(如下)

The percentage of G's and C's for  is 53.4868841970569
The percentage of G's and C's for  is 52.5443110348771
The percentage of G's and C's for  is 50.8746355685131

【问题讨论】:

    标签: perl printing fasta


    【解决方案1】:

    您只需要匹配您的标签并将其保存在变量中:

    my $label;
    
    # reads each line
    while (my $line = <INFILE>){ 
        ...
    
        if ($line =~ />(.*)/){
            $label = $1;
    
        # Calculates percent
        } else{
            ...
            print "The percentage of G's and C's for $label, @seq[1] is $percent\n";
        }
    }
    

    【讨论】:

    • 完美解决了。非常感谢。我花了很长时间对此感到困惑。 :)
    • 您在提出问题方面做得非常出色。包含所有需要的信息以便于提供帮助的方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多