#!/usr/bin/perl
# seqlong2peshort.pl

use strict;

my $RL= $ENV{rlen}||250; my $RL2=2*$RL;
my $DOPE=(defined $ENV{paired})? $ENV{paired} : 1;
# my $DOPEIN=($DOPE > 1)?1:0; # now Insert = 2*RL;
my $DOFQ= $ENV{fastq}||0;
my $outname=$ENV{out}||""; # "outseqlong2short";
my($nrd,$brd,$id,$fa)=(0) x 9;
my $DEFqual=join"", (("K") x $RL); # $DEFqual="\+\n".$DEFqual."\n";

my($oh1,$oh2);
my $suf=($DOFQ)?"fq":"fa";

if($DOPE) {
  $outname ||="seqlong2short$$";
  open($oh1,'>',$outname."_1.$suf");
  open($oh2,'>',$outname."_2.$suf");
} else {
  if($outname){ 
    open($oh1,'>',$outname.".$suf");
  } else {
    $oh1=*STDOUT;
  }
}

# need FQ @ID input opt
my $inIsFq= 0; # -1, +1 when found
my($fa,$qa,$id,$inq)= ("") x 9;
while(<>) {
  if($inIsFq < 0) {
    if(/^>(\S+)/){ my $d=$1; putv($id,$fa) if($fa); $id=$d; $fa=""; } else { chomp; $fa.=$_; }
  
  } elsif($inIsFq > 0) {
    if(/^@(\S+)/){ my $d=$1; putv($id,$fa,$qa) if($fa); $id=$d; $qa=$fa=""; $inq=0; } 
    elsif(/^\+/){ $inq=1; }
    elsif($inq) { chomp; $qa.=$_; }
    else { chomp; $fa.=$_; }
  } else {
    if(/^>(\S+)/) {  my $d=$1; $inIsFq= -1; $id=$d; $fa=""; } 
    elsif(/^\@(\S+)/) {  my $d=$1; $inIsFq= 1; $id=$d; $qa=$fa=""; }    
    else { } # bad data?
  }
  # if(/^>(\S+)/){ my $d=$1; putv($id,$fa) if($fa); $id=$d; $fa=""; } else { chomp; $fa.=$_; }
}
putv($id,$fa) if($fa);

my $mbrd=int($brd/100_000)/10;  
warn"# nread=$nrd, mb=$mbrd bp=$brd for rlen=$RL, paired=$DOPE to $outname _1,2.$suf\n";
close($oh1) if($outname); close($oh2) if($DOPE);

sub revcomp{ my($s)=@_; $s=reverse(split('',$s)); $s=~tr/ACGTacgt/TGCAtgca/; return($s); }

sub putv{ return ($DOPE>1)? putpairINSERT(@_) : ($DOPE)? putpair(@_) : putone(@_); }

sub putpair {
  my($id,$fa,$qa)=@_; 
  $id=~s/\./_/g; 
  my $k=1; my $i=0; my $imax= length($fa) + 1 - $RL2; 
  while($i < $imax) { 
    my $j=$i+$RL; # NO insert: al|ar,bl|br,cl|cr... 
    my $ls=substr($fa,$i,$RL); $ls=revcomp($ls);
    my $rs=substr($fa,$j,$RL);
    my $ids=$id."_s$k"; 
    if($DOFQ) { 
    my($lq,$rq);
    if($qa){
      $lq=substr($qa,$i,$RL);  $lq= reverse(split('',$lq));
      $rq=substr($qa,$j,$RL);
    } else {
      $lq= $rq= $DEFqual; # == join '', (('K') x $RL);
    }
    print $oh1 "\@$ids/1\n$ls\n","\+\n",$lq,"\n";
    print $oh2 "\@$ids/2\n$rs\n","\+\n",$rq,"\n";
    } else {
    print $oh1 ">$ids/1\n$ls\n";
    print $oh2 ">$ids/2\n$rs\n";
    }
    $i += $RL2; $k++; $nrd += 2; 
    $brd += length($ls) + length($rs);
  }

}

sub putpairINSERT {  # try w/ insert gap == 2*RL ?
  my($id,$fa,$qa)=@_; 
  $id=~s/\./_/g; 
  my $IL= 2*$RL; # == RL2
  my $k=1; my $i=0; my $jb=0; my $je=0;
  my $jmax= length($fa);
  my $imax= $jmax + 1 - $RL2; 
  while($i < $imax) { 
    # my $j=$i+$RL;  
    my $j= $i + $RL + $IL; 
    if($jb < $i+$RL) { $jb=$j; } 
    $je= $j+$RL; last if($je > $jmax);
    my $ls=substr($fa,$i,$RL); $ls=revcomp($ls);
    my $rs=substr($fa,$j,$RL);
    my $ids=$id."_s$k"; 
    if($DOFQ) { 
    my($lq,$rq);
    if($qa){
      $lq=substr($qa,$i,$RL);  $lq= reverse(split('',$lq));
      $rq=substr($qa,$j,$RL);
    } else {
      $lq= $rq= $DEFqual; # == join '', (('K') x $RL);
    }
    print $oh1 "\@$ids/1\n$ls\n","\+\n",$lq,"\n";
    print $oh2 "\@$ids/2\n$rs\n","\+\n",$rq,"\n";
    } else {
    print $oh1 ">$ids/1\n$ls\n";
    print $oh2 ">$ids/2\n$rs\n";
    }
    # $i += $RL2; 
    $i= $i + $RL; # in insert, 2 steps, then skip over j parts ??
    if($i >= $jb and $i < $je){ $i= $je; } #??
    $k++; $nrd += 2; 
    $brd += length($ls) + length($rs);
  }

}

sub putone { 
  my($id,$fa,$qa)=@_; 
  $id=~s/\./_/g; 
  my $k=1; my $i=0; my $imax=length($fa) - $RL; 
  while($i < $imax){ 
    my $fs=substr($fa,$i,$RL); my $ids=$id."_s$k"; my $lfs=length($fs); 
    if($DOFQ) { 
      my $lq= ($qa) ? substr($qa,$i,$RL) : $DEFqual;
      print $oh1 "\@$ids\n",$fs,"\n","\+\n",$lq,"\n"; 
    } else { print $oh1 ">$ids\n$fs\n"; }
    $i+=$RL; $k++; $nrd++; $brd+=$lfs; 
  }
} 


=item basic split to readlen=NNN

# eel22gnodes/readsf/eueel_sra.info
# split long reads to short for gnodes, like w/ kmer shredding but bigger bites...

pt=SRR13496139; 
pt=SRR13496140; 
gunzip -c readsf/$pt.fasta.gz | env rlen=300 perl -ne \
'BEGIN{ $RL=$ENV{rlen}||250; } if(/^>(\S+)/){ my $d=$1; putv($id,$fa) if($fa); $id=$d; $fa=""; } else { chomp; $fa.=$_; } 
END{ putv($id,$fa);  $mbrd=int($brd/100_000)/10;  warn"# nrd=$nrd, mb=$mbrd bp=$brd for rlen=$RL\n"; } 
sub putv{ my($id,$fa)=@_; $id=~s/\./_/g; my $k=1; my $i=0; my $len=length($fa) - $RL; while($i < $len){ 
 my $fs=substr($fa,$i,$RL); my $ids=$id."_s$k"; my $lfs=length($fs); 
  print ">$ids\n$fs\n"; $i+=$RL; $k++; $nrd++; $brd+=$lfs; } }' \
> readsf/${pt}r300b.fa

cat SRR134961{39,40}r300b.fa > SRR134961nano300b.fa

=cut
