#!/usr/bin/env perl
use warnings;
use strict;

# The whole genome sequence was split into artificial reads(50bp length, starting every 10bp)

my $num_chrs = $ARGV[0]; #mouse 20 or human 23
my $dir_rg_fa = $ARGV[1]; # reference genome directory
my $output_file = $ARGV[2];

my $read_length = 50; # read sequence length
my $interval = 10; #starting every 10 bp

my @chroms = (1..($num_chrs-1));
push(@chroms, "X");

my $token;
for (1..$read_length) { $token .= ">"; }

open OUT, ">$output_file";
for my $index (@chroms) {
  my $file_chr_seq = "$dir_rg_fa/chr$index.fa";
  my $chr_seq;
  open IN, "<$file_chr_seq" or die $!;
  my $n = 0;
  while (my $line = <IN>) {
    chomp $line;
    $n++; if($n == 1){ next; }
    $chr_seq .= $line;
  }
  close IN;
  my $chr_length = length($chr_seq);

  for (my $i=0; $i < ($chr_length-$interval); $i += $interval) {
    my $read_seq = substr $chr_seq, $i, $read_length;
    if (length($read_seq) < $read_length) { next; }
    my $read_start = $i + 1; # 1-based coordinates
    my $read_end = $i + $read_length; 
    print OUT "\@$index:$read_start:$read_end\n";
    print OUT "$read_seq\n";
    print OUT "+\n";
    print OUT "$token\n"; 
  }
}
close OUT;
 
# End

