CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Trying to name newly created files using the contents of the file input

 



Mr Keystrokes
Novice

Jun 22, 2017, 5:04 AM

Post #1 of 6 (4889 views)
Trying to name newly created files using the contents of the file input Can't Post

Hi there,

I have a script which extracts sequences from a file containing thousands of fasta sequences and creates separate files for each of them.
Here is my script:

Code
#!/usr/bin/perl  
use strict;
use warnings;


open my $fh, '<',"human_hg19_circRNAs_putative_spliced_sequence.fa",or die $!;

my %id2seq;

while(<$fh>){
my $id = '';
chomp;
if($_ =~ /^>(.+)/){
$id = $1;
}else{
$id2seq{$id} .= $_;
}
}



foreach my $id (keys %id2seq){
my $filename = (split /\|/, $id)[0];
open my $out_fh, '>>', "$filename" or die $!;
print $out_fh ">".$id."\n",$id2seq{$id}, "\n";
close $out_fh;
}


close $fh;


Now, the human_hg19_circRNAs_putative_spliced_sequence.fa file which I am working on contains sequences as such:

>hsa_circ_0000001|chr1:1080738-1080845-|None|None
ATGGGGTTGGGTCAGCCGTGCGGTCAGGTCAGGTCGGCCATGAGGTCAGGTGGGGTCGGCCATGAAGGTGGTGGGGGTCATGAGGTCACAAGGGGGTCGGCCATGT

My script captures each sequence header as the key of a hash and captures the sequence itself as the hash. But the problem is that I want to name the files with only a part of the $id and not the whole of it i.e. hsa_circ_0000001. This is why I've split it like this:


Code
my $filename = (split /\|/, $id)[0];

But the thing is, when I try to integrate it into my loop wherein I create each new file, it claims that $filename is undefined.
I am wondering how then I integrate it into the naming of the file


BillKSmith
Veteran

Jun 22, 2017, 6:06 AM

Post #2 of 6 (4887 views)
Re: [Mr Keystrokes] Trying to name newly created files using the contents of the file input [In reply to] Can't Post

$id is being reinitialized. Change

Code
while(<$fh>){  
my $id = '';


to


Code
my $id = '';  
while(<$fh>){

Good Luck,
Bill


Mr Keystrokes
Novice

Jun 22, 2017, 10:38 AM

Post #3 of 6 (4875 views)
Re: [BillKSmith] Trying to name newly created files using the contents of the file input [In reply to] Can't Post

An earlier version of my script was like your suggestion, but it still didn't work. Also, I don't know how it relates.

Keystrokes.


FishMonger
Veteran / Moderator

Jun 22, 2017, 11:43 AM

Post #4 of 6 (4868 views)
Re: [Mr Keystrokes] Trying to name newly created files using the contents of the file input [In reply to] Can't Post

What does "didn't work" mean?

What results did you get and how did they differ from what you expected?


FishMonger
Veteran / Moderator

Jun 22, 2017, 11:46 AM

Post #5 of 6 (4866 views)
Re: [Mr Keystrokes] Trying to name newly created files using the contents of the file input [In reply to] Can't Post

Personally, I'd read the file in chunks where each chunk is a complete sequence instead of reading line-by-line.


Mr Keystrokes
Novice

Jul 11, 2017, 5:48 AM

Post #6 of 6 (4482 views)
Re: [FishMonger] Trying to name newly created files using the contents of the file input [In reply to] Can't Post

Hey, apologies for my late reply. Thanks a lot, I managed to solve that last problem some how. Actually, I think I compromised on something, but it's cool.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives