# a Jrna template file

package jrna_template;

use Jrna;
use Jrna_objs;
use strict;
use warnings;


my $data = Jrna->input (
			"data/arth/18S_arthropods.00.02.Nex",		# full path to input file
			"data/arth/18S_arthropods.SI.00.01.txt",	# full path to stem file
			"arth",						# model "name"
			174						# number taxa (n-1)
		);


#my $slice = slice->new;    # this is the entire dataset
#$slice->blocks(0..341);
#$slice->taxa(0..174);

 use Data::Dumper;
 
 sub foo {
       $data->init();
       my @rna = qw/A U G C N \- \? o/;

		# note I inserted this here so it's only done once (don't want to put it in the loop
		my @alphabet = qw/A C G U - ?/;
		my @exclude = qw/- ? N/;


       my $taxa;
       $taxa->{Tardigrada} = ([0]);
       $taxa->{Onychophora} = ([1..2]);
       $taxa->{Chelicerata} = ([3..7, 26]);
       $taxa->{Myriapoda} =  ([8..11, 25, 27]);
       $taxa->{Crustacea} =  ([12..18]);
       $taxa->{Collembola} =  ([19, 30..37]);
       $taxa->{Thysanura} =  ([20, 51..55]);
       $taxa->{Coleoptera} =  ([21, 24, 157-158]);
       $taxa->{Diptera} =  ([22..23, 159-160]);
       $taxa->{Protura} =  ([38..40]);
       $taxa->{Diplura} =  ([41..46]);
       $taxa->{Archaeognatha} =  ([47..50]);
       $taxa->{Odonata} =  ([56..74]);
       $taxa->{Ephemeroptera} =  ([75..86]);
       $taxa->{Plecoptera} =  ([87..96]);
       $taxa->{Phasmatodea} =  ([97..98]);
       $taxa->{Dermaptera} = ([99..103]);
       $taxa->{Blattaria} =  ([104..108]);
       $taxa->{Mantodea} =  ([109..113]);
       $taxa->{Isoptera} =  ([114..119]);
       $taxa->{Grylloblattodea} = ([120..121]);
       $taxa->{Embioptera} =  ([122..124]);
       $taxa->{Orthoptera} =  ([125..133]);
	   $taxa->{Hemiptera} =  ([134..138]);
       $taxa->{Thysanoptera} =  ([139]);
       $taxa->{Psocoptera} =  ([140..142]);
       $taxa->{Phthiraptera} =  ([143..146]);
       $taxa->{Mecoptera} =  ([147..149]);
       $taxa->{Siphonaptera} =  ([150]);
       $taxa->{Lepidoptera} =  ([151..152]);
       $taxa->{Trichoptera} =  ([153..154]);
       $taxa->{Neuroptera} =  ([155..156]);
       $taxa->{Hymenoptera} =  ([161..162]);
       $taxa->{Strepsiptera} =  ([163..173]);
    	 
       
	my $variable_region;
 
       $variable_region->{V1} = ([12..16]);
       $variable_region->{H143} = ([21..23]);
       $variable_region->{V2} = ([27..43]);
       $variable_region->{H184c} = ([44..46]);
       $variable_region->{H198b} = ([49..51]);
       $variable_region->{V3} = ([80..82]);
       $variable_region->{V4} = ([109..189]);
       $variable_region->{E231to2} = ([110..127]);
       $variable_region->{E235} = ([129..135]);
       $variable_region->{E236} = ([136..146]);
       $variable_region->{E237} = ([147..151]);
       $variable_region->{E238to14} = ([153..189]);
       $variable_region->{V5} = ([208..218]);
       $variable_region->{V6} = ([245..247]);
       $variable_region->{V7} = ([264..283]);
       $variable_region->{H1118b2} = ([269..271]);
       $variable_region->{H1118c} = ([273..278]);
       $variable_region->{V8} = ([299..316]);
       $variable_region->{V9} = ([332..334]);

       foreach my $tax (keys %{$taxa}) {
       		foreach my $vr (keys %{$variable_region}) {
 
         	#print Dumper($working_slice);
			print "\n[ $tax\t$vr ]\n";

			my $working_slice = slice->new();
			$working_slice->blocks(@{$variable_region->{$vr}});
			$working_slice->taxa(@{$taxa->{$tax}});

			#print Dumper($working_slice);
			
			$data->init_stems($working_slice);	 # you must leave this in (it won't hurt)	
			
			&Jrna::slice_prune_uninformative_taxa($working_slice); # always a good idea
			
			&Jrna::descr_average_length($working_slice); # your magic function
		

		
			# &Jrna::descr_seq_length($working_slice); - this describes the length for each taxon
			
			#&Jrna::calc_stem_bp_stats($working_slice, 0);
			#&Jrna::out_slice_stats($working_slice);

			#$data->out_column_stats($working_slice,  	
			#		'-exclude' => \@exclude,       # excluded alphabet (also '-alphabet'=> \@alphabet 		
			#		'-alphabet'=> \@alphabet,       
			#		'-type' => 'figure',		   # 'concensus' | 'max' | 'count' | 'percent' | 'combined' | 'tstv' | 'figure' | 'score'  
			#		'-concensuscutoff' => 0.099,	   # range 0-1 (%/100)	
			#		'-concensusminrows' => 0,	   # range 0-1 (%/100) - 'X' concensus returned if this % of rows (compared to total) do not have @alphabet characters
			#			'-concensustype' => 'iupac',  	   # 'iupac' | 'residue' (option has no effect in non 'concensus' tables)
			#		'-header' => 'true',		   # 'true' | 'false' (formating only)
			#		'-blockgaps' => 'true',		   # 'true' | 'false' (formating only)
			#		'-outfile' => "$tax-$vr.txt"
			#);

			# print Dumper(&Jrna::calc_stem_bp_total_for_slice($working_slice, 0));

			# sum the 
			# &Jrna::descr_stembpfreq(*STDOUT, $data, 0, 1);
		}
	}
}

