Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Instead of speculating about what might work, here's a threaded solution that does.

I've C&P's most of the code from your OP, but I've made various changes to adapt it to my environment. I only had one chromosome file kicking around ( Chr_22_14-09-2001 Release 3, [nucleotoides 13100001-47848585] ), so I used that multiple times. And for the motifes I just clipped out a few short (80 character) sequences at random.

The output goes to STDOUT. You could redirect this to a file via the command line or open STDOUT to a file before spawning the threads. On my system which has only 1 cpu, there is no conflict with writing to a file from multiple threads. Most threaded C-runtimes would serialise this for you, but if yours does not, it would only require the addition of 1 shared variable and two locks to do so.

The code creates a separate thread for each chromosome file in the list and passes the chromosome name as a paramater to each thread. Depending upon the number of chromosome files you are processing, it might be better to limit the number of concurrent threads--but given that each thread is also doing IO to the same file, it might be ok as is for upto 2 or 3 times the number of chromosomes as you have cpus. It will require a substantial amount of memory as written if the chromosome files are large, but with better knowledge of the task, the code can be easily adapted.

The motifs file is read and loaded into a shared hash and re-used by all the threads.

#! perl -slw use strict; use Carp; use threads; use threads::shared; my $chr_file = $ARGV[0]; my $seq_file = $ARGV[1]; if ( 2 != scalar(@ARGV) ) { croak 'Invalid parameter number'; } elsif ( ! -e $chr_file || ! -T $chr_file ) { croak 'Missing or invalid chromsome-listing file'; } elsif ( ! -e $seq_file || ! -T $seq_file ) { croak 'Missing or invalid sequence-listing file'; } my %motifs : shared; sub thread { my $chromosome = shift; my $directory = 'fasta/'; my $file = 'chr_'.$chromosome.'.fa'; my $path = $directory.$file; # my $seqio = Bio::SeqIO->new( # -file => "<$path", # -format => 'largefasta' # ); # my $seq = $seqio->next_seq(); # my $sequence = $seq->seq(); ## Crude fasta load--Expects 1 sequence per file open my $fh, '<', $path or croak "$path : $!\n"; <$fh>; ## discard header ( my $sequence = do{ local $/; <$fh> } ) =~ s[\s+][]g; close $fh; foreach my $motif ( keys(%motifs) ) { my $str = $motifs{$motif}; my $len = length($str); my $pos = 0; while ( ($pos = index( $sequence, $str, $pos)) >= 0 ) { print join "\t", $chromosome, $pos, $motif; $pos += $len; } } } open my $fh_seq, '<', $seq_file or croak "Unable to open motif file: $seq_file"; while (<$fh_seq>) { s/^\s+//; s/\s+$//; my @row = split("\t"); next if 2 != scalar @row; $motifs{ $row[0] } = $row[1]; } close($fh_seq); my @chromosomes; open my $fh_chr, '<', $chr_file or croak "Unable to open chromsome list: $chr_file"; while (<$fh_chr>) { s/^\s+//; s/\s+$//; my $row = $_; next if !$row; push @chromosomes, $row; } close($fh_chr); my @threads = map{ threads->new( \&thread, $_ ) } @chromosomes; $_->join for @threads; __END__ C:\test>568393 fasta\chromosomes.lst fasta\motifs.lst 22_14-09-2001 6666540 6 22_14-09-2001 6666540 6 22_14-09-2001 6666540 6 22_14-09-2001 6666540 6 22_14-09-2001 6666540 6 22_14-09-2001 6666540 6 22_14-09-2001 6666540 6 22_14-09-2001 6666540 6 22_14-09-2001 6666540 6 22_14-09-2001 6666540 6 22_14-09-2001 6666540 6 22_14-09-2001 6540 3 22_14-09-2001 6540 3 22_14-09-2001 6540 3 22_14-09-2001 6540 3 22_14-09-2001 6540 3 22_14-09-2001 6540 3 22_14-09-2001 6540 3 22_14-09-2001 6540 3 22_14-09-2001 6540 3 22_14-09-2001 6540 3 22_14-09-2001 6540 3 22_14-09-2001 12666540 7 22_14-09-2001 12666540 7 22_14-09-2001 12666540 7 22_14-09-2001 12666540 7 22_14-09-2001 12666540 7 22_14-09-2001 24666540 9 22_14-09-2001 12666540 7 22_14-09-2001 12666540 7 22_14-09-2001 12666540 7 22_14-09-2001 12666540 7 22_14-09-2001 12666540 7 22_14-09-2001 12666540 7 22_14-09-2001 540 2 22_14-09-2001 24666540 9 22_14-09-2001 540 2 22_14-09-2001 24666540 9 22_14-09-2001 24666540 9 22_14-09-2001 24666540 9 22_14-09-2001 540 2 22_14-09-2001 540 2 22_14-09-2001 24666540 9 22_14-09-2001 540 2 22_14-09-2001 24666540 9 22_14-09-2001 24666540 9 22_14-09-2001 540 2 22_14-09-2001 24666540 9 22_14-09-2001 24666540 9 22_14-09-2001 540 2

It's easier to respond to questions if there are any than to try guess, and comment on what you might not understand.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Forking Multiple Regex's on a Single String (use threads) by BrowserUk
in thread Forking Multiple Regex's on a Single String by bernanke01

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-20 05:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found