#!/usr/bin/perl use strict; use warnings; use threads; use Thread::Semaphore; my $max_thread = 5; my $semaphore = Thread::Semaphore->new($max_thread); sub TestFun { $semaphore->up(); } for(my $index = 1; $index <= 10; $index ++) { $semaphore->down(); my $thread = threads->create(\&TestFun); $thread->detach(); } WaitQuit(); <pre name="code" class="plain">sub WaitQuit { my $num = 0; while($num < $max_thread) { $semaphore->down(); $num ++; } }
#!perl use warnings; use strict; use threads; use Thread::Semaphore; die "perl $0 <thread> <blastConfig> <pep || dna> perl $0 3 blast.config dna\n" if @ARGV != 3; my $max_thread = $ARGV[0]; my $semaphore = Thread::Semaphore->new($max_thread); mkdir "compliantFasta"; chdir "compliantFasta"; open FA,"../$ARGV[1]" or die $!; while(<FA>) { chomp; my @tmp = split; $semaphore->down(); my $thread = threads->create(\&adjust,$tmp[0],$tmp[1]); $thread->detach(); } &wait; chdir ".."; my $len = 0; my ($type,$p); if($ARGV[2] =~ /dna/i) { $len = 30; $type = "F"; $p = "blastn"; }elsif($ARGV[2] =~ /pep/i){ $len = 10; $type = "T"; $p = "blastp"; } `orthomclFilterFasta compliantFasta $len 20`; `formatdb -i goodProteins.fasta -p $type`; mkdir "splitBlast"; `perl split_fasta.pl goodProteins.fasta 8 splitBlast/blast`; my @blast = `ls splitBlast/*`; my $sp2 = Thread::Semaphore->new(8); foreach my $i(@blast) { chomp($i); $sp2->down(); my $thread = threads->create(\&blast,$i,$p,$ARGV[0]); $thread->detach(); } &wait2; `cat splitBlast/*.out > all.blast`; `orthomclBlastParser all.blast compliantFasta > similarsequences.txt`; `rm all.blast -rf`; sub blast { my ($f,$t,$p) = @_; `blastall -p $t -i $f -d goodProteins.fasta -m 8 -F F -b 1000 -v 1000 -a $p -o $f.out`; $sp2->up(); } sub wait2 { my $num = 0; while($num < 8) { $sp2->down(); $num ++; } } sub adjust { my ($label,$path) = @_; `orthomclAdjustFasta $label $path 1`; $semaphore->up(); } sub wait { my $num = 0; while($num < $max_thread) { $semaphore->down(); $num ++; } }