2014-01-25 45 views
0

我有一個數組,其中包含一些DNA序列作爲存儲在其元素中的字符串。
例如:print $array[0];給出如下輸出:ACTAG(#每個序列中的第一個位置)。從數組perl中索引字符串元素的值

我寫了這段代碼,可以讓我分析每個序列的第一個位置。

#!/usr/bin/perl 
$infile = @ARGV[0]; 
$ws= $ARGV[1]; 
$wsnumber= $ARGV[2];     

open INFILE, $infile or die "Can't open $infile: $!";  # This opens file, but if file isn't there it mentions this will not open 

my $sequence =(); # This sequence variable stores the sequences from the .fasta file 
my $line;        # This reads the input file one-line-at-a-time 

while ($line = <INFILE>) { 
    chomp $line; 

    if ($ws ne "--ws=") { 
    print "no flag or invalid flag\n"; 
    last; 
    } 
    else { 
     if($line =~ /^\s*$/) {   # This finds lines with whitespaces from the beginning to the ending of the sequence. Removes blank line. 
     next; 

    } elsif($line =~ /^\s*#/) {  # This finds lines with spaces before the hash character. Removes .fasta comment 
     next; 
    } elsif($line =~ /^>/) {   # This finds lines with the '>' symbol at beginning of label. Removes .fasta label 
     next; 
    } else { 
     $sequence = $line; 

    $sequence =~ s/\s//g;    # Whitespace characters are removed 


    @array = split //,$sequence; 
    $seqlength = length($sequence);} 
} 

    $count=0; 
foreach ($array[0]){ 
    if($array[0] !~ m/A|T|C|G/){ 
     next; 
     } 
    else { 
     $count += 1; 
     $suma += $count; 

     } 
    } 
} 

但我不知道如何修改$陣列運行該代碼爲每個位置[0](我只能勉強做特定現在的位置(在上面的例子中的第一位置。?)

有人可以幫助我 謝謝

+0

**總是**'使用嚴格;使用警告;' – Toto

回答

0

你只是在尋找一個元素數組中:

foreach ($array[0]){ ... } 

遍歷整個陣列中使用:

my @array = qw(ATTTCFCGGCTTA); 

foreach (@array){ 
    my @split = split(''); 
    foreach (@split){ 
     die "$_ is not a valid character\n" unless /[ATGC]/; 
     print "$_\n"; 
    } 
} 
+0

好吧我認爲它應該可以工作,但它不起作用。我打印「$ _ \ n」,我認爲$ _存儲我的序列的每個位置。我編輯了我的帖子並插入了所有代碼。你能看看嗎?非常感謝你! – userbio

+0

@userbio - 我不確定我是否理解。查看更新 – fugu

+0

我剛剛觀察到我的@array不包含按元素排序的一個序列。所有的序列都在1個元素中。那我該如何解決呢?對不起... – userbio

0

不知道我理解的很好,但那是你想要的嗎?

假設@array包含一個元素的序列。

foreach my $seq(@array) { 
    my @chars = split '', $seq; 
    foreach my $char(@char) { 
     if ($char =~ /[ATCG]/) { 
      # do stuff 
     } 
    } 
} 
+0

嗯我不知道爲什麼它不工作,因爲我認爲它應該工作。你能再看看我的文章嗎?我編輯了它並插入了我的所有代碼。謝謝! – userbio

+0

我剛剛觀察到我的@array不包含一個元素的序列。所有的序列都在1個元素中。那我該如何解決呢? – userbio

相關問題