如何在两个不同的文件中找到两个子字符串的计数和位置?

时间:2011-02-03 06:12:23

标签: perl bioinformatics

从给出的两个序列中我需要检查每三个密码子,如果变化与下面的列表相同,那么我必须检查变化的位置和更改的密码子并计算它们的出现次数。

例如:

sequence 1 - TTCAUUUCCCAU
sequence 2 - TTTAUAUCGCAC

我需要得到的输出是

TTC->TTT considered/location-1/count-1
AUU->AUA considered/location-2/count-1
UCC->UCG considered/location-3/count-1

注意:CAU->CAC未被考虑,因为它不在以下列表中。 LIST: - >还应考虑改变的方向。

first sequence->second sequence
TTC->TTT
CTG->UUA
AUU->AUA
GUG->GUA
UCC->UCG
CCC->CCG
ACC->ACG
GCC->GCG
UAC->UAU
UGA->UAG
CAC->CAU
CAG->CAA
AAC->AAU
AAG->AAA
GAC->GAU
GAG->GAA
UGC->UGU
CGG->CGU
AGC->AGU
AGG->CGU
AGA->CGU
UAA->UAG
GGC->GGU

我到目前为止编写的代码是:

print "Enter the sequence:";
$a = <>;

print "Enter the mutated sequence:";
$b = <>;

chomp($a);
chomp($b);

my @codon = split(/(\w{3})/, $a);
my @codon1 = split(/(\w{3})/, $b);

open(OUT, ">output.txt") or die;
$count = 0;
@new = ();
@new1 = ();
for ($i = 0; $i <= $#codon; $i++) {
    for ($j = 0; $j <= $#codon1; $j++) {
        if ($codon[$i] = {TTC}) || ($codon1[$j] = {TTT}) {
            $count++;
        }
    }
}
print OUT " @new";
close OUT;

3 个答案:

答案 0 :(得分:2)

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

my %seq_map = (
    "TTC"=>"TTT",
    "CTG"=>"UUA",
    "AUU"=>"AUA",
    "GUG"=>"GUA",
    "UCC"=>"UCG",
    "CCC"=>"CCG",
    "ACC"=>"ACG",
    "GCC"=>"GCG",
    "UAC"=>"UAU",
    "UGA"=>"UAG",
    "CAC"=>"CAU",
    "CAG"=>"CAA",
    "AAC"=>"AAU",
    "AAG"=>"AAA",
    "GAC"=>"GAU",
    "GAG"=>"GAA",
    "UGC"=>"UGU",
    "CGG"=>"CGU",
    "AGC"=>"AGU",
    "AGG"=>"CGU",
    "AGA"=>"CGU",
    "UAA"=>"UAG",
    "GGC"=>"GGU"
);

my %seq_count = ();

my $seq1 = "TTCAUUUCCCAU";
my $seq2 = "TTTAUAUCGCAC";

my $max = int(length($seq1) / 3);
for(my $i=0;$i<$max;$i++) {
    my $c1 = substr($seq1, $i*3, 3);
    my $c2 = substr($seq2, $i*3, 3);
    my $found = $seq_map{$c1};
    if ($found && ($found eq $c2)) {
        $seq_count{$c1} ||= 0;
        my $count = ++$seq_count{$c1};
        my $loc = $i+1;
        print "${c1}->${c2} considered / location ${loc} / count ${count}\n";
    }
}

答案 1 :(得分:1)

有许多方法可以实现这一点,例如Perl中的情况。

如果文件不大,您可以逐行读入文件(或者如果每行已经有一个条目,那么只需将整个文件粘贴到一个数组中)。然后使用while循环(和第二个文件的文件句柄)来比较二核苷酸的位置。

因为这是一个生物信息学问题,而且文件通常很大,我会很聪明,并且会从每个文件句柄中逐行阅读,并做同事。

对于你想要做的3个字符的分割,我会使用for循环,直到你检查的字符串的长度除以3 -1。然后创建一个正则表达式,继续抓住前三个字母,然后是下一个字母,依此类推......

/\d{$count}(\w{3})/

这样的东西

while循环可能如下所示:

#!/usr/bin/perl -w
use strict;

open FILE1, "file1.txt" or die "Cannot open file1.txt: $!\n";
open FILE2, "file2.txt" or die "Cannot open file2.txt: $!\n";

my $count = 0;
while (<FILE1>) {
    chomp(my $lineF1 = $_);
    chomp(my $lineF2 = <FILE2>);

    # some changes may need to be made to this if statement
    if ($lineF1 eq $lineF2) {
        # do something important here
        print "$lineF1\n";
    } else {
        print "Line $count mismatch\n";
    }
    $count++;
}

close(FILE1);
close(FILE2);

答案 2 :(得分:0)

您能否认为两个文件中的密码子是“对齐的”?如果是这种情况,问题很简单:在2级哈希中加载有效转换列表:

 # of course, you load this from a file...
 $transitions{TTC}{TTT} = 1;
 $transitions{CTG}{UUA} = 1;
 ...

然后,逐行读取这两个文件(或者它们只是一个字符串?):

# of course, I'm leaving out all the file manipulation...
my $line1 = <FILE1>;
my $line2 = <FILE2>;

my $maxlen1 = length($line1);
my $maxlen2 = length($line2);
my $i = 0;

while($i < $maxlen1 && $i < $maxlen2){
  my $codon1 = substr($line1, $i, $i+3);
  if(exists($transitions{$codon1}){
    my $codon2 = substr($line2, $i, $i+3);
    if(exists($transitions{$codon1}{$codon2}){
      print "we have a match $codon1 -> $codon2 at index $i\n";
    }
  }
  $i += 3;
}

注意使用'exists()而不是defined(),因为它可以节省一些额外的计算。如果你不想让nexted if(),你可以计算$ codon1和$ codon2,然后检查if(exists($ transitions {$ codon1} {$ codon2})){}使用'exists'避免自动生成问题...