#This is a program designed to scramble the yeast genome.  It is a result of the combined efforts of Patrick Killion and Martin
#Kracklauer.

#!/usr/bin/perl -w 
use strict; 
my (@possible) = ('A','C','G','T');
my ($lastnuc) = $possible[rand@possible];                                      
my $nextnuc;
my $counter; 

open(OUTPUT,">scrambleyeast.txt") or die "I can't open your output file\n";
for ($counter = 0; $counter < 12156305; $counter++)
		{                                                              
		 print OUTPUT $lastnuc;
                 $nextnuc = & GeneralSub($lastnuc);
		 $lastnuc = $nextnuc;

		 if($counter % 100000 == 0) { print "\n";print $counter; }
		
                }
	                                                                       
#The above part of the program is a counter/incrementor.  It tells the program when to stop the random genome reassembly process. In
#addition, it also randomly picks the first nucleotide to hand to the "switchboard" subroutine (see below).


close(OUTPUT);
                                                                               
sub GeneralSub {
	        if($_[0] eq 'A')
			{
			 return &ASubroutine();                                
			}
		elsif($_[0] eq 'C')
			{
			 return &CSubroutine(); 
			}                                                      
		elsif($_[0] eq 'G')
			{
			 return &GSubroutine();
			}
		elsif($_[0] eq 'T')                                      
			{
			 return &TSubroutine();
			}
	       }

#The above subroutine is a "switchboard."  When the incrementor/counter part of the program randomly picks a nucleotide, that 
#nucleotide is handed into the above subroutine.  Depending on the nucleotide picked, this subroutine calls subroutines specific for
#A, C, G, and T nucleotides (below).  Nucleotides generated from the A, C, G and T subroutines are returned to the switchboard, which
#returns them to the incrementor, which puts them in a text file.  This calling, generating nucleotides, returning them and adding
#them to the switchboard happens iteratively until the counter stops.

sub ASubroutine {
	       	 my $PickNumber = int(rand(100000)) + 1;
                 if ($PickNumber >= 1 && $PickNumber <= 35071) {
							    	undef $PickNumber;
							    	return 'A';
							       }
		 elsif ($PickNumber >= 35072 && $PickNumber <= 52050) {              
							    	       undef $PickNumber;
								       return 'C';
								      }
		 elsif ($PickNumber >= 52051 && $PickNumber <= 70869) {
							    	       undef $PickNumber;
								       return 'G';
								      }		             
		 elsif ($PickNumber >= 70870 && $PickNumber <= 100000) {
							    	        undef $PickNumber;
								        return 'T';
								       }
		}

#The above subroutine tells the program what to do if an A was picked as the last nucleotide picked.  A random integer between 1 and
#100000 is generated, and depending on where the randomly picked number falls, an A, C, G or T is returned to the switchboard.  Based
#on which nucleotide is returned to the switchboard, the switchboard then both returns the nucleotide to the incrementor (which puts 
#it in the text file), and decides which subroutine to call next.

sub CSubroutine {                                                              		 
		 my $PickNumber = int(rand(100000)) + 1; 
                 if ($PickNumber >=1 && $PickNumber <= 33882) {
							       undef $PickNumber;
							       return 'A';
							      }
		 elsif ($PickNumber >= 33883 && $PickNumber <= 54291) {               
							    	      undef $PickNumber;
								      return 'C';
							             }
		 elsif ($PickNumber >= 54292 && $PickNumber <= 69603){ 
							    	     undef $PickNumber;
								     return 'G';  
							       	    }               
		 elsif ($PickNumber >= 69604 && $PickNumber <= 100000) {
							     	       undef $PickNumber;
								       return 'T';
								      }
		}

#The above subroutine tells the program what to do if C was picked as the last nucleotide picked.  A random integer between 1 and
#100000 is generated, and depending on where the randomly picked number falls, an A, C, G or T is returned to the switchboard.  Based
#on which nucleotide is returned to the switchboard, the switchboard then both returns the nucleotide to the incrementor (which puts 
#it in the text file), and decides which subroutine to call next.

sub GSubroutine { 		 
		 my $PickNumber = int(rand(100000)) + 1;
                 if ($PickNumber >=1 && $PickNumber <= 32629) {                  
							       undef $PickNumber;
							       return 'A'; 	 
							      } 
		 elsif ($PickNumber >= 32630 && $PickNumber <= 52194) {              
							    	       undef $PickNumber;
								       return 'C'; 
								      }
		 elsif ($PickNumber >= 52195 && $PickNumber <= 72498) {
							    	       undef $PickNumber;
								       return 'G';
                                                                      }              
		 elsif ($PickNumber >= 72499 && $PickNumber <= 100000) {
							    	        undef $PickNumber;
								  	return 'T';
								       }
		}

#The above subroutine tells the program what to do if G was picked as the last nucleotide picked.  A random integer between 1 and
#100000 is generated, and depending on where the randomly picked number falls, an A, C, G or T is returned to the switchboard.  Based
#on which nucleotide is returned to the switchboard, the switchboard then both returns the nucleotide to the incrementor (which puts 
#it in the text file), and decides which subroutine to call next.

sub TSubroutine {
		 my $PickNumber = int(rand(100000)) + 1;
                 if ($PickNumber >=1 && $PickNumber <= 24059) {
							       undef $PickNumber;
							       return 'A';
							      }
		 elsif ($PickNumber >= 24060 && $PickNumber <= 44156) {              
							               undef $PickNumber;
								       return 'C';
								      }
		 elsif ($PickNumber >= 44157 && $PickNumber <= 65005) {
							               undef $PickNumber;
								       return 'G';
								      }              
		 elsif ($PickNumber >= 65006 && $PickNumber <= 100000) {
							                undef $PickNumber;
								        return 'T';
								       }
		}  

#The above subroutine tells the program what to do if T was picked as the last nucleotide picked.  A random integer between 1 and
#100000 is generated, and depending on where the randomly picked number falls, an A, C, G or T is returned to the switchboard.  Based
#on which nucleotide is returned to the switchboard, the switchboard then both returns the nucleotide to the incrementor (which puts 
#it in the text file), and decides which subroutine to call next.                                                            


			


