473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Saving a file into a hash

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

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. my %seqInfo = ();
  3. open(FILE, 'test.fasta');
  4. {
  5. while(<FILE>)
  6. {
  7. my $line = $_;
  8.  if ($line =~ />.*/)
  9.   {
  10.   #print "$&\n";
  11.   }
  12. else
  13.   {
  14.   #print "$line\n";
  15.   }
  16. $seqInfo{$&} = $line;
  17. print "$& => $line\n";
  18. }
  19. }
  20.  
The output is like

Expand|Select|Wrap|Line Numbers
  1.  => >1elwA
  2.  
  3.  => AAAAAASSSSSSSSSSSSSSDDDDDDDDDDDFFFFFFFFFF
  4.  
  5.  => >1flwA
  6.  
  7.  => GGGGGGGGGGHHHHHHHHHHHHIIIIIIIIIIIIIIJJJJJJJJJJ
  8.  
  9.  => >1ghwA
  10.  
  11.  => KKKKKKKKKKLLLLLLLLLLMMMMMMMMMMNNNNNNNNNNOOOOO
  12.  
However according to my understanding the output should be like

Expand|Select|Wrap|Line Numbers
  1. >1elwA => AAAAAASSSSSSSSSSSSSSDDDDDDDDDDDFFFFFFFFFF
  2.  
  3.  
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.
Jan 5 '12 #1

✓ answered by numberwhun

Ok, here is what I used in the text file:

Expand|Select|Wrap|Line Numbers
  1. >1elwA
  2. AAAAAASSSSSSSSSSSSSSDDDDDDDDDDDFFFFFFFFFF
  3. >1flwA
  4. GGGGGGGGGGHHHHHHHHHHHHIIIIIIIIIIIIIIJJJJJJJJJJ
  5. >1ghwA
  6. KKKKKKKKKKLLLLLLLLLLMMMMMMMMMMNNNNNNNNNNOOOOO
  7.  
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:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my %seqInfo;
  7. my $key;
  8. my $val;
  9. my $count = 0;
  10.  
  11. open(FILE, '<file2hash.txt');
  12.  
  13. while(<FILE>){
  14.     my $line = $_;
  15.     chomp($line);
  16.  
  17.     if($count == 0){
  18.         $count++;
  19.  
  20.         if($line =~ m/\>\d.*/){
  21.             $key = $line;
  22.         }
  23.         next;
  24.     }
  25.  
  26.     if($count == 1){
  27.         $count++;
  28.  
  29.         if($line =~ m/\w+/){
  30.             $val = $line;
  31.         }
  32.  
  33.         if($count == 2){
  34.             if($key && $val){
  35.                 $seqInfo{$key} = $val;
  36.             }
  37.  
  38.             $key = "";
  39.             $val = "";
  40.             $count = 0;
  41.         }
  42.  
  43.         next;
  44.     }
  45. }        
  46. close(FILE);
  47.  
  48. while( my ($k, $v) = each(%seqInfo)){
  49.         print "key: $k, value: $v.\n";
  50. }
  51.  

I ran it and it seemed to work fine.

Regards,

Jeff

4 2345
numberwhun
3,509 Expert Mod 2GB
Ok, here is what I used in the text file:

Expand|Select|Wrap|Line Numbers
  1. >1elwA
  2. AAAAAASSSSSSSSSSSSSSDDDDDDDDDDDFFFFFFFFFF
  3. >1flwA
  4. GGGGGGGGGGHHHHHHHHHHHHIIIIIIIIIIIIIIJJJJJJJJJJ
  5. >1ghwA
  6. KKKKKKKKKKLLLLLLLLLLMMMMMMMMMMNNNNNNNNNNOOOOO
  7.  
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:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my %seqInfo;
  7. my $key;
  8. my $val;
  9. my $count = 0;
  10.  
  11. open(FILE, '<file2hash.txt');
  12.  
  13. while(<FILE>){
  14.     my $line = $_;
  15.     chomp($line);
  16.  
  17.     if($count == 0){
  18.         $count++;
  19.  
  20.         if($line =~ m/\>\d.*/){
  21.             $key = $line;
  22.         }
  23.         next;
  24.     }
  25.  
  26.     if($count == 1){
  27.         $count++;
  28.  
  29.         if($line =~ m/\w+/){
  30.             $val = $line;
  31.         }
  32.  
  33.         if($count == 2){
  34.             if($key && $val){
  35.                 $seqInfo{$key} = $val;
  36.             }
  37.  
  38.             $key = "";
  39.             $val = "";
  40.             $count = 0;
  41.         }
  42.  
  43.         next;
  44.     }
  45. }        
  46. close(FILE);
  47.  
  48. while( my ($k, $v) = each(%seqInfo)){
  49.         print "key: $k, value: $v.\n";
  50. }
  51.  

I ran it and it seemed to work fine.

Regards,

Jeff
Jan 6 '12 #2
numberwhun
3,509 Expert Mod 2GB
Ok, here is what I used in the text file:

Expand|Select|Wrap|Line Numbers
  1. >1elwA
  2. AAAAAASSSSSSSSSSSSSSDDDDDDDDDDDFFFFFFFFFF
  3. >1flwA
  4. GGGGGGGGGGHHHHHHHHHHHHIIIIIIIIIIIIIIJJJJJJJJJJ
  5. >1ghwA
  6. KKKKKKKKKKLLLLLLLLLLMMMMMMMMMMNNNNNNNNNNOOOOO
  7.  
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:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my %seqInfo;
  7. my $key;
  8. my $val;
  9. my $count = 0;
  10.  
  11. open(FILE, '<file2hash.txt');
  12.  
  13. while(<FILE>){
  14.     my $line = $_;
  15.     chomp($line);
  16.  
  17.     if($count == 0){
  18.         $count++;
  19.  
  20.         if($line =~ m/\>\d.*/){
  21.             $key = $line;
  22.         }
  23.         next;
  24.     }
  25.  
  26.     if($count == 1){
  27.         $count++;
  28.  
  29.         if($line =~ m/\w+/){
  30.             $val = $line;
  31.         }
  32.  
  33.         if($count == 2){
  34.             if($key && $val){
  35.                 $seqInfo{$key} = $val;
  36.             }
  37.  
  38.             $key = "";
  39.             $val = "";
  40.             $count = 0;
  41.         }
  42.  
  43.         next;
  44.     }
  45. }        
  46. close(FILE);
  47.  
  48. while( my ($k, $v) = each(%seqInfo)){
  49.         print "key: $k, value: $v.\n";
  50. }
  51.  

I ran it and it seemed to work fine.

Regards,

Jeff
Jan 6 '12 #3
Thankyou Jeff...it worked :) :)
Jan 6 '12 #4
numberwhun
3,509 Expert Mod 2GB
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.
Jan 6 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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...
0
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...
7
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...
2
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...
4
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: ........
5
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. ...
1
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...
6
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)...
1
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...
5
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...
0
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
0
BarryA
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...
1
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...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.