Perl 无法从Emsembl FASTA中删除换行符

Perl 无法从Emsembl FASTA中删除换行符,perl,newline,fasta,Perl,Newline,Fasta,我想从Ensembl FASTA文件中找到蛋白质基序。我已经完成了大部分脚本,比如检索序列ID和序列本身,但是我收到了一些有趣的结果 #!/usr/bin/perl use strict; use warnings; use autodie; my $motif1 = qr/(HE(\D)(\D)H(\D{18})E)/x; my $motif2 = qr/(AMEN)/x; my $input; my $output; my $count_total = 0; my $count_p

我想从Ensembl FASTA文件中找到蛋白质基序。我已经完成了大部分脚本,比如检索序列ID和序列本身,但是我收到了一些有趣的结果

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

my $motif1 = qr/(HE(\D)(\D)H(\D{18})E)/x;
my $motif2 = qr/(AMEN)/x;
my $input;
my $output;
my $count_total     = 0;
my $count_processed = 0;
my $total_run       = 0;
my $id;
my $seq;
my $motif1_count    = 0;
my $motif2_count    = 0;
my $motifboth_count = 0;

############################################################################################################################
# FILEHANDLING - INPUT/OUTPUT
# User input prompting and handling
print "**********************************************************\n";
print "Question 3\n";
print "**********************************************************\n";

#opens the user input file previously assigned to varible to new variable or kills script.
open my $fh, '<', "chr2.txt" || die "Error! Cannot open file:$!\n";

#Opens and creates output file previously assigned to variable to new variable or kills script
#open(RESULTS, '>', $output)||die "Error! Cannot create output file:$!\n";

# FILE and DATA PROCESSING
############################################################################################################################

while (<$fh>) {

    if (/^>(\S+)/) {
        $count_total = ++$count_total;    # Plus one to count
        find_motifs($id, $seq) if $seq;   # Passing to subroutine
        $id = substr($1, 0, 15);          # Taking only the first 16 characters for the id
        $seq = '';
    }
    else {
        chomp;
        $seq .= $_;
    }
}

print "Total proteins: $count_total \n";
print "Proteins with both motifs: $motifboth_count \n";
print "Proteins with motif 1: $motif1_count \n";
print "Proteins with motif 2: $motif2_count \n";

exit;

######################################################################################################################################
# SUBROUTINES
#
# Takes passed variables from special array
# Finds the position of motif within seq
# Checks for motif 1 presence and if found, checks for motif 2. If not found, prints motif 1 results
# If no motif 1, checks for motif 2

sub find_motifs {
    my ($id, $seq) = @_;
    if ($seq =~ $motif1) {
        my $motif_position = index $seq, $1;
        my $motif = $1;
        if ($seq =~ $motif2) {
            $motif1_count    = ++$motif1_count;
            $motif2_count    = ++$motif2_count;
            $motifboth_count = ++$motifboth_count;
            print "$id, $motif_position, \n$motif \n";
        }
        else {
            $motif1_count = ++$motif1_count;
            print "$id, $motif_position,\n $motif\n\n";
        }
    }
    elsif ($seq =~ $motif2) {
        $motif2_count = ++$motif2_count;
    }
}
这就是问题所在。motif匹配,但返回前半部分,即换行符,然后在同一行上打印后半部分(这是更大问题的症状-去掉换行符!)


我在脚本中的不同位置尝试了不同的方法,例如
@seq=~s/\r//g
或`s/\n//g。

从您的描述中不清楚,但是“在同一行上打印后半部分”听起来像是输出本身被覆盖了,因为它的末尾有一个回车字符

如果您在Linux系统上运行,而您只是
chomp
一行来自Windows的代码,则会发生这种情况

您应该将
chomp
替换为
s/\s+\z/
,这将删除所有尾随空格。因为回车符和换行符都算作“空白”,所以它将删除所有可能的终止字符

顺便说一下,您误解了
++
运算符的用途。它还修改应用于的变量的内容,因此您只需要
+$motif 1_count
等。您的代码按原样工作,因为运算符也返回递增变量的值,因此
$motif 1_count=+$motif 1_count
首先递增变量,然后将其分配给自身

另外,在正则表达式中使用
\D
。您知道这与任何非数字字符匹配吗?这似乎是一个非常模糊的分类

ENSG00000119013, 6,  HEHGHHKMELPDYRQWKIEGTPLE (CORRECT!)

ENSG00000142327, 123,  HEVAHSWFGNAVTNATWEEMWLSE (CORRECT!) 

ENSG00000151694, 410, **AECAPNEFGAEHDPDGL**
Total proteins: 13653  
Proteins with both motifs: 1  
Proteins with motif 1: 12  
Proteins with motif 2: 22