Hello.
I have a file named "test.fasta" which contains three patterns and looks like
>1elwA
AAAAAASSSSSSSSSSSSSSDDDDDDDDDDDFFFFFFFFFF
>1flwA
GGGGGGGGGGHHHHHHHHHHHHIIIIIIIIIIIIIIJJJJJJJJJJ
>1ghwA
KKKKKKKKKKLLLLLLLLLLMMMMMMMMMMNNNNNNNNNNOOOOO
I want to store each pattern into hash with the name (>1elwA, >1flwA, >1ghwA, in this case) being the key and the other data being the value of the hash. My code is -
#!/usr/bin/perl
-
my %seqInfo = ();
-
open(FILE, 'test.fasta');
-
{
-
while(<FILE>)
-
{
-
my $line = $_;
-
if ($line =~ />.*/)
-
{
-
#print "$&\n";
-
}
-
else
-
{
-
#print "$line\n";
-
}
-
$seqInfo{$&} = $line;
-
print "$& => $line\n";
-
}
-
}
-
The output is like -
=> >1elwA
-
-
=> AAAAAASSSSSSSSSSSSSSDDDDDDDDDDDFFFFFFFFFF
-
-
=> >1flwA
-
-
=> GGGGGGGGGGHHHHHHHHHHHHIIIIIIIIIIIIIIJJJJJJJJJJ
-
-
=> >1ghwA
-
-
=> KKKKKKKKKKLLLLLLLLLLMMMMMMMMMMNNNNNNNNNNOOOOO
-
However according to my understanding the output should be like -
>1elwA => AAAAAASSSSSSSSSSSSSSDDDDDDDDDDDFFFFFFFFFF
-
-
What modifications are required in my code to get the required output. I am assuming that in this case my keys and values are not being stored properly. Any thoughts?? I shall be very thankful.
Ok, here is what I used in the text file: -
>1elwA
-
AAAAAASSSSSSSSSSSSSSDDDDDDDDDDDFFFFFFFFFF
-
>1flwA
-
GGGGGGGGGGHHHHHHHHHHHHIIIIIIIIIIIIIIJJJJJJJJJJ
-
>1ghwA
-
KKKKKKKKKKLLLLLLLLLLMMMMMMMMMMNNNNNNNNNNOOOOO
-
That has each element on a separate line. So, the trick to this, that I used that is, was counting. You are only ever worried about 2 lines at a time. So, as you will see, I used a counter in this code. I hope this helps: -
#!/usr/bin/perl
-
-
use strict;
-
use warnings;
-
-
my %seqInfo;
-
my $key;
-
my $val;
-
my $count = 0;
-
-
open(FILE, '<file2hash.txt');
-
-
while(<FILE>){
-
my $line = $_;
-
chomp($line);
-
-
if($count == 0){
-
$count++;
-
-
if($line =~ m/\>\d.*/){
-
$key = $line;
-
}
-
next;
-
}
-
-
if($count == 1){
-
$count++;
-
-
if($line =~ m/\w+/){
-
$val = $line;
-
}
-
-
if($count == 2){
-
if($key && $val){
-
$seqInfo{$key} = $val;
-
}
-
-
$key = "";
-
$val = "";
-
$count = 0;
-
}
-
-
next;
-
}
-
}
-
close(FILE);
-
-
while( my ($k, $v) = each(%seqInfo)){
-
print "key: $k, value: $v.\n";
-
}
-
I ran it and it seemed to work fine.
Regards,
Jeff
4 2345
Ok, here is what I used in the text file: -
>1elwA
-
AAAAAASSSSSSSSSSSSSSDDDDDDDDDDDFFFFFFFFFF
-
>1flwA
-
GGGGGGGGGGHHHHHHHHHHHHIIIIIIIIIIIIIIJJJJJJJJJJ
-
>1ghwA
-
KKKKKKKKKKLLLLLLLLLLMMMMMMMMMMNNNNNNNNNNOOOOO
-
That has each element on a separate line. So, the trick to this, that I used that is, was counting. You are only ever worried about 2 lines at a time. So, as you will see, I used a counter in this code. I hope this helps: -
#!/usr/bin/perl
-
-
use strict;
-
use warnings;
-
-
my %seqInfo;
-
my $key;
-
my $val;
-
my $count = 0;
-
-
open(FILE, '<file2hash.txt');
-
-
while(<FILE>){
-
my $line = $_;
-
chomp($line);
-
-
if($count == 0){
-
$count++;
-
-
if($line =~ m/\>\d.*/){
-
$key = $line;
-
}
-
next;
-
}
-
-
if($count == 1){
-
$count++;
-
-
if($line =~ m/\w+/){
-
$val = $line;
-
}
-
-
if($count == 2){
-
if($key && $val){
-
$seqInfo{$key} = $val;
-
}
-
-
$key = "";
-
$val = "";
-
$count = 0;
-
}
-
-
next;
-
}
-
}
-
close(FILE);
-
-
while( my ($k, $v) = each(%seqInfo)){
-
print "key: $k, value: $v.\n";
-
}
-
I ran it and it seemed to work fine.
Regards,
Jeff
Ok, here is what I used in the text file: -
>1elwA
-
AAAAAASSSSSSSSSSSSSSDDDDDDDDDDDFFFFFFFFFF
-
>1flwA
-
GGGGGGGGGGHHHHHHHHHHHHIIIIIIIIIIIIIIJJJJJJJJJJ
-
>1ghwA
-
KKKKKKKKKKLLLLLLLLLLMMMMMMMMMMNNNNNNNNNNOOOOO
-
That has each element on a separate line. So, the trick to this, that I used that is, was counting. You are only ever worried about 2 lines at a time. So, as you will see, I used a counter in this code. I hope this helps: -
#!/usr/bin/perl
-
-
use strict;
-
use warnings;
-
-
my %seqInfo;
-
my $key;
-
my $val;
-
my $count = 0;
-
-
open(FILE, '<file2hash.txt');
-
-
while(<FILE>){
-
my $line = $_;
-
chomp($line);
-
-
if($count == 0){
-
$count++;
-
-
if($line =~ m/\>\d.*/){
-
$key = $line;
-
}
-
next;
-
}
-
-
if($count == 1){
-
$count++;
-
-
if($line =~ m/\w+/){
-
$val = $line;
-
}
-
-
if($count == 2){
-
if($key && $val){
-
$seqInfo{$key} = $val;
-
}
-
-
$key = "";
-
$val = "";
-
$count = 0;
-
}
-
-
next;
-
}
-
}
-
close(FILE);
-
-
while( my ($k, $v) = each(%seqInfo)){
-
print "key: $k, value: $v.\n";
-
}
-
I ran it and it seemed to work fine.
Regards,
Jeff
Thankyou Jeff...it worked :) :)
Just so you know, the output when you run my code above is:
./file2hash.pl
key: >1ghwA, value: KKKKKKKKKKLLLLLLLLLLMMMMMMMMMMNNNNNNNNNNOOOOO.
key: >1flwA, value: GGGGGGGGGGHHHHHHHHHHHHIIIIIIIIIIIIIIJJJJJJJJJJ.
key: >1elwA, value: AAAAAASSSSSSSSSSSSSSDDDDDDDDDDDFFFFFFFFFF.
I know that you put something else, but that is configurable and you can change it to however you like.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: john bailo |
last post by:
For a c# program to automatically extract information from email attachments
using the Outlook 9 OM, rather than saving to a file, then reading the file,
I would prefer to 'Save' the attachment to...
|
by: marfi95 |
last post by:
I'm automating a piece of my app to allow my customers to send me a
copy of their event logs (for diagnostics) on demand just through the
click of a button. I know I can use the...
|
by: juli |
last post by:
I want to save file ,using the SaveFileDialog control, in such way
that uploaded file will be saved always as "text.txt" and always in
c:\Text folder.
How do I do it?
Thank you a lot and Happy...
|
by: Eduardo Pérez Ureta |
last post by:
I was wondering what the best way is to store a file hash (MD5 or SHA1)
and make it primary key indexed.
I have seen some people storing the hexadecimal encoded MD5 in a
CHAR(32) but it may be a...
|
by: zombek |
last post by:
I'm making a small program which takes a folder with images and
generates optimized normal-sized images and thumbnails using Python
Imaging Lbrary (PIL). The problem is here:
........
|
by: andrewcw |
last post by:
I have a VB 5 module that duplicates the FCIV.exe from Microsoft. I need to
move an application forward to C#, but the samples for MD5 hash using the
framework I tried gave different hashes. ...
|
by: John Wright |
last post by:
I want to create a file hash on my exe files and store the hash signature in
a database so I can retrieve the value to compare the file hash to ensure I
have an untampered file. How can this be...
|
by: Mark Denardo |
last post by:
My question is similar to one someone posted a few months back, but I don't
see any replies.
Basically I want to be able to have users upload photos and save them in a
database (as byte data)...
|
by: John |
last post by:
Hi
I am using an open file dialog which allows me to select a file to open.
This is all great. But it does not allow me to give a filename and select
folder to save the file to if the file does...
|
by: JohnLorac |
last post by:
Hello,
can somebody help me with saving file into local disk
using javascript? I made some sample code which
unfortunately won't work :(.
Applet sample file:
public class IO extends...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |