view NEUMA-1.2.1/auto_NEUMA_PE.pl @ 0:c44c43d185ef draft default tip

NEUMA-1.2.1 Uploaded
author chawhwa
date Thu, 08 Aug 2013 00:46:13 -0400
parents
children
line wrap: on
line source

#!/usr/bin/perl

if(@ARGV<8) { print "usage: $0 [options] -L=<read_length> -D=<maxdist> -1=<input_file1(mate1)> -2=<input_file2(mate2)> -U=<Utable_prefix(fullpath, before .gU.table or .iU.table)> --g2m=<gene2NM_file> --g2s=<gene2symbol_file> -b=<bowtie_dir(eg.bin/bowtie-0.12.7)> --bi=<bowtieindex> -o=<outputdir> -s=<sample_name>\n\nOrder of arguments can be interchangeable.\n\n"; exit; }


## options ##
($fastoption)= grep /^-f=/, @ARGV;
if(!defined($fastoption)){$fastoption='q'; }
else { $fastoption =~s/^-f=//; if($fastoption !~/^[fq]$/) { die "ERROR: wrong file type (-f).\n"; } }

($coding_option)= grep /^-c=/, @ARGV;
if(!defined($coding_option)){$coding_option='n'; }
else { $coding_option =~s/^-c=//; if($coding_option !~/^[nc]$/) { die "ERROR: wrong coding option (-c).\n"; } }

($datatype)= grep /^-d=/, @ARGV;
if(!defined($datatype)){$datatype='R'; }
else { $datatype =~s/^-d=//; if($datatype !~/^[RE]$/) { die "ERROR: wrong data type (-d).\n"; } }

($parallel)= grep /^-p=/, @ARGV;
if(!defined($parallel)){$parallel=1; }
else { $parallel =~s/^-p=//; if($parallel !~/^[\d]+$/) { die "ERROR: wrong parallel (multi-thread) option (-p).\n"; } }

($EUMAcut)= grep /^-t=/, @ARGV;
if(!defined($EUMAcut)){$EUMAcut=50; }
else { $EUMAcut =~s/^-t=//; if($EUMAcut !~/^[\d\.]+$/) { die "ERROR: wrong EUMAcut (-t).\n"; } }

($StrandSpecificity)= grep /^--str=/, @ARGV;
if(!defined($StrandSpecificity)){ $StrandSpecificity='N'; }
else { $StrandSpecificity =~s/^--str=//; if($StrandSpecificity !~/^[SN]+$/) { die "ERROR: wrong strand specificity (--str).\n"; } }

($skip_init)= grep /^--skip_init/, @ARGV;
if(!defined($skip_init)){ $skip_init=0; }
else { $skip_init=1; }

($only_init)= grep /^--only_init/, @ARGV;
if(!defined($only_init)){ $only_init=0; }
else { $only_init=1; }

if($only_init==1 && $skip_init==1) { die "ERROR: --only_init and --skip_init cannot be used simultaneously.\n"; }

($noNIR)= grep /^--noNIR/, @ARGV;
if(!defined($noNIR)){ $noNIR=0; }
else { $noNIR=1; }

($noNEUMA)= grep /^--noNEUMA/, @ARGV;
if(!defined($noNEUMA)){ $noNEUMA=0; }
else { $noNEUMA=1; }

if($noNIR==1 && $noNEUMA==0) { die "ERROR: --noNIR must be used together with --noNEUMA.\n"; }

($get_gReadcount)= grep /^--gReadcount/, @ARGV;
if(!defined($get_gReadcount)){ $get_gReadcount=0; }
else { $get_gReadcount=1; }

($mm)= grep /^--mm=/, @ARGV;
if(!defined($mm)){ $mm=0; }
else { $mm =~s/^--mm=//; if($mm !~/^[\d\.]+$/) { die "ERROR: wrong number of mismatches (--mm).\n"; } }



## required arguments ##

($READ_LENGTH)= grep /^-L=/, @ARGV;
if(!defined($READ_LENGTH)){ die "ERROR: READ_LENGTH must be specified (-L).\n"; }
else { $READ_LENGTH =~s/^-L=//; if($READ_LENGTH !~/^[\d]+$/) { die "ERROR: wrong READ_LENGTH (-L).\n"; } }

($MAXDIST)= grep /^-D=/, @ARGV;
if(!defined($MAXDIST)){ die "ERROR: MAXDIST must be specified (-D).\n"; }
else { $MAXDIST =~s/^-D=//; if($MAXDIST !~/^[\d]+$/) { die "ERROR: wrong MAXDIST (-D).\n"; } }

($input_file1)= grep /^-1=/, @ARGV;
if(!defined($input_file1)){ die "ERROR: input_file1 must be specified (-1).\n"; }
else { $input_file1 =~s/^-1=//; if(!-e $input_file1) { die "ERROR: wrong input_file1 (-1).\n"; } }

($input_file2)= grep /^-2=/, @ARGV;
if(!defined($input_file2)){ die "ERROR: input_file2 must be specified (-2).\n"; }
else { $input_file2 =~s/^-2=//; if(!-e $input_file2) { die "ERROR: wrong input_file2 (-2).\n"; } }

($Utable_prefix)= grep /^-U=/, @ARGV;
if($only_init==0 && !defined($Utable_prefix)){ die "ERROR: Utable_prefix must be specified (-U).\n"; }
else { $Utable_prefix =~s/^-U=//; if(!-e $Utable_prefix.".gU.table" || !-e $Utable_prefix.".iU.table") { die "ERROR: wrong Utable_prefix (-U).\n"; } }

($gene2NM_file)= grep /^--g2m=/, @ARGV;
if($only_init==0 && !defined($gene2NM_file)){ die "ERROR: gene2NM_file must be specified (--g2m).\n"; }
else { $gene2NM_file =~s/^--g2m=//; if(!-e $gene2NM_file) { die "ERROR: wrong gene2NM_file (--g2m).\n"; } }

($gene2symbol_file)= grep /^--g2s=/, @ARGV;
if($only_init==0 && !defined($gene2symbol_file)){ die "ERROR: gene2symbol_file must be specified (--g2s).\n"; }
else { $gene2symbol_file =~s/^--g2s=//; if(!-e $gene2symbol_file) { die "ERROR: wrong gene2symbol_file (--g2s).\n"; } }

($bowtie_dir)= grep /^-b=/, @ARGV;
if(!defined($bowtie_dir)){ die "ERROR: bowtie_dir must be specified (-b).\n"; }
else { $bowtie_dir =~s/^-b=//;  if(!-d $bowtie_dir || !-e "$bowtie_dir/bowtie") { die "ERROR: wrong bowtie_dir(please avoid using '~') (-b).\n"; } }

($bowtieindex)= grep /^--bi=/, @ARGV;
if(!defined($bowtieindex)){ die "ERROR: bowtieindex must be specified (--bi).\n"; }
else { $bowtieindex =~s/^--bi=//; if(!-e "$bowtieindex.1.ebwt") { die "ERROR: wrong bowtieindex (--bi).\n"; } }

($basedir)= grep /^-o=/, @ARGV;
if(!defined($basedir)){ die "ERROR: output dir must be specified (-o).\n"; }
else { $basedir =~s/^-o=//; if($basedir=~/~/) { die "ERROR: wrong base_dir(please avoid using '~') (-o).\n"; }  }

($sample)= grep /^-s=/, @ARGV;
if(!defined($sample)){ die "ERROR: sample name must be specified (-s).\n"; }
else { $sample =~s/^-s=//;  }


##############


$gUtable_file = $Utable_prefix.".gU.table";
$iUtable_file = $Utable_prefix.".iU.table";

my $coding='';
if($coding_option eq 'n') {$coding='';}
elsif($coding_option eq 'c') {$coding='-C';}
else { die "ERROR: Error: invalid coding option.\n"; }

if($datatype eq 'R') { $mapping_stat_column = 4; }
elsif($datatype eq 'E') { $mapping_stat_column = 6; }
else { die "ERROR: Error: wrong datatype.\n"; }

if($StrandSpecificity eq 'S') { $bowtie_strand_option = "--norc"; }
else { $bowtie_strand_option = ""; }

($scriptdir) = $0 =~ /(.+)\/[^\/]+$/;

$bowtieoutdir = "$basedir/bowtieout";
$readcount_dir = "$basedir/readcount";
$lendis_dir = "$basedir/insertlendis";
$EUMAdir = "$basedir/EUMA";
$FVKM_dir = "$basedir/FVKM";
$LVKM_dir = "$basedir/LVKM";

$mapping_stat_file = "$basedir/mapping_stat.$sample";

($midrefix) = $bowtieindex=~/([^\/]+)$/;


if(!-d $bowtieoutdir) { `mkdir -p $bowtieoutdir`; }
if(!-d $readcount_dir) { `mkdir -p $readcount_dir`; }
if(!-d $lendis_dir) { `mkdir -p $lendis_dir`; }
if(!-d $EUMAdir) { `mkdir -p $EUMAdir`; }
if(!-d $FVKM_dir) { `mkdir -p $FVKM_dir`; }
if(!-d $LVKM_dir) { `mkdir -p $LVKM_dir`; }


#`dos2unix $gene2NM_file`;
#`dos2unix $gene2symbol_file`;



#$perlcommand = "perl -I $scriptdir";
$perlcommand = "perl";
$bestbowtieout_script = "$scriptdir/filter.best.from.bowtieout.3.pl";
$bowtie2insertlendis_script = "$scriptdir/bowtieout2insertlendis.PE.pl";
$bowtieout2mappingstat_script = "$scriptdir/bowtieout2mappingstat.3.pl";
$bowtieout2readcount_script = "$scriptdir/bowtie2genecount.11.pl";

$calc_gEUMA_script = "$scriptdir/calculate_gEUMA.2.pl";
$calc_iEUMA_script = "$scriptdir/calculate_iEUMA.2.pl";
$NIR2LVKM_script = "$scriptdir/NIR2LVKM.pl";


system("date");


if($skip_init==0){
  print STDERR "Mapping reads using bowtie...\n";
  system("$bowtie_dir/bowtie -$fastoption $coding --minins 0 --maxins $MAXDIST -v $mm -a --suppress 5,6,7 -p $parallel $bowtie_strand_option $bowtieindex -1 $input_file1 -2 $input_file2 > $bowtieoutdir/$sample.$midrefix.maxins$MAXDIST.mm$mm.bowtieout"); 

  print STDERR "Filtering best-matching alignments...\n";
  if($mm > 0) {
    system("$perlcommand $bestbowtieout_script -d $datatype --rm $bowtieoutdir/$sample.$midrefix.maxins$MAXDIST.mm$mm.bowtieout 1");
  }
  else {
    system("mv $bowtieoutdir/$sample.$midrefix.maxins$MAXDIST.mm$mm.bowtieout $bowtieoutdir/$sample.$midrefix.maxins$MAXDIST.mm$mm.best.bowtieout");
  }

  print STDERR "Computing fragment length distribution...\n";
  system("$perlcommand $bowtie2insertlendis_script $bowtieoutdir/$sample.$midrefix.maxins$MAXDIST.mm$mm.best.bowtieout $READ_LENGTH >  $lendis_dir/$sample.$midrefix.maxins$MAXDIST.mm0.i.insertlendis");
}

if($only_init==0) {

  print STDERR "Computing the mapping stat...\n";
  system("$perlcommand $bowtieout2mappingstat_script -d $datatype -m $MAXDIST -l $READ_LENGTH $bowtieoutdir/$sample.$midrefix.maxins$MAXDIST.mm$mm.best.bowtieout $sample 1 > $mapping_stat_file");
  print STDERR "Mapping stat :\n";
  system("cat $mapping_stat_file");

  if($noNIR==0){
    print STDERR "Computing NIRs...\n";
    system("$perlcommand $bowtieout2readcount_script -d $datatype -m $MAXDIST -l $READ_LENGTH --gNIR -f -s $sample -v $gene2NM_file $bowtieoutdir/$sample.$midrefix.maxins$MAXDIST.mm$mm.best.bowtieout 1 > $readcount_dir/$sample.$midrefix.maxins$MAXDIST.mm$mm.gNIR");
    system("$perlcommand $bowtieout2readcount_script -d $datatype -m $MAXDIST -l $READ_LENGTH --iNIR -f -s $sample -v $gene2NM_file $bowtieoutdir/$sample.$midrefix.maxins$MAXDIST.mm$mm.best.bowtieout 1 > $readcount_dir/$sample.$midrefix.maxins$MAXDIST.mm$mm.iNIR");
  }  

  if($get_gReadcount==1) { 
     print STDERR "Computing total gene read counts (gReadcount)...\n";
     print STDERR "$perlcommand $bowtieout2readcount_script -d $datatype -m $MAXDIST -l $READ_LENGTH -f -s $sample -v $gene2NM_file $bowtieoutdir/$sample.$midrefix.maxins$MAXDIST.mm$mm.best.bowtieout 1 > $readcount_dir/$sample.$midrefix.maxins$MAXDIST.mm$mm.gReadcount\n"; 
     system("$perlcommand $bowtieout2readcount_script -d $datatype -m $MAXDIST -l $READ_LENGTH -f -s $sample -v $gene2NM_file $bowtieoutdir/$sample.$midrefix.maxins$MAXDIST.mm$mm.best.bowtieout 1 > $readcount_dir/$sample.$midrefix.maxins$MAXDIST.mm$mm.gReadcount"); 
  }
    
  if($noNEUMA==0){
    print STDERR "Computing gEUMA and iEUMA...\n";
    system("$perlcommand $calc_gEUMA_script $gUtable_file $lendis_dir/$sample.$midrefix.maxins$MAXDIST.mm0.i.insertlendis $READ_LENGTH > $EUMAdir/$sample.$midrefix.maxins$MAXDIST.mm$mm.gEUMA");
    system("$perlcommand $calc_iEUMA_script $iUtable_file $lendis_dir/$sample.$midrefix.maxins$MAXDIST.mm0.i.insertlendis $READ_LENGTH > $EUMAdir/$sample.$midrefix.maxins$MAXDIST.mm$mm.iEUMA");
  
    print STDERR "Computing FVKM and LVKM...\n";
    system("$perlcommand $NIR2LVKM_script $sample $readcount_dir $midrefix.maxins$MAXDIST.mm$mm $EUMAdir $FVKM_dir $LVKM_dir $EUMAcut $mapping_stat_column $mapping_stat_file 1 2 $gene2NM_file $gene2symbol_file $datatype $scriptdir");

  }
}

print STDERR "Done.\n";

system("date");