微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

perl 多线程及信号控制

#!/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 ++;
	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