&foo;



# end partitions 

# don't init your data twice

# $data->init();				# required
# $data->init_stems($my_data);		# required for output using stems (nearly everything)

#&Jrna::descr_stembpfreq(*STDOUT, $my_data, 0, 1);

#$data->out_web($my_data, 1);

#&Jrna::mx_safenames();			# translate long taxon names to unique short ones
#$data->out_web($my_data, 1, 0);	# best to start with this one as a test


#&Jrna::slice_prune_uninformative_taxa($my_data);


#$data->out_column_stats($my_data,  	
#					'-exclude' => \@exclude,       # excluded alphabet (also '-alphabet'=> \@alphabet 		
#					#	'-alphabet'=> \@alphabet       
#					'-type' => 'figure',		   # 'concensus' | 'max' | 'count' | 'percent' | 'combined' | 'tstv' | 'figure' | 'score'  
#					'-concensuscutoff' => 0.099,	   # range 0-1 (%/100)	
#					'-concensusminrows' => 0,	   # range 0-1 (%/100) - 'X' concensus returned if this % of rows (compared to total) do not have @alphabet characters
#					'-concensustype' => 'iupac',  	   # 'iupac' | 'residue' (option has no effect in non 'concensus' tables)
#					'-header' => 'true',		   # 'true' | 'false' (formating only)
#					'-blockgaps' => 'true'		   # 'true' | 'false' (formating only)
#						);





#$data->out_nexus($my_data, 2);
#$data->out_phylip($my_data, 0);
#$data->out_tnt($my_data, 0);
#$data->out_poy($my_data, 0);
#$data->out_inaase($my_data, 1);
#$data->out_fasta($my_data, 1, 0);
#$data->out_block_stats($my_data, '\w' ) ;
#$data->out_slice_stats($my_data, '\w', 1 ); 
#$data->out_stockholm($my_data);


#in 0.77 

#- legal options for '-type' are 'percent', 'max', 'count' 
#- any true option can be made 'false' 
#- you can leave out the my @alphabet part, and the '-alphabet' => \@alphabet and it'll use all unique chars in your dataset (might barf on numbers, i'm not sure 
#- it only writes to screen right now, you'll have to pipe it to a file like this: '> foo.txt' 

#perl jrna_template_foo.pl > foo.txt 


#to use include this in your template: 


#my @alphabet = qw/A C G U - ?/; 

#$data->out_column_stats($my_data, '-alphabet' => \@alphabet, '-type' => 'percent', '-header' => 'true', '-blockgaps' => 'true' );



#my @exclude = qw/- ?/; 

#$data->out_column_stats($my_data, '-exclude' => \@exclude, '-offset' => 0, '-type' => 'percent', '-header' => 'true', '-blockgaps' => 'true' );



#$data->out_phase($my_data, 0, "ichs", 2); 
#$data->out_bayes($my_data, 0);

#$data->out_rna_translate($my_data);
#$data->out_gde($my_data);

# methods that produce file-output:
#    	out_inaase(slice; correct data [0-no, 1-yes])
#    	*out_phylip(slice, stem mode)
#    	out_bayes(slice, stem mode)
#	out_poy(slice, stem mode, fileroot)
# 	out_phase(slice, stem mode, fileroot (text name for result files), output control file(s) [0-no; 1-single RNA6A; 2-multi mode])
#    	out_nexus(slice, stem mode)
#    	*out_tnt(slice, stem mode)
#    	out_rna_translate(slice)
#    	*out_web(slice, mode, interleave size, interleave)
#    	out_gde(slice)
#
# methods that produce screen output
#	descr_seq_length(slice)

1;

__END__

