如何在Perl中将字符串读入哈希 [英] How do I read strings into a hash in Perl
问题描述
我有一个文件,其中包含一系列随机的A,G,C和T,如下所示:
I have a file with a series of random A's, G's, C's and T's in them that look like this:
>Mary
ACGTACGTACGTAC
>Jane
CCCGGCCCCTA
>Arthur
AAAAAAAAAAT
我拿了那些字母并将其缩写为ACGTACGTACGTACCCCGGCCCCTAAAAAAAAAAT
.现在,我在关联的序列中有一系列我感兴趣的位置,并且我想找到与这些位置(坐标)匹配的关联名称.我正在使用Perl函数长度来计算每个序列的长度,然后将累积长度与哈希中的名称相关联.
到目前为止,我有:
I took those letters and concatinated them to end up with ACGTACGTACGTACCCCGGCCCCTAAAAAAAAAAT
. I now have a series of positions within that concatenated sequence that are of interest to me, and I want to find the associated Names that match with those positions (coordinates). I'm using the Perl function length to calculate the legnth of each sequence, and then associate the culmulative length with the name in a hash.
So far I have:
#! /usr/bin/perl -w
use strict;
my $seq_input = $ARGV[0];
my $coord_input = $ARGV[1];
my %idSeq; #Stores sequence and associated ID's.
open (my $INPUT, "<$seq_input") or die "unable to open $seq_input";
open (my $COORD, "<$coord_input") or die "unable to open $fcoord_input";
while (<$INPUT>) {
if ($_ = /^[AGCT/) {
$idSeq{$_
my $id = ( /^[>]/)
#put information into a hash
#loop through hash looking for coordinates that are lower than the culmulative length
foreach $id
$totallength = $totallength + length($seq)
$lengthId{$totalLength} = $id
foreach $position
foreach $length
if ($length >= $position) { print; last }
close $fasta_input;
close $coord_input;
print "Done!\n";
到目前为止,我在将文件读入哈希表时遇到了麻烦.我还需要一个数组来打印散列吗?
So far I'm having trouble reading the file into a hash. Also would I need an array to print the hash?
推荐答案
不清楚您想要什么;也许是这样:
Not completely clear on what you want; maybe this:
my $seq;
my %idSeq;
while ( my $line = <$INPUT> ) {
if ( my ($name) = $line =~ /^>(.*)/ ) {
$idSeq{$name} = length $seq || 0;
}
else {
chomp $line;
$seq .= $line;
}
}
产生:
$seq = 'ACGTACGTACGTACCCCGGCCCCTAAAAAAAAAAAT';
%idSeq = (
'Mary' => 0,
'Jane' => 14,
'Arthur' => 25,
);
这篇关于如何在Perl中将字符串读入哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!