This is a static copy of a profile report

Home

LOH_hapmap_v4 (1 call, 46.607 sec)
Generated 14-Nov-2016 07:47:05 using cpu time.
function in file /home/user/dev/ymap/scripts_seqModules/scripts_WGseq/LOH_hapmap_v4.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
analyze_SNPs_hapmapfunction1
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
1165
saveas(fig,        [projectDir...
17.559 s16.2%
482
calculate_allelic_ratio_cutoff...
16.995 s15.0%
1166
saveas(fig,        [projectDir...
16.552 s14.1%
1171
saveas(Linear_fig, [projectDir...
13.780 s8.1%
891
f = fill(x_,y_,c_);
31403.185 s6.8%
All other lines  18.536 s39.8%
Totals  46.607 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
saveasfunction420.703 s44.4%
calculate_allelic_ratio_cutoffsscript16.995 s15.0%
newplotfunction63962.338 s5.0%
isholdfunction63961.058 s2.3%
angle_plot_subfiguresscript80.947 s2.0%
FindChrSizes_4function10.464 s1.0%
titlefunction90.222 s0.5%
Load_genome_informationfunction10.101 s0.2%
subplotfunction160.071 s0.2%
addpathfunction10.040 s0.1%
phased_and_unphased_color_definitionsscript10.030 s0.1%
num2strfunction540.030 s0.1%
ylimfunction160.010 s0.0%
holdfunction400.010 s0.0%
Load_size_infofunction10.010 s0.0%
xlimfunction160 s0%
str2numfunction100 s0%
Load_dataset_informationfunction10 s0%
filepartsfunction10 s0%
filereadfunction10 s0%
Self time (built-ins, overhead, etc.)  13.577 s29.1%
Totals  46.607 s100% 
Code Analyzer results
Line numberMessage
1Input argument 'LOH_verString' might be unused, although a later one is used. Consider replacing it by ~.
1Input argument 'CNV_verString' might be unused, although a later one is used. Consider replacing it by ~.
1Extra semicolon is unnecessary.
3The value assigned to variable 'workingDir' might be unused.
18The value assigned to variable 'Chr_max_width' might be unused.
19The value assigned to variable 'colorBars' might be unused.
22The value assigned to variable 'Yscale_nearest_even_ploidy' might be unused.
25The value assigned to variable 'AnglePlot' might be unused.
26The value assigned to variable 'FillColors' might be unused.
27The value assigned to variable 'show_uncalibrated' might be unused.
42The value assigned here to 'FastaPath' appears to be unused. Consider replacing it by ~.
42The value assigned here to 'FastaName' appears to be unused. Consider replacing it by ~.
42The value assigned here to 'FastaExt' appears to be unused. Consider replacing it by ~.
54The value assigned to variable 'useHapmap' might be unused.
55The value assigned to variable 'hapmapDir' might be unused.
57The value assigned to variable 'hapmapUser' might be unused.
59The value assigned to variable 'useHapmap' might be unused.
60The value assigned to variable 'hapmapDir' might be unused.
62The value assigned to variable 'hapmapUser' might be unused.
64The value assigned to variable 'useHapmap' might be unused.
65The value assigned to variable 'hapmapDir' might be unused.
67The value assigned to variable 'hapmapUser' might be unused.
76The value assigned to variable 'parentDir' might be unused.
77The value assigned to variable 'parentUser' might be unused.
79The value assigned to variable 'parentDir' might be unused.
80The value assigned to variable 'parentUser' might be unused.
82The value assigned to variable 'parent' might be unused.
85The value assigned to variable 'parentDir' might be unused.
86The value assigned to variable 'parent' might be unused.
87The value assigned to variable 'parentUser' might be unused.
92The value assigned here to 'ploidy_default' appears to be unused. Consider replacing it by ~.
94The value assigned to variable 'num_chrs' might be unused.
96The variable 'chr_size' appears to change size on every loop iteration. Consider preallocating for speed.
97The variable 'cen_start' appears to change size on every loop iteration. Consider preallocating for speed.
98The variable 'cen_end' appears to change size on every loop iteration. Consider preallocating for speed.
105Using ISEMPTY is usually faster than comparing LENGTH to 0.
108The variable 'annotation_chr' appears to change size on every loop iteration. Consider preallocating for speed.
109The variable 'annotation_type' appears to change size on every loop iteration. Consider preallocating for speed.
110The variable 'annotation_start' appears to change size on every loop iteration. Consider preallocating for speed.
111The variable 'annotation_end' appears to change size on every loop iteration. Consider preallocating for speed.
112The variable 'annotation_fillcolor' appears to change size on every loop iteration. Consider preallocating for speed.
113The variable 'annotation_edgecolor' appears to change size on every loop iteration. Consider preallocating for speed.
114The variable 'annotation_size' appears to change size on every loop iteration. Consider preallocating for speed.
121The value assigned to variable 'key_posX' might be unused.
122The value assigned to variable 'key_posY' might be unused.
123The value assigned to variable 'key_width' might be unused.
124The value assigned to variable 'key_height' might be unused.
127The value assigned to variable 'chr_id' might be unused.
127The variable 'chr_id' appears to change size on every loop iteration. Consider preallocating for speed.
128The variable 'chr_label' appears to change size on every loop iteration. Consider preallocating for speed.
129The variable 'chr_name' appears to change size on every loop iteration. Consider preallocating for speed.
130The variable 'chr_posX' appears to change size on every loop iteration. Consider preallocating for speed.
131The variable 'chr_posY' appears to change size on every loop iteration. Consider preallocating for speed.
132The variable 'chr_width' appears to change size on every loop iteration. Consider preallocating for speed.
133The variable 'chr_height' appears to change size on every loop iteration. Consider preallocating for speed.
134The variable 'chr_in_use' appears to change size on every loop iteration. Consider preallocating for speed.
134If you are operating on scalar values, consider using STR2DOUBLE for faster performance.
142The variable 'chr_breaks' appears to change size on every loop iteration. Consider preallocating for speed.
144Using ISEMPTY is usually faster than comparing LENGTH to 0.
148The value assigned to variable 'chr_broken' might be unused.
153The variable 'chr_breaks' appears to change size on every loop iteration. Consider preallocating for speed.
170If you are operating on scalar values, consider using STR2DOUBLE for faster performance.
172If you are operating on scalar values, consider using STR2DOUBLE for faster performance.
200The variable 'chr_SNPdata' appears to change size on every loop iteration. Consider preallocating for speed.
205The variable 'chr_SNPdata_colorsC' appears to change size on every loop iteration. Consider preallocating for speed.
206The value assigned to variable 'chr_SNPdata_colorsP' might be unused.
206The variable 'chr_SNPdata_colorsP' appears to change size on every loop iteration. Consider preallocating for speed.
209The variable 'chr_SNPdata_countC' appears to change size on every loop iteration. Consider preallocating for speed.
210The value assigned to variable 'chr_SNPdata_countP' might be unused.
210The variable 'chr_SNPdata_countP' appears to change size on every loop iteration. Consider preallocating for speed.
219Variable 'CNVplot2' is used, but might be unset.
234The variable 'chr_breaks_new' appears to change size on every loop iteration. Consider preallocating for speed.
235The variable 'chrCopyNum_new' appears to change size on every loop iteration. Consider preallocating for speed.
236The variable 'chr_breaks_new' appears to change size on every loop iteration. Consider preallocating for speed.
238Using ISEMPTY is usually faster than comparing LENGTH to 0.
240The variable 'chrCopyNum_new' appears to change size on every loop iteration. Consider preallocating for speed.
254The variable 'chr_breaks_new' appears to change size on every loop iteration. Consider preallocating for speed.
324Using ISEMPTY is usually faster than comparing LENGTH to 0.
338If you are operating on scalar values, consider using STR2DOUBLE for faster performance.
339If you are operating on scalar values, consider using STR2DOUBLE for faster performance.
340The value assigned to variable 'fragment_end' might be unused.
340If you are operating on scalar values, consider using STR2DOUBLE for faster performance.
341The value assigned to variable 'chr_length' might be unused.
351The variable 'gap_string' appears to change size on every loop iteration. Consider preallocating for speed.
363Using ISEMPTY is usually faster than comparing LENGTH to 0.
368If you are operating on scalar values, consider using STR2DOUBLE for faster performance.
377Using ISEMPTY is usually faster than comparing LENGTH to 0.
382If you are operating on scalar values, consider using STR2DOUBLE for faster performance.
391Using ISEMPTY is usually faster than comparing LENGTH to 0.
396If you are operating on scalar values, consider using STR2DOUBLE for faster performance.
405Using ISEMPTY is usually faster than comparing LENGTH to 0.
410If you are operating on scalar values, consider using STR2DOUBLE for faster performance.
419Using ISEMPTY is usually faster than comparing LENGTH to 0.
433Using ISEMPTY is usually faster than comparing LENGTH to 0.
497The value assigned to variable 'localCopyEstimate' might be unused.
506Using ISEMPTY is usually faster than comparing LENGTH to 0.
532Variable 'chrSegment_actual_cutoffs' is used, but might be unset.
533Variable 'chrSegment_mostLikelyGaussians' is used, but might be unset.
563For better readability, use newline, semicolon, or comma before this statement.
746Using ISEMPTY is usually faster than comparing LENGTH to 0.
779The variable 'SNPs_count' appears to change size on every loop iteration. Consider preallocating for speed.
782The variable 'SNPs_to_fullData_ratio' appears to change size on every loop iteration. Consider preallocating for speed.
785The variable 'SNPs_to_fullData_ratio' appears to change size on every loop iteration. Consider preallocating for speed.
787The variable 'phased_plot' appears to change size on every loop iteration. Consider preallocating for speed.
788The variable 'phased_plot2' appears to change size on every loop iteration. Consider preallocating for speed.
789The variable 'phased_plot2' appears to change size on every loop iteration. Consider preallocating for speed.
791The variable 'unphased_plot' appears to change size on every loop iteration. Consider preallocating for speed.
792The variable 'unphased_plot2' appears to change size on every loop iteration. Consider preallocating for speed.
793The variable 'unphased_plot2' appears to change size on every loop iteration. Consider preallocating for speed.
801The value assigned here to 'linear_axis_font_size' appears to be unused. Consider replacing it by ~.
802The value assigned here to 'stacked_axis_font_size' appears to be unused. Consider replacing it by ~.
803The value assigned here to 'stacked_copy_font_size' appears to be unused. Consider replacing it by ~.
803The value assigned here to 'max_chrom_label_size' appears to be unused. Consider replacing it by ~.
828The value assigned to variable 'axisLabelPosition_horiz' might be unused.
830The value assigned to variable 'axisLabelPosition_vert' might be unused.
837The value assigned to variable 'first_chr' might be unused.
846The value assigned to variable 'subPlotHandle' might be unused.
853The value assigned to variable 'infill' might be unused.
872The variable 'colors' appears to change size on every loop iteration. Consider preallocating for speed.
873The variable 'colors' appears to change size on every loop iteration. Consider preallocating for speed.
874The variable 'colors' appears to change size on every loop iteration. Consider preallocating for speed.
905Using ISEMPTY is usually faster than comparing LENGTH to 0.
983Using ISEMPTY is usually faster than comparing LENGTH to 0.
1011The value assigned to variable 'apply_phasing' might be unused.
1107Using ISEMPTY is usually faster than comparing LENGTH to 0.
1134Using ISEMPTY is usually faster than comparing LENGTH to 0.
1158The value assigned to variable 'first_chr' might be unused.
Coverage results
Show coverage for parent directory
Total lines in function1177
Non-code lines (comments, blank lines)339
Code lines (lines that can run)838
Code lines that did run482
Code lines that did not run356
Coverage (did run/can run)57.52 %
Function listing
time 
calls 
 line
   1 
function [] = LOH_hapmap_v4(main_dir,user,genomeUser,project,parent_or_hapmap,genome,ploidyEstimateString,ploidyBaseString, SNP_verString,LOH_verString,CNV_verString,displayBREAKS);
  0.04 
      1 
   2 
addpath('../'); 
      1 
   3 
workingDir = [main_dir 'users/' user '/projects/' project '/']; 
      1 
   4 
fprintf('\n\n\t*===============================================================*\n'); 
      1 
   5 
fprintf(    '\t| Generate SNP/LOH only plot in script "LOH_hapmap_v4.m".       |\n'); 
      1 
   6 
fprintf(    '\t*---------------------------------------------------------------*\n'); 
      1 
   7 
tic; 
   8 

   9 

  10 
%% ========================================================================
  11 
%    Centromere_format          : Controls how centromeres are depicted.   [0..2]   '2' is pinched cartoon default.
  12 
%    bases_per_bin              : Controls bin sizes for SNP/CGH fractions of plot.
  13 
%    scale_type                 : 'Ratio' or 'Log2Ratio' y-axis scaling of copy number.
  14 
%                                 'Log2Ratio' does not properly scale CGH data by ploidy.
  15 
%    Chr_max_width              : max width of chrs as fraction of figure width.
      1 
  16 
fprintf('\t|\tSetup for processing.\n'); 
      1 
  17 
Centromere_format              = 0; 
      1 
  18 
Chr_max_width                  = 0.8; 
      1 
  19 
colorBars                      = true; 
      1 
  20 
blendColorBars                 = false; 
      1 
  21 
show_annotations               = true; 
      1 
  22 
Yscale_nearest_even_ploidy     = true; 
      1 
  23 
Linear_display                 = true; 
      1 
  24 
Linear_displayBREAKS           = false; 
      1 
  25 
AnglePlot                      = true;   % Show histogram of alleleic fraction at the left end of standard figure chromosomes. 
      1 
  26 
FillColors                     = true;   %     Fill histogram using colors. 
      1 
  27 
show_uncalibrated              = false;  %     Fill with single color instead of ratio call colors. 
  28 

  29 

  30 

  31 
%%=========================================================================
  32 
% Load FASTA file name from 'reference.txt' file for project.
  33 
%--------------------------------------------------------------------------
      1 
  34 
fprintf('\t|\tLoad FASTA file name for the genome in use.\n'); 
      1 
  35 
userReference                  = [main_dir 'users/' user '/genomes/' genome '/reference.txt']; 
      1 
  36 
defaultReference               = [main_dir 'users/default/genomes/' genome '/reference.txt']; 
      1 
  37 
if (exist(userReference,'file') == 0)    
  38 
	FASTA_string               = strtrim(fileread(defaultReference));
      1 
  39 
else                     
      1 
  40 
	FASTA_string               = strtrim(fileread(userReference)); 
      1 
  41 
end; 
      1 
  42 
[FastaPath,FastaName,FastaExt] = fileparts(FASTA_string); 
  43 

  44 

  45 
%%=========================================================================
  46 
% Control variables for Candida albicans SC5314.
  47 
%--------------------------------------------------------------------------
      1 
  48 
projectDir = [main_dir 'users/' user '/projects/' project '/']; 
      1 
  49 
genomeDir  = [main_dir 'users/' genomeUser '/genomes/' genome '/']; 
  50 

  51 

      1 
  52 
fprintf('\t|\tDetermine if hapmap is in use.\n'); 
      1 
  53 
if (exist([main_dir 'users/default/hapmaps/' parent_or_hapmap '/'], 'dir') == 7) 
  54 
	useHapmap  = true;
  55 
	hapmapDir  = [main_dir 'users/default/hapmaps/' parent_or_hapmap '/'];   % system hapmap.
  56 
	hapmap     = parent_or_hapmap;
  57 
	hapmapUser = 'default';
  0.01 
      1 
  58 
elseif (exist([main_dir 'users/' user '/hapmaps/' parent_or_hapmap '/'], 'dir') == 7) 
  59 
	useHapmap  = true;
  60 
	hapmapDir  = [main_dir 'users/' user '/hapmaps/' parent_or_hapmap '/'];  % user hapmap.
  61 
	hapmap     = parent_or_hapmap;
  62 
	hapmapUser = user;
      1 
  63 
else 
      1 
  64 
	useHapmap  = false; 
      1 
  65 
	hapmapDir  = ''; 
      1 
  66 
	hapmap     = ''; 
      1 
  67 
	hapmapUser = ''; 
      1 
  68 
end; 
  69 

  70 

      1 
  71 
fprintf('\t|\tDetermine if parent project is in use.\n'); 
  72 
% The 'parent' will == the 'project' when no 'parent' is selected in setup.
      1 
  73 
if (strcmp(project,parent_or_hapmap) == 0)   % different 
      1 
  74 
	useParent  = true; 
      1 
  75 
	if (exist([main_dir 'users/default/projects/' parent_or_hapmap '/'], 'dir') == 7) 
  76 
		parentDir  = [main_dir 'users/default/projects/' parent_or_hapmap '/'];   % system parent.
  77 
		parentUser = 'default';
      1 
  78 
	else 
      1 
  79 
		parentDir  = [main_dir 'users/' user '/projects/' parent_or_hapmap '/'];  % user parent. 
      1 
  80 
		parentUser = user; 
      1 
  81 
	end; 
      1 
  82 
	parent     = parent_or_hapmap; 
  83 
else
  84 
	useParent  = false;
  85 
	parentDir  = projectDir;
  86 
	parent     = project;
  87 
	parentUser = user;
  88 
end;
  89 

  90 

      1 
  91 
fprintf('\t|\tLoad deteilas of genome in use.\n'); 
  0.10 
      1 
  92 
[centromeres, chr_sizes, figure_details, annotations, ploidy_default] = Load_genome_information(genomeDir); 
      1 
  93 
[Aneuploidy]                                                          = Load_dataset_information(projectDir); 
      1 
  94 
num_chrs                          = length(chr_sizes); 
      1 
  95 
for i = 1:length(chr_sizes) 
      9 
  96 
	chr_size(i)                   = 0; 
      9 
  97 
	cen_start(i)                  = 0; 
      9 
  98 
	cen_end(i)                    = 0; 
      9 
  99 
end; 
      1 
 100 
for i = 1:length(chr_sizes) 
      9 
 101 
	chr_size(chr_sizes(i).chr)    = chr_sizes(i).size; 
      9 
 102 
	cen_start(centromeres(i).chr) = centromeres(i).start; 
      9 
 103 
	cen_end(centromeres(i).chr)   = centromeres(i).end; 
      9 
 104 
end; 
      1 
 105 
if (length(annotations) > 0) 
 106 
	fprintf(['\nAnnotations for ' genome '.\n']);
 107 
	for i = 1:length(annotations)
 108 
		annotation_chr(i)       = annotations(i).chr;
 109 
		annotation_type{i}      = annotations(i).type;
 110 
		annotation_start(i)     = annotations(i).start;
 111 
		annotation_end(i)       = annotations(i).end;
 112 
		annotation_fillcolor{i} = annotations(i).fillcolor;
 113 
		annotation_edgecolor{i} = annotations(i).edgecolor;
 114 
		annotation_size(i)      = annotations(i).size;
 115 
		fprintf(['\t[' num2str(annotations(i).chr) ':' annotations(i).type ':' num2str(annotations(i).start) ':' num2str(annotations(i).end) ':' annotations(i).fillcolor ':' annotations(i).edgecolor ':' num2str(annotations(i).size) ']\n']);
 116 
	end;
 117 
end;
      1 
 118 
for i = 1:length(figure_details) 
      8 
 119 
	if (figure_details(i).chr == 0) 
 120 
		if (strcmp(figure_details(i).label,'Key') == 1)
 121 
			key_posX   = figure_details(i).posX;
 122 
			key_posY   = figure_details(i).posY;
 123 
			key_width  = figure_details(i).width;
 124 
			key_height = figure_details(i).height;
 125 
		end;
      8 
 126 
	else 
      8 
 127 
		chr_id    (figure_details(i).chr) = figure_details(i).chr; 
      8 
 128 
		chr_label {figure_details(i).chr} = figure_details(i).label; 
      8 
 129 
		chr_name  {figure_details(i).chr} = figure_details(i).name; 
      8 
 130 
		chr_posX  (figure_details(i).chr) = figure_details(i).posX; 
      8 
 131 
		chr_posY  (figure_details(i).chr) = figure_details(i).posY; 
      8 
 132 
		chr_width (figure_details(i).chr) = figure_details(i).width; 
      8 
 133 
		chr_height(figure_details(i).chr) = figure_details(i).height; 
      8 
 134 
		chr_in_use(figure_details(i).chr) = str2num(figure_details(i).useChr); 
      8 
 135 
	end; 
      8 
 136 
end; 
      1 
 137 
num_chrs = length(chr_size); 
 138 
%% This block is normally calculated in FindChrSizes_2 in CNV analysis.
      1 
 139 
for usedChr = 1:num_chrs 
  0.01 
      9 
 140 
	if (chr_in_use(usedChr) == 1) 
 141 
		% determine where the endpoints of ploidy segments are.
      8 
 142 
		chr_breaks{usedChr}(1) = 0.0; 
      8 
 143 
		break_count = 1; 
      8 
 144 
		if (length(Aneuploidy) > 0) 
      8 
 145 
			for i = 1:length(Aneuploidy) 
     48 
 146 
				if (Aneuploidy(i).chr == usedChr) 
      6 
 147 
					break_count = break_count+1; 
      6 
 148 
					chr_broken = true; 
      6 
 149 
					chr_breaks{usedChr}(break_count) = Aneuploidy(i).break; 
      6 
 150 
				end; 
     48 
 151 
			end; 
      8 
 152 
		end; 
      8 
 153 
		chr_breaks{usedChr}(length(chr_breaks{usedChr})+1) = 1; 
      8 
 154 
	end; 
      9 
 155 
end; 
      1 
 156 
bases_per_bin = max(chr_size)/700; 
 157 

 158 

 159 
%% =========================================================================================
 160 
%% =========================================================================================
 161 
%% =========================================================================================
 162 
%% -----------------------------------------------------------------------------------------
 163 
%% =========================================================================================
 164 
%% =========================================================================================
 165 
%% =========================================================================================
 166 

 167 

      1 
 168 
fprintf('\t|\tProcess input ploidy.\n'); 
 169 
% Process input ploidy.
      1 
 170 
ploidy = str2num(ploidyEstimateString); 
 171 
% Sanitize user input of euploid state.
      1 
 172 
ploidyBase = round(str2num(ploidyBaseString)); 
      1 
 173 
if (ploidyBase > 4);   ploidyBase = 4;   end; 
      1 
 174 
if (ploidyBase < 1);   ploidyBase = 1;   end; 
      1 
 175 
fprintf(['\nEuploid base = "' num2str(ploidyBase) '"\n']); 
 176 

 177 

 178 
%% =========================================================================================
 179 
% Define colors for figure generation.
 180 
%-------------------------------------------------------------------------------------------
      1 
 181 
fprintf('\t|\tLoad color definitions.\n'); 
  0.03 
      1 
 182 
phased_and_unphased_color_definitions; 
 183 

 184 

 185 
%%================================================================================================
 186 
% Setup for SNP/LOH data calculations.
 187 
%-------------------------------------------------------------------------------------------------
      1 
 188 
fprintf('\t|\tInitialize data vectors for tracking data presentation.\n'); 
 189 
% Initializes vectors used to hold allelic ratios for each chromosome segment.
      1 
 190 
for chr = 1:length(chr_sizes) 
 191 
	% Build data structure for SNP information:  chr_SNPdata{chr,j}{chr_bin} = [];
 192 
	%       1 : phased SNP ratio data.
 193 
	%       2 : unphased SNP ratio data.
 194 
	%       3 : phased SNP position data.
 195 
	%       4 : unphased SNP position data.
 196 
	%       5 : phased SNP allele strings.   (baseCall:alleleA/alleleB)
 197 
	%       6 : unphased SNP allele strings.
      9 
 198 
	chr_length = ceil(chr_size(chr)/bases_per_bin); 
      9 
 199 
	for j = 1:6 
     54 
 200 
		chr_SNPdata{chr,j} = cell(1,chr_length); 
     54 
 201 
	end; 
 202 
	% Setup to track RGB values used to present SNP/LOH data for each chromosome bin.
      9 
 203 
	for j = 1:3 
 204 
		% Track the RGB value sum per standard bin, then divide by the count to reach the average color per standard genome bin.
     27 
 205 
		chr_SNPdata_colorsC{chr,j}           = zeros(chr_length,1); 
     27 
 206 
		chr_SNPdata_colorsP{chr,j}           = zeros(chr_length,1); 
     27 
 207 
	end; 
 208 
	% Track the number of SNP colors per standard bin.
      9 
 209 
	chr_SNPdata_countC{chr} = zeros(chr_length,1); 
      9 
 210 
	chr_SNPdata_countP{chr} = zeros(chr_length,1); 
      9 
 211 
end; 
 212 

 213 

 214 
%%================================================================================================
 215 
% Load 'Common_CNV.mat' file containing CNV estimates per standard genome bin.
 216 
%-------------------------------------------------------------------------------------------------
      1 
 217 
fprintf('\t|\tLoading "Common_CNV" data file, to be used in copy number estimation.\n'); 
      1 
 218 
load([projectDir 'Common_CNV.mat']);   % 'CNVplot2', 'genome_CNV' 
  0.46 
      1 
 219 
[chr_breaks, chrCopyNum, ploidyAdjust] = FindChrSizes_4(Aneuploidy,CNVplot2,ploidy,num_chrs,chr_in_use); 
 220 

 221 

 222 
%% =========================================================================================
 223 
% Test adjacent segments for no change in copy number estimate.
 224 
%...........................................................................................
 225 
% Adjacent pairs of segments with the same copy number will be fused into a single segment.
 226 
% Segments with a <= zero copy number will be fused to an adjacent segment.
 227 
%-------------------------------------------------------------------------------------------
      1 
 228 
for chr = 1:num_chrs 
      9 
 229 
	if (chr_in_use(chr) == 1) 
      8 
 230 
		if (length(chrCopyNum{chr}) > 1)  % more than one segment, so lets examine if adjacent segments have different copyNums. 
 231 
			%% Merge any adjacent segments with the same copy number.
 232 
			% add break representing left end of chromosome.
      3 
 233 
			breakCount_new         = 1; 
      3 
 234 
			chr_breaks_new{chr}    = []; 
      3 
 235 
			chrCopyNum_new{chr}    = []; 
      3 
 236 
			chr_breaks_new{chr}(1) = 0.0; 
      3 
 237 
			fprintf(['\nlength(chrCopyNum{chr}) = ' num2str(length(chrCopyNum{chr})) '\n']); 
      3 
 238 
			if (length(chrCopyNum{chr}) > 0) 
      3 
 239 
				fprintf(['chrCopyNum{chr}(1) = ' num2str(chrCopyNum{chr}(1)) '\n']); 
      3 
 240 
				chrCopyNum_new{chr}(1) = chrCopyNum{chr}(1); 
      3 
 241 
				for segment = 1:(length(chrCopyNum{chr})-1) 
      6 
 242 
					if (round(chrCopyNum{chr}(segment)) == round(chrCopyNum{chr}(segment+1))) 
 243 
						% two adjacent segments have identical copyNum and should be fused into one; don't add boundry to new list.
      4 
 244 
					else 
 245 
						% two adjacent segments have different copyNum; add boundry to new list.
      4 
 246 
						breakCount_new                      = breakCount_new + 1; 
      4 
 247 
						chr_breaks_new{chr}(breakCount_new) = chr_breaks{chr}(segment+1); 
      4 
 248 
						chrCopyNum_new{chr}(breakCount_new) = chrCopyNum{chr}(segment+1); 
      4 
 249 
					end; 
      6 
 250 
				end; 
      3 
 251 
			end; 
 252 
			% add break representing right end of chromosome.
      3 
 253 
			breakCount_new = breakCount_new+1; 
      3 
 254 
			chr_breaks_new{chr}(breakCount_new) = 1.0; 
      3 
 255 
			fprintf(['@@@ chr = ' num2str(chr) '\n']); 
  0.01 
      3 
 256 
			fprintf(['@@@    chr_breaks_old = ' num2str(chr_breaks{chr})     '\n']); 
  0.01 
      3 
 257 
			fprintf(['@@@    chrCopyNum_old = ' num2str(chrCopyNum{chr})     '\n']); 
      3 
 258 
			fprintf(['@@@    chr_breaks_new = ' num2str(chr_breaks_new{chr}) '\n']); 
      3 
 259 
			fprintf(['@@@    chrCopyNum_new = ' num2str(chrCopyNum_new{chr}) '\n']); 
 260 
			% copy new lists to old.
      3 
 261 
			chr_breaks{chr} = chr_breaks_new{chr}; 
      3 
 262 
			chrCopyNum{chr} = []; 
      3 
 263 
			chrCopyNum{chr} = chrCopyNum_new{chr}; 
      3 
 264 
		end; 
      8 
 265 
	end; 
      9 
 266 
end; 
 267 

 268 

 269 
%%================================================================================================
 270 
% Load SNP/LOH data.
 271 
%.................................................................................................
 272 
% if (useHapmap)
 273 
%       chr_SNPdata{chr,1}{chr_bin} = phased SNP ratio data.
 274 
%       chr_SNPdata{chr,2}{chr_bin} = unphased SNP ratio data.
 275 
%       chr_SNPdata{chr,3}{chr_bin} = phased SNP position data.
 276 
%       chr_SNPdata{chr,4}{chr_bin} = unphased SNP position data.
 277 
%       chr_SNPdata{chr,5}{chr_bin} = flipper value for phased SNP.
 278 
%       chr_SNPdata{chr,6}{chr_bin} = flipper value for unphased SNP.
 279 
% elseif (useParent)
 280 
%       chr_SNPdata{chr,1}{chr_bin} = parent SNP ratio data.
 281 
%       chr_SNPdata{chr,2}{chr_bin} = child SNP ratio data.
 282 
%       chr_SNPdata{chr,3}{chr_bin} = parent SNP position data.
 283 
%       chr_SNPdata{chr,4}{chr_bin} = child SNP position data.
 284 
% else
 285 
%       chr_SNPdata{chr,2}{chr_bin} = child SNP ratio data.
 286 
%       chr_SNPdata{chr,4}{chr_bin} = child SNP position data.
 287 
% end;
 288 
%-------------------------------------------------------------------------------------------------
 289 
%	if (useHapmap)
 290 
%		phased_ratio_data_string    = '(1.0)';
 291 
%		unphased_ratio_data_string  = '()';
 292 
%		phased_coordinates_string   = '(19846)';
 293 
%		unphased_coordinates_string = '()';
 294 
%		phased_alleles_string       = '(G:G/C)';
 295 
%		unphased_alleles_string     = '()';
 296 
%	elseif (useParent)
 297 
%		% coordinates that are heterozygous (0.25 .. 0.75) in the parent.
 298 
%		phased_ratio_data_string    = '()';
 299 
%		unphased_ratio_data_string  = '(1.0,1.0)';
 300 
%		phased_coordinates_string   = '()';
 301 
%		unphased_coordinates_string = '(19846,20572)';
 302 
%		phased_alleles_string       = '()';
 303 
%		unphased_alleles_string     = '(Z:G/C,Z:A/C)';
 304 
%	else
 305 
%		% coordinate sthat are heterozygous (0.25 .. 0.75) in the experimental dataset.
 306 
%		phased_ratio_data_string    = '()';
 307 
%		unphased_ratio_data_string  = '(0.529411764706,0.538461538462)';
 308 
%		phased_coordinates_string   = '()';
 309 
%		unphased_coordinates_string = '(19846,20572)';
 310 
%		phased_alleles_string       = '()';
 311 
%		unphased_alleles_string     = '(Z:G/C,Z:A/C)';
 312 
%	end;
 313 

      1 
 314 
fprintf('\t|\tLoad SNP/LOH data.\n'); 
  0.01 
      1 
 315 
if (exist([projectDir 'SNP_' SNP_verString '.mat'],'file') == 0) 
 316 
	fprintf(['\t|\t\tMAT file "SNP_' SNP_verString '.mat" not found, regenerating.']);
 317 
	datafile       = [projectDir 'preprocessed_SNPs.txt'];
 318 
	data           = fopen(datafile, 'r');
 319 
	count          = 0;
 320 
	old_chr        = 0;
 321 
	gap_string     = '';	
 322 
	while not (feof(data))
 323 
		dataLine = fgetl(data);
 324 
		if (length(dataLine) > 0)
 325 
			if (dataLine(1) ~= '#')
 326 
				% process the loaded line into data channels.
 327 
				chr_num                     = sscanf(dataLine, '%s',1);
 328 
				fragment_start              = sscanf(dataLine, '%s',2);   for i = 1:size(sscanf(dataLine,'%s',1),2);   fragment_start(1)              = [];   end;
 329 
				fragment_end                = sscanf(dataLine, '%s',3);   for i = 1:size(sscanf(dataLine,'%s',2),2);   fragment_end(1)                = [];   end;
 330 
				phased_ratio_data_string    = sscanf(dataLine, '%s',4);   for i = 1:size(sscanf(dataLine,'%s',3),2);   phased_ratio_data_string(1)    = [];   end;
 331 
				unphased_ratio_data_string  = sscanf(dataLine, '%s',5);   for i = 1:size(sscanf(dataLine,'%s',4),2);   unphased_ratio_data_string(1)  = [];   end;
 332 
				phased_coordinates_string   = sscanf(dataLine, '%s',6);   for i = 1:size(sscanf(dataLine,'%s',5),2);   phased_coordinates_string(1)   = [];   end;
 333 
				unphased_coordinates_string = sscanf(dataLine, '%s',7);   for i = 1:size(sscanf(dataLine,'%s',6),2);   unphased_coordinates_string(1) = [];   end;
 334 
				phased_alleles_string       = sscanf(dataLine, '%s',8);   for i = 1:size(sscanf(dataLine,'%s',7),2);   phased_alleles_string(1)       = [];   end;
 335 
				unphased_alleles_string     = sscanf(dataLine, '%s',9);   for i = 1:size(sscanf(dataLine,'%s',8),2);   unphased_alleles_string(1)     = [];   end;
 336 

 337 
				% format = simple, one number per column.
 338 
				chr_num                     = str2num(chr_num);
 339 
				fragment_start              = str2num(fragment_start);
 340 
				fragment_end                = str2num(fragment_end);
 341 
				chr_length                  = ceil(chr_size(chr_num)/bases_per_bin);
 342 
				chr_bin                     = ceil(fragment_start/bases_per_bin);
 343 

 344 
				% Log file output to indicate progression of this section of code.
 345 
				count = count+1;
 346 
				if (old_chr ~= chr_num)
 347 
					fprintf(['\n\t|\t\t' chr_name{chr_num} '\n\t|\t' gap_string]);
 348 
				end;
 349 
				if (mod(count,10) == 0)
 350 
					fprintf('.');
 351 
					gap_string = [gap_string ' '];
 352 
				end;
 353 
				if (count == 800)
 354 
					fprintf('\n\t|\t\t');
 355 
					count = 0;
 356 
					gap_string = '';
 357 
				end;
 358 
				old_chr = chr_num;
 359 

 360 
				% format = '(number1,number2,...,numberN)'
 361 
				phased_ratio_data_string(1)              = [];
 362 
				phased_ratio_data_string(end)            = [];
 363 
				if (length(phased_ratio_data_string)    == 0)
 364 
					phased_ratio_data                = [];
 365 
				else
 366 
					commaCount                       = length(find(phased_ratio_data_string==','));
 367 
					if (commaCount == 0)
 368 
						phased_ratio_data        = str2num(phased_ratio_data_string);
 369 
					else
 370 
						phased_ratio_data        = strsplit(phased_ratio_data_string,',');   % function converts number lists from strings to numbers.
 371 
					end;
 372 
				end;
 373 

 374 
				% format = '(number1,number2,...,numberN)'
 375 
				phased_coordinates_string(1)             = [];
 376 
				phased_coordinates_string(end)           = [];
 377 
				if (length(phased_coordinates_string)   == 0)
 378 
					phased_coordinates               = [];
 379 
				else
 380 
					commaCount                       = length(find(phased_coordinates_string==','));
 381 
					if (commaCount == 0)
 382 
						phased_coordinates       = str2num(phased_coordinates_string);
 383 
					else
 384 
						phased_coordinates       = strsplit(phased_coordinates_string,',');   % function converts number lists from strings to numbers.
 385 
					end;
 386 
				end;
 387 

 388 
				% format = '(number1,number2,...,numberN)'
 389 
				unphased_ratio_data_string(1)            = [];
 390 
				unphased_ratio_data_string(end)          = [];
 391 
				if (length(unphased_ratio_data_string)  == 0)
 392 
					unphased_ratio_data              = [];
 393 
				else
 394 
					commaCount                       = length(find(unphased_ratio_data_string==','));
 395 
					if (commaCount == 0)
 396 
						unphased_ratio_data      = str2num(unphased_ratio_data_string);
 397 
					else
 398 
						unphased_ratio_data      = strsplit(unphased_ratio_data_string,',');  % function converts number lists from strings to numbers.
 399 
					end;
 400 
				end;
 401 

 402 
				% format = '(number1,number2,...,numberN)'
 403 
				unphased_coordinates_string(1)           = [];
 404 
				unphased_coordinates_string(end)         = [];
 405 
				if (length(unphased_coordinates_string) == 0)
 406 
					unphased_coordinates             = [];
 407 
				else
 408 
					commaCount                       = length(find(unphased_coordinates_string==','));
 409 
					if (commaCount == 0)
 410 
						unphased_coordinates     = str2num(unphased_coordinates_string);
 411 
					else
 412 
						unphased_coordinates     = strsplit(unphased_coordinates_string,',');   % function converts number lists from strings to numbers.
 413 
					end;
 414 
				end;
 415 

 416 
				% format = '(A:A/T,C:G/C,...,T:A/T)'
 417 
				phased_alleles_string(1)                 = [];
 418 
				phased_alleles_string(end)               = [];
 419 
				if (length(phased_alleles_string)       == 0)
 420 
					phased_alleles                   = [];
 421 
				else
 422 
					commaCount                       = length(find(phased_alleles_string==','));
 423 
					if (commaCount == 0)
 424 
						phased_alleles           = phased_alleles_string;
 425 
					else
 426 
						phased_alleles           = strsplit(phased_alleles_string,',');   % function converts number lists from strings to numbers.
 427 
					end;
 428 
				end;
 429 

 430 
				% format = '(A:A/T,C:G/C,...,T:A/T)'
 431 
				unphased_alleles_string(1)               = [];
 432 
				unphased_alleles_string(end)             = [];
 433 
				if (length(unphased_alleles_string)     == 0)
 434 
					unphased_alleles                 = [];
 435 
				else
 436 
					commaCount                       = length(find(unphased_alleles_string==','));
 437 
					if (commaCount == 0)
 438 
						unphased_alleles         = unphased_alleles_string;
 439 
					else
 440 
						unphased_alleles         = strsplit(unphased_alleles_string,',');   % function converts number lists from strings to numbers.
 441 
					end;
 442 
				end;
 443 

 444 
				% add phased and unphased data to storage arrays.
 445 
				chr_SNPdata{chr_num,1}{chr_bin}          = phased_ratio_data;
 446 
				chr_SNPdata{chr_num,2}{chr_bin}          = unphased_ratio_data;
 447 

 448 
				% add phased and unphased data coordinates to storage arrays.
 449 
				chr_SNPdata{chr_num,3}{chr_bin}          = phased_coordinates;
 450 
				chr_SNPdata{chr_num,4}{chr_bin}          = unphased_coordinates;
 451 

 452 
				% add phased and unphased data allele strings to storage arrays.
 453 
				chr_SNPdata{chr_num,5}{chr_bin}          = phased_alleles;
 454 
				chr_SNPdata{chr_num,6}{chr_bin}          = unphased_alleles;
 455 
			end;
 456 
		end;
 457 
	end;
 458 
	fclose(data);
 459 

 460 
	save([projectDir 'SNP_' SNP_verString '.mat'],'chr_SNPdata');
      1 
 461 
else 
      1 
 462 
	fprintf('\t|\t\tMAT file found, loading.\n'); 
  0.31 
      1 
 463 
	load([projectDir 'SNP_' SNP_verString '.mat']); 
      1 
 464 
end; 
 465 

 466 

 467 
%%================================================================================================
 468 
% Process SNP/hapmap data to determine colors to be presented for each SNP locus.
 469 
%-------------------------------------------------------------------------------------------------
 470 
%    Calculate allelic fraction cutoffs for each segment and populate data structure containing
 471 
%    SNP phasing information.
 472 
%        chr_SNPdata{chr,1}{chr_bin} = phased SNP ratio data.
 473 
%        chr_SNPdata{chr,2}{chr_bin} = unphased SNP ratio data.
 474 
%        chr_SNPdata{chr,3}{chr_bin} = phased SNP position data.
 475 
%        chr_SNPdata{chr,4}{chr_bin} = unphased SNP position data.
 476 
%        chr_SNPdata{chr,5}{chr_bin} = phased SNP allele strings.   (baseCall:alleleA/alleleB)
 477 
%        chr_SNPdata{chr,6}{chr_bin} = unphased SNP allele strings.
 478 
%-------------------------------------------------------------------------------------------
 479 

      1 
 480 
fprintf('\t|\tCalculate allelic ratio cutoffs using Gaussian fitting.\n'); 
      1 
 481 
temp_holding = chr_SNPdata; 
  7.00 
      1 
 482 
calculate_allelic_ratio_cutoffs; 
      1 
 483 
chr_SNPdata = temp_holding; 
 484 

 485 

 486 
%% =========================================================================================
 487 
% Define new colors for SNPs, using Gaussian fitting crossover points as ratio cutoffs.
 488 
%-------------------------------------------------------------------------------------------
      1 
 489 
fprintf('\t|\tDetermine display color for each SNP.\n'); 
      1 
 490 
for chr = 1:num_chrs 
 491 
	% avoid running over chromosomes with empty copy number
      9 
 492 
	if (chr_in_use(chr) == 1 && ~isempty(chrCopyNum{chr})) 
      8 
 493 
		for chr_bin = 1:ceil(chr_size(chr)/bases_per_bin) 
 494 
			%
 495 
			% Determining colors for each SNP coordinate from calculated cutoffs.
 496 
			%
   3140 
 497 
			localCopyEstimate                       = round(CNVplot2{chr}(chr_bin)*ploidy*ploidyAdjust); 
  0.06 
   3140 
 498 
			allelic_ratios                          = [chr_SNPdata{chr,1}{chr_bin} chr_SNPdata{chr,2}{chr_bin}]; 
  0.03 
   3140 
 499 
			coordinates                             = [chr_SNPdata{chr,3}{chr_bin} chr_SNPdata{chr,4}{chr_bin}]; 
  0.01 
   3140 
 500 
			if (length(chr_SNPdata{chr,1}{chr_bin}) == 1) && (length(chr_SNPdata{chr,2}{chr_bin}) == 1) 
 501 
				allele_strings                  = {chr_SNPdata{chr,5}{chr_bin} chr_SNPdata{chr,6}{chr_bin}};
  0.01 
   3140 
 502 
			else 
  0.06 
   3140 
 503 
				allele_strings                  = [chr_SNPdata{chr,5}{chr_bin} chr_SNPdata{chr,6}{chr_bin}]; 
   3140 
 504 
			end; 
 505 

  0.01 
   3140 
 506 
			if (length(allelic_ratios) > 0) 
  0.01 
   2417 
 507 
				for SNP = 1:length(allelic_ratios) 
 508 
					% Load phased SNP data from earlier defined structure.
  0.05 
  63957 
 509 
					allelic_ratio                         = allelic_ratios(SNP); 
  0.19 
  63957 
 510 
					coordinate                            = coordinates(SNP); 
  0.22 
  63957 
 511 
					if (length(allelic_ratios) > 1) 
  0.22 
  63802 
 512 
						allele_string                 = allele_strings{SNP}; 
    155 
 513 
					else 
    155 
 514 
						allele_string                 = allele_strings; 
    155 
 515 
					end; 
  0.10 
  63957 
 516 
					baseCall                              = allele_string(1); 
  0.15 
  63957 
 517 
					homologA                              = allele_string(3); 
  0.09 
  63957 
 518 
					homologB                              = allele_string(5);					 
 519 

 520 
					% identify the segment containing the SNP.
  0.16 
  63957 
 521 
					segmentID                             = 0; 
  0.42 
  63957 
 522 
					for segment = 1:(length(chrCopyNum{chr})) 
  0.65 
 106731 
 523 
						segment_start                 = chr_breaks{chr}(segment  )*chr_size(chr); 
  0.68 
 106731 
 524 
						segment_end                   = chr_breaks{chr}(segment+1)*chr_size(chr); 
  0.21 
 106731 
 525 
						if (coordinate > segment_start) && (coordinate <= segment_end) 
  0.03 
  63957 
 526 
							segmentID             = segment; 
  0.13 
  63957 
 527 
						end; 
  0.13 
 106731 
 528 
					end; 
 529 

 530 
					% Load cutoffs between Gaussian fits performed earlier.
  0.25 
  63957 
 531 
					segment_copyNum                       = round(chrCopyNum{              chr}(segmentID)); 
  0.32 
  63957 
 532 
					actual_cutoffs                        = chrSegment_actual_cutoffs{     chr}{segmentID}; 
  0.32 
  63957 
 533 
					mostLikelyGaussians                   = chrSegment_mostLikelyGaussians{chr}{segmentID}; 
 534 

 535 
					% Calculate allelic ratio on range of [1..200].
  0.09 
  63957 
 536 
					SNPratio_int                          = (allelic_ratio)*199+1; 
 537 

 538 
					% Identify the allelic ratio region containing the SNP.
  0.23 
  63957 
 539 
					cutoffs                               = [1 actual_cutoffs 200]; 
  0.04 
  63957 
 540 
					ratioRegionID                         = 0; 
  0.07 
  63957 
 541 
					for GaussianRegionID = 1:length(mostLikelyGaussians) 
  0.31 
 219595 
 542 
						cutoff_start                  = cutoffs(GaussianRegionID  ); 
  0.22 
 219595 
 543 
						cutoff_end                    = cutoffs(GaussianRegionID+1); 
  0.24 
 219595 
 544 
						if (GaussianRegionID == 1) 
  0.10 
  63957 
 545 
							if (SNPratio_int >= cutoff_start) && (SNPratio_int <= cutoff_end) 
     26 
 546 
								ratioRegionID = mostLikelyGaussians(GaussianRegionID); 
     26 
 547 
							end; 
  0.12 
 155638 
 548 
						else 
  0.15 
 155638 
 549 
							if (SNPratio_int > cutoff_start) && (SNPratio_int <= cutoff_end) 
  0.11 
  63931 
 550 
								ratioRegionID = mostLikelyGaussians(GaussianRegionID); 
  0.05 
  63931 
 551 
							end; 
  0.13 
 155638 
 552 
						end; 
  0.39 
 219595 
 553 
					end; 
 554 

  0.13 
  63957 
 555 
					if (segment_copyNum <= 0);                          colorList = colorNoData; 
  0.09 
  63957 
 556 
					elseif (segment_copyNum == 1) 
 557 
						% allelic fraction cutoffs: [0.50000] => [A B]
    382 
 558 
						if ((baseCall == homologA) || (baseCall == homologB)) 
 559 
							if (ratioRegionID == 2);            colorList = colorB;
 560 
							else                                colorList = colorA;
 561 
							end;
    382 
 562 
						else 
    382 
 563 
							if (useParent)                      colorList = unphased_color_1of1; 
 564 
							else                                colorList = colorNoData;
 565 
							end;
    382 
 566 
						end; 
  0.10 
  63575 
 567 
					elseif (segment_copyNum == 2) 
 568 
						%   allelic fraction cutoffs: [0.25000 0.75000] => [AA AB BB]
  0.23 
  58373 
 569 
						if ((baseCall == homologA) || (baseCall == homologB)) 
 570 
							if (ratioRegionID == 3);            colorList = colorBB;
 571 
							elseif (ratioRegionID == 2);        colorList = colorAB;
 572 
							else                                colorList = colorAA;
 573 
							end;
  0.10 
  58373 
 574 
						else 
  0.05 
  58373 
 575 
							if (ratioRegionID == 3);            colorList = unphased_color_2of2; 
  0.09 
  56778 
 576 
							elseif (ratioRegionID == 2);        colorList = unphased_color_1of2; 
   1273 
 577 
							else                                colorList = unphased_color_2of2; 
  0.01 
   1273 
 578 
							end; 
  0.12 
  58373 
 579 
						end; 
   5202 
 580 
					elseif (segment_copyNum == 3) 
 581 
						% allelic fraction cutoffs: [0.16667 0.50000 0.83333] => [AAA AAB ABB BBB]
  0.03 
   5202 
 582 
						if ((baseCall == homologA) || (baseCall == homologB)) 
 583 
							if (ratioRegionID == 4);            colorList = colorBBB;
 584 
							elseif (ratioRegionID == 3);        colorList = colorABB;
 585 
							elseif (ratioRegionID == 2);        colorList = colorAAB;
 586 
							else                                colorList = colorAAA;
 587 
							end;
  0.01 
   5202 
 588 
						else 
  0.01 
   5202 
 589 
							if (ratioRegionID == 4);            colorList = unphased_color_3of3; 
   5117 
 590 
							elseif (ratioRegionID == 3);        colorList = unphased_color_2of3; 
     17 
 591 
							elseif (ratioRegionID == 2);        colorList = unphased_color_2of3; 
 592 
							else                                colorList = unphased_color_3of3;
 593 
							end;
  0.03 
   5202 
 594 
						end; 
 595 
					elseif (segment_copyNum == 4)
 596 
						% allelic fraction cutoffs: [0.12500 0.37500 0.62500 0.87500] => [AAAA AAAB AABB ABBB BBBB]
 597 
						if ((baseCall == homologA) || (baseCall == homologB))
 598 
							if (ratioRegionID == 5);            colorList = colorBBBB;
 599 
							elseif (ratioRegionID == 4);        colorList = colorABBB;
 600 
							elseif (ratioRegionID == 3);        colorList = colorAABB;
 601 
							elseif (ratioRegionID == 2);        colorList = colorAAAB;
 602 
							else                                colorList = colorAAAA;
 603 
							end;
 604 
						else
 605 
							if (ratioRegionID == 5);            colorList = unphased_color_4of4;
 606 
							elseif (ratioRegionID == 4);        colorList = unphased_color_3of4;
 607 
							elseif (ratioRegionID == 3);        colorList = unphased_color_2of4;
 608 
							elseif (ratioRegionID == 2);        colorList = unphased_color_3of4;
 609 
							else                                colorList = unphased_color_4of4;
 610 
							end;
 611 
						end;
 612 
					elseif (segment_copyNum == 5)
 613 
						% allelic fraction cutoffs: [0.10000 0.30000 0.50000 0.70000 0.90000] => [AAAAA AAAAB AAABB AABBB ABBBB BBBBB]
 614 
						if ((baseCall == homologA) || (baseCall == homologB))
 615 
							if (ratioRegionID == 6);            colorList = colorBBBBB;
 616 
							elseif (ratioRegionID == 5);        colorList = colorABBBB;
 617 
							elseif (ratioRegionID == 4);        colorList = colorAABBB;
 618 
							elseif (ratioRegionID == 3);        colorList = colorAAABB;
 619 
							elseif (ratioRegionID == 2);        colorList = colorAAAAB;
 620 
							else                                colorList = colorAAAAA;
 621 
							end;
 622 
						else
 623 
							if (ratioRegionID == 6);            colorList = unphased_color_5of5;
 624 
							elseif (ratioRegionID == 5);        colorList = unphased_color_4of5;
 625 
							elseif (ratioRegionID == 4);        colorList = unphased_color_3of5;
 626 
							elseif (ratioRegionID == 3);        colorList = unphased_color_3of5;
 627 
							elseif (ratioRegionID == 2);        colorList = unphased_color_4of5;
 628 
							else                                colorList = unphased_color_5of5;
 629 
							end;
 630 
						end;
 631 
					elseif (segment_copyNum == 6)
 632 
						% allelic fraction cutoffs: [0.08333 0.25000 0.41667 0.58333 0.75000 0.91667] => [AAAAAA AAAAAB AAAABB AAABBB AABBBB ABBBBB BBBBBB]
 633 
						if ((baseCall == homologA) || (baseCall == homologB))
 634 
							if (ratioRegionID == 7);            colorList = colorBBBBBB;
 635 
							elseif (ratioRegionID == 6);        colorList = colorABBBBB;
 636 
							elseif (ratioRegionID == 5);        colorList = colorAABBBB;
 637 
							elseif (ratioRegionID == 4);        colorList = colorAAABBB;
 638 
							elseif (ratioRegionID == 3);        colorList = colorAAAABB;
 639 
							elseif (ratioRegionID == 2);        colorList = colorAAAAAB;
 640 
							else                                colorList = colorAAAAAA;
 641 
							end;
 642 
						else
 643 
							if (ratioRegionID == 7);            colorList = unphased_color_6of6;
 644 
							elseif (ratioRegionID == 6);        colorList = unphased_color_5of6;
 645 
							elseif (ratioRegionID == 5);        colorList = unphased_color_4of6;
 646 
							elseif (ratioRegionID == 4);        colorList = unphased_color_3of6;
 647 
							elseif (ratioRegionID == 3);        colorList = unphased_color_4of6;
 648 
							elseif (ratioRegionID == 2);        colorList = unphased_color_5of6;
 649 
							else                                colorList = unphased_color_6of6;
 650 
							end;
 651 
						end;
 652 
					elseif (segment_copyNum == 7)
 653 
						% allelic fraction cutoffs: [0.07143 0.21429 0.35714 0.50000 0.64286 0.78571 0.92857] => [AAAAAAA AAAAAAB AAAAABB AAAABBB AAABBBB AABBBBB ABBBBBB BBBBBBB]
 654 
						if ((baseCall == homologA) || (baseCall == homologB))
 655 
							if (ratioRegionID == 8);            colorList = colorBBBBBBB;
 656 
							elseif (ratioRegionID == 7);        colorList = colorABBBBBB;
 657 
							elseif (ratioRegionID == 6);        colorList = colorAABBBBB;
 658 
							elseif (ratioRegionID == 5);        colorList = colorAAABBBB;
 659 
							elseif (ratioRegionID == 4);        colorList = colorAAAABBB;
 660 
							elseif (ratioRegionID == 3);        colorList = colorAAAAABB;
 661 
							elseif (ratioRegionID == 2);        colorList = colorAAAAAAB;
 662 
							else                                colorList = colorAAAAAAA;
 663 
							end;
 664 
						else
 665 
							if (ratioRegionID == 8);            colorList = unphased_color_7of7;
 666 
							elseif (ratioRegionID == 7);        colorList = unphased_color_6of7;
 667 
							elseif (ratioRegionID == 6);        colorList = unphased_color_5of7;
 668 
							elseif (ratioRegionID == 5);        colorList = unphased_color_4of7;
 669 
							elseif (ratioRegionID == 3);        colorList = unphased_color_4of7;
 670 
							elseif (ratioRegionID == 3);        colorList = unphased_color_5of7;
 671 
							elseif (ratioRegionID == 2);        colorList = unphased_color_6of7;
 672 
							else                                colorList = unphased_color_7of7;
 673 
							end;
 674 
						end;
 675 
					elseif (segment_copyNum == 8)
 676 
						% allelic fraction cutoffs: [0.06250 0.18750 0.31250 0.43750 0.56250 0.68750 0.81250 0.93750] => [AAAAAAAA AAAAAAAB AAAAAABB AAAAABBB AAAABBBB AAABBBBB AABBBBBB ABBBBBBB BBBBBBBB]
 677 
						if ((baseCall == homologA) || (baseCall == homologB))
 678 
							if (ratioRegionID == 9);            colorList = colorBBBBBBBB;
 679 
							elseif (ratioRegionID == 8);        colorList = colorABBBBBBB;
 680 
							elseif (ratioRegionID == 7);        colorList = colorAABBBBBB;
 681 
							elseif (ratioRegionID == 6);        colorList = colorAAABBBBB;
 682 
							elseif (ratioRegionID == 5);        colorList = colorAAAABBBB;
 683 
							elseif (ratioRegionID == 4);        colorList = colorAAAAABBB;
 684 
							elseif (ratioRegionID == 3);        colorList = colorAAAAAABB;
 685 
							elseif (ratioRegionID == 2);        colorList = colorAAAAAAAB;
 686 
							else                                colorList = colorAAAAAAAA;
 687 
							end;
 688 
						else
 689 
							if (ratioRegionID == 9);            colorList = unphased_color_8of8;
 690 
							elseif (ratioRegionID == 8);        colorList = unphased_color_7of8;
 691 
							elseif (ratioRegionID == 7);        colorList = unphased_color_6of8;
 692 
							elseif (ratioRegionID == 6);        colorList = unphased_color_5of8;
 693 
							elseif (ratioRegionID == 5);        colorList = unphased_color_4of8;
 694 
							elseif (ratioRegionID == 4);        colorList = unphased_color_5of8;
 695 
							elseif (ratioRegionID == 3);        colorList = unphased_color_6of8;
 696 
							elseif (ratioRegionID == 2);        colorList = unphased_color_7of8;
 697 
							else                                colorList = unphased_color_8of8;
 698 
							end;
 699 
						end;
 700 
					elseif (segment_copyNum >= 9)
 701 
						% allelic fraction cutoffs: [0.05556 0.16667 0.27778 0.38889 0.50000 0.61111 0.72222 0.83333 0.94444] => [AAAAAAAAA AAAAAAAAB AAAAAAABB AAAAAABBB AAAAABBBB AAAABBBBB AAABBBBBB AABBBBBBB
 702 
						%                                                                                                         ABBBBBBBB BBBBBBBBB]
 703 
						if ((baseCall == homologA) || (baseCall == homologB))
 704 
							if (ratioRegionID == 10);           colorList = colorBBBBBBBBB;
 705 
							elseif (ratioRegionID == 9);        colorList = colorABBBBBBBB;
 706 
							elseif (ratioRegionID == 8);        colorList = colorAABBBBBBB;
 707 
							elseif (ratioRegionID == 7);        colorList = colorAAABBBBBB;
 708 
							elseif (ratioRegionID == 6);        colorList = colorAAAABBBBB;
 709 
							elseif (ratioRegionID == 5);        colorList = colorAAAAABBBB;
 710 
							elseif (ratioRegionID == 4);        colorList = colorAAAAAABBB;
 711 
							elseif (ratioRegionID == 3);        colorList = colorAAAAAAABB;
 712 
							elseif (ratioRegionID == 2);        colorList = colorAAAAAAAAB;
 713 
							else                                colorList = colorAAAAAAAAA;
 714 
							end;
 715 
						else
 716 
							if (ratioRegionID == 10);           colorList = unphased_color_9of9;
 717 
							elseif (ratioRegionID == 9);        colorList = unphased_color_8of9;
 718 
							elseif (ratioRegionID == 8);        colorList = unphased_color_7of9;
 719 
							elseif (ratioRegionID == 7);        colorList = unphased_color_6of9;
 720 
							elseif (ratioRegionID == 6);        colorList = unphased_color_5of9;
 721 
							elseif (ratioRegionID == 5);        colorList = unphased_color_5of9;
 722 
							elseif (ratioRegionID == 4);        colorList = unphased_color_6of9;
 723 
							elseif (ratioRegionID == 3);        colorList = unphased_color_7of9;
 724 
							elseif (ratioRegionID == 2);        colorList = unphased_color_8of9;
 725 
							else                                colorList = unphased_color_9of9;
 726 
							end;
 727 
						end;
 728 
					end;
  0.17 
  63957 
 729 
					chr_SNPdata_colorsC{chr,1}(chr_bin) = chr_SNPdata_colorsC{chr,1}(chr_bin) + colorList(1); 
  0.12 
  63957 
 730 
					chr_SNPdata_colorsC{chr,2}(chr_bin) = chr_SNPdata_colorsC{chr,2}(chr_bin) + colorList(2); 
  0.14 
  63957 
 731 
					chr_SNPdata_colorsC{chr,3}(chr_bin) = chr_SNPdata_colorsC{chr,3}(chr_bin) + colorList(3); 
  0.22 
  63957 
 732 
					chr_SNPdata_countC{ chr  }(chr_bin) = chr_SNPdata_countC{ chr  }(chr_bin) + 1; 
 733 

 734 
					% Troubleshooting output.
 735 
					% fprintf(['chr = ' num2str(chr) '; seg = ' num2str(segment) '; bin = ' num2str(chr_bin) '; ratioRegionID = ' num2str(ratioRegionID) '\n']);
  0.08 
  63957 
 736 
				end; 
   2417 
 737 
			end; 
   3140 
 738 
		end; 
 739 

 740 
		%
 741 
		% Average colors of SNPs found in bin.
 742 
		%
      8 
 743 
		fprintf('\t|\tDetermine average color for SNPs in chromosome bin.\n'); 
      8 
 744 
		for chr_bin = 1:ceil(chr_size(chr)/bases_per_bin) 
  0.05 
   3140 
 745 
			allelic_ratios                                      = [chr_SNPdata{chr,1}{chr_bin} chr_SNPdata{chr,2}{chr_bin}]; 
   3140 
 746 
			if (length(allelic_ratios) > 0) 
   2417 
 747 
				if (chr_SNPdata_countC{chr}(chr_bin) > 0) 
   2417 
 748 
					chr_SNPdata_colorsC{chr,1}(chr_bin) = chr_SNPdata_colorsC{chr,1}(chr_bin)/chr_SNPdata_countC{chr}(chr_bin); 
  0.01 
   2417 
 749 
					chr_SNPdata_colorsC{chr,2}(chr_bin) = chr_SNPdata_colorsC{chr,2}(chr_bin)/chr_SNPdata_countC{chr}(chr_bin); 
   2417 
 750 
					chr_SNPdata_colorsC{chr,3}(chr_bin) = chr_SNPdata_colorsC{chr,3}(chr_bin)/chr_SNPdata_countC{chr}(chr_bin); 
 751 
				else
 752 
					chr_SNPdata_colorsC{chr,1}(chr_bin) = 1.0;
 753 
					chr_SNPdata_colorsC{chr,2}(chr_bin) = 1.0;
 754 
					chr_SNPdata_colorsC{chr,3}(chr_bin) = 1.0;
 755 
				end;
    723 
 756 
			else 
    723 
 757 
				chr_SNPdata_colorsC{chr,1}(chr_bin)         = 1.0; 
    723 
 758 
				chr_SNPdata_colorsC{chr,2}(chr_bin)         = 1.0; 
    723 
 759 
				chr_SNPdata_colorsC{chr,3}(chr_bin)         = 1.0; 
    723 
 760 
			end; 
  0.02 
   3140 
 761 
		end; 
      8 
 762 
	end; 
      9 
 763 
end; 
 764 

 765 

 766 
%%================================================================================================
 767 
% Setup for main figure generation.
 768 
%-------------------------------------------------------------------------------------------------
 769 
% threshold for full color saturation in SNP/LOH figure.
 770 
% synced to bases_per_bin as below, or defaulted to 50.
      1 
 771 
fprintf('\t|\tCalculate color intensity for each chromosome bin.\n'); 
      1 
 772 
largestChr          = find(chr_width == max(chr_width)); 
      1 
 773 
largestChr          = largestChr(1); 
      1 
 774 
full_data_threshold = floor(bases_per_bin/100); 
      1 
 775 
for chr = 1:num_chrs 
      9 
 776 
	if (chr_in_use(chr) == 1) 
      8 
 777 
		for chr_bin = 1:ceil(chr_size(chr)/bases_per_bin) 
 778 
			% the number of heterozygous data points in this bin.
  0.07 
   3140 
 779 
			SNPs_count{chr}(chr_bin)                                     = length(chr_SNPdata{chr,1}{chr_bin}) + length(chr_SNPdata{chr,2}{chr_bin}); 
 780 

 781 
			% divide by the threshold for full color saturation in SNP/LOH figure.
  0.01 
   3140 
 782 
			SNPs_to_fullData_ratio{chr}(chr_bin)                         = SNPs_count{chr}(chr_bin)/full_data_threshold; 
 783 

 784 
			% any bins with more data than the threshold for full color saturation are limited to full saturation.
  0.02 
   3140 
 785 
			SNPs_to_fullData_ratio{chr}(SNPs_to_fullData_ratio{chr} > 1) = 1; 
 786 

  0.02 
   3140 
 787 
			phased_plot{ chr}(chr_bin)                                   = length(chr_SNPdata{chr,1}{chr_bin});             % phased data. 
   3140 
 788 
			phased_plot2{chr}(chr_bin)                                   = phased_plot{chr}(chr_bin)/full_data_threshold;   % 
   3140 
 789 
			phased_plot2{chr}(phased_plot2{chr} > 1)                     = 1;                                               % 
 790 

  0.02 
   3140 
 791 
			unphased_plot{ chr}(chr_bin)                                 = length(chr_SNPdata{chr,2}{chr_bin});             % unphased data. 
  0.02 
   3140 
 792 
			unphased_plot2{chr}(chr_bin)                                 = unphased_plot{chr}(chr_bin)/full_data_threshold; % 
  0.03 
   3140 
 793 
			unphased_plot2{chr}(unphased_plot2{chr} > 1)                 = 1;                                               % 
  0.02 
   3140 
 794 
		end; 
      8 
 795 
	end; 
      9 
 796 
end; 
      1 
 797 
fprintf('\n'); 
 798 

 799 
% load size definitions
  0.01 
      1 
 800 
[linear_fig_height,linear_fig_width,Linear_left_start,Linear_chr_gap,Linear_Chr_max_width,Linear_height... 
 801 
    ,Linear_base,rotate,linear_chr_font_size,linear_axis_font_size,linear_gca_font_size,stacked_fig_height,...
 802 
    stacked_fig_width,stacked_chr_font_size,stacked_title_size,stacked_axis_font_size,...
 803 
    gca_stacked_font_size,stacked_copy_font_size,max_chrom_label_size] = Load_size_info(chr_in_use,num_chrs,chr_label,chr_size);
 804 

 805 
%%================================================================================================
 806 
% Setup for main-view figure generation.
 807 
%-------------------------------------------------------------------------------------------------
      1 
 808 
fprintf('\t|\tIntitial setup of main figure.\n'); 
  0.04 
      1 
 809 
fig = figure(1); 
 810 
%set(gcf, 'Position', [0 70 1024 600]);
 811 
% basic plot parameters not defined per genome.
      1 
 812 
TickSize         = -0.005;  %negative for outside, percentage of longest chr figure. 
      1 
 813 
maxY             = ploidyBase*2; 
      1 
 814 
cen_tel_Xindent  = 5; 
      1 
 815 
cen_tel_Yindent  = maxY/5; 
 816 

 817 

 818 
%%================================================================================================
 819 
% Setup for linear-view figure generation.
 820 
%-------------------------------------------------------------------------------------------------
      1 
 821 
if (Linear_display == true) 
      1 
 822 
	fprintf('\t|\tInitial setup of linear figure.\n'); 
  0.04 
      1 
 823 
	Linear_fig = figure(2); 
      1 
 824 
	Linear_genome_size   = sum(chr_size); 
      1 
 825 
	Linear_TickSize      = -0.01;  %negative for outside, percentage of longest chr figure. 
      1 
 826 
	maxY                 = ploidyBase*2; 
      1 
 827 
	Linear_left          = Linear_left_start; 
      1 
 828 
	axisLabelPosition_horiz = 0.01125; 
      1 
 829 
end; 
      1 
 830 
axisLabelPosition_vert = 0.01125; 
 831 

 832 

 833 
%%================================================================================================
 834 
% Make figures
 835 
%-------------------------------------------------------------------------------------------------
      1 
 836 
fprintf('\t|\tDraw figures.\n'); 
      1 
 837 
first_chr = true; 
      1 
 838 
for chr = 1:num_chrs 
      9 
 839 
	if (chr_in_use(chr) == 1) 
      8 
 840 
		figure(fig); 
 841 
		% make standard chr cartoons.
      8 
 842 
		left          = chr_posX(chr); 
      8 
 843 
		bottom        = chr_posY(chr); 
      8 
 844 
		width         = chr_width(chr); 
      8 
 845 
		height        = chr_height(chr); 
  0.03 
      8 
 846 
		subPlotHandle = subplot('Position',[left bottom width height]); 
  0.01 
      8 
 847 
		fprintf(['\tfigposition = [' num2str(left) ' | ' num2str(bottom) ' | ' num2str(width) ' | ' num2str(height) ']\n']); 
      8 
 848 
		hold on; 
 849 

      8 
 850 
		c_prev = colorInit; 
      8 
 851 
		c_post = colorInit; 
      8 
 852 
		c_     = c_prev; 
      8 
 853 
		infill = zeros(1,length(phased_plot2{chr})); 
      8 
 854 
		colors = []; 
 855 

 856 

 857 
		%% standard : determine color of each bin.
      8 
 858 
		for chr_bin = 1:ceil(chr_size(chr)/bases_per_bin) 
  0.05 
   3140 
 859 
			c_tot_post = SNPs_to_fullData_ratio{chr}(chr_bin)+SNPs_to_fullData_ratio{chr}(chr_bin); 
  0.02 
   3140 
 860 
			if (c_tot_post == 0) 
    723 
 861 
				c_post = colorNoData; 
  0.02 
    723 
 862 
				fprintf('.'); 
  0.01 
    723 
 863 
				if (mod(chr_bin,100) == 0);   fprintf('\n');   end; 
   2417 
 864 
			else 
 865 
				% Average of SNP position colors defined earlier.
  0.01 
   2417 
 866 
				colorMix = [chr_SNPdata_colorsC{chr,1}(chr_bin) chr_SNPdata_colorsC{chr,2}(chr_bin) chr_SNPdata_colorsC{chr,3}(chr_bin)]; 
 867 

 868 
				% Determine color to draw bin, accounting for limited data and data saturation.
  0.07 
   2417 
 869 
				c_post =   colorMix   *   min(1,SNPs_to_fullData_ratio{chr}(chr_bin)) + ... 
 870 
				           colorNoData*(1-min(1,SNPs_to_fullData_ratio{chr}(chr_bin)));
  0.01 
   2417 
 871 
			end; 
  0.02 
   3140 
 872 
			colors(chr_bin,1) = c_post(1); 
  0.01 
   3140 
 873 
			colors(chr_bin,2) = c_post(2); 
   3140 
 874 
			colors(chr_bin,3) = c_post(3); 
  0.01 
   3140 
 875 
		end; 
 876 
		% standard : end determine color of each bin.
 877 

 878 

 879 
		%% standard : draw colorbars.
      8 
 880 
		for chr_bin = 1:ceil(chr_size(chr)/bases_per_bin) 
   3140 
 881 
			x_ = [chr_bin chr_bin chr_bin-1 chr_bin-1]; 
   3140 
 882 
			y_ = [0 maxY maxY 0]; 
  0.02 
   3140 
 883 
			c_post(1) = colors(chr_bin,1); 
   3140 
 884 
			c_post(2) = colors(chr_bin,2); 
   3140 
 885 
			c_post(3) = colors(chr_bin,3); 
 886 
			% makes a colorBar for each bin, using local smoothing
   3140 
 887 
			if (c_(1) > 1); c_(1) = 1; end; 
   3140 
 888 
			if (c_(2) > 1); c_(2) = 1; end; 
  0.01 
   3140 
 889 
			if (c_(3) > 1); c_(3) = 1; end; 
   3140 
 890 
			if (blendColorBars == false) 
  3.19 
   3140 
 891 
				f = fill(x_,y_,c_); 
 892 
			else
 893 
				f = fill(x_,y_,c_/2+c_prev/4+c_post/4);
 894 
			end;
   3140 
 895 
			c_prev = c_; 
   3140 
 896 
			c_     = c_post; 
  0.21 
   3140 
 897 
			set(f,'linestyle','none'); 
  0.01 
   3140 
 898 
		end; 
 899 

 900 
		% standard : axes labels etc.
  0.01 
      8 
 901 
		hold off; 
      8 
 902 
		xlim([0,chr_size(chr)/bases_per_bin]); 
 903 

 904 
		%% standard : modify y axis limits to show annotation locations if any are provided.
      8 
 905 
		if (length(annotations) > 0) 
 906 
			ylim([-maxY/10*1.5,maxY]);
      8 
 907 
		else 
      8 
 908 
			ylim([0,maxY]); 
      8 
 909 
		end; 
      8 
 910 
		set(gca,'TickLength',[(TickSize*chr_size(largestChr)/chr_size(chr)) 0]); %ensures same tick size on all subfigs. 
      8 
 911 
		set(gca,'YTick',[]); 
  0.01 
      8 
 912 
		set(gca,'YTickLabel',[]); 
      8 
 913 
		set(gca,'XTick',0:(40*(5000/bases_per_bin)):(650*(5000/bases_per_bin))); 
      8 
 914 
		set(gca,'XTickLabel',{'0.0','0.2','0.4','0.6','0.8','1.0','1.2','1.4','1.6','1.8','2.0','2.2','2.4','2.6','2.8','3.0','3.2'}); 
      8 
 915 
		text(-50000/5000/2*3, maxY/2,     chr_label{chr}, 'Rotation',90, 'HorizontalAlignment','center', 'VerticalAlign','bottom', 'Fontsize',stacked_chr_font_size); 
 916 

      8 
 917 
		set(gca,'FontSize',gca_stacked_font_size); 
      8 
 918 
		if (chr == find(chr_posY == max(chr_posY))) 
  0.02 
      1 
 919 
			title([ project ' vs. (hapmap)' hapmap ' SNP/LOH map'],'Interpreter','none','FontSize',stacked_title_size); 
      1 
 920 
		end; 
      8 
 921 
		hold on; 
 922 
		% standard : end axes labels etc.
 923 
    
 924 
		% standard : show segmental anueploidy breakpoints.
      8 
 925 
                if (displayBREAKS == true) && (show_annotations == true) 
      8 
 926 
                        chr_length = ceil(chr_size(chr)/bases_per_bin); 
      8 
 927 
                        for segment = 2:length(chr_breaks{chr})-1 
      4 
 928 
                                bP = chr_breaks{chr}(segment)*chr_length; 
  0.01 
      4 
 929 
                                plot([bP bP], [(-maxY/10*2.5) 0],  'Color',[1 0 0],'LineWidth',2); 
      4 
 930 
                        end; 
      8 
 931 
                end; 
 932 

 933 
		% show centromere outlines and horizontal marks.
      8 
 934 
		x1 = cen_start(chr)/bases_per_bin; 
      8 
 935 
		x2 = cen_end(chr)/bases_per_bin; 
      8 
 936 
		leftEnd  = 0.5*5000/bases_per_bin; 
      8 
 937 
		rightEnd = (chr_size(chr) - 0.5*5000)/bases_per_bin; 
      8 
 938 
		if (Centromere_format == 0) 
 939 
		        % standard chromosome cartoons in a way which will not cause segfaults when running via commandline.
      8 
 940 
		        dx = cen_tel_Xindent; %5*5000/bases_per_bin; 
      8 
 941 
		        dy = cen_tel_Yindent; %maxY/10; 
 942 
		        % draw white triangles at corners and centromere locations.
 943 
		        % top left corner.
      8 
 944 
		        c_ = [1.0 1.0 1.0]; 
      8 
 945 
		        x_ = [leftEnd   leftEnd   leftEnd+dx]; 
      8 
 946 
		        y_ = [maxY-dy   maxY      maxY      ]; 
      8 
 947 
		        f = fill(x_,y_,c_); 
      8 
 948 
		        set(f,'linestyle','none'); 
 949 
		        % bottom left corner.
      8 
 950 
		        x_ = [leftEnd   leftEnd   leftEnd+dx]; 
      8 
 951 
		        y_ = [dy        0         0         ]; 
  0.01 
      8 
 952 
	        	f = fill(x_,y_,c_); 
      8 
 953 
		        set(f,'linestyle','none'); 
 954 
		        % top right corner.
      8 
 955 
	        	x_ = [rightEnd   rightEnd   rightEnd-dx]; 
      8 
 956 
		        y_ = [maxY-dy    maxY       maxY      ]; 
  0.01 
      8 
 957 
		        f = fill(x_,y_,c_); 
      8 
 958 
	        	set(f,'linestyle','none'); 
 959 
		        % bottom right corner.
      8 
 960 
		        x_ = [rightEnd   rightEnd   rightEnd-dx]; 
      8 
 961 
	        	y_ = [dy         0          0         ]; 
      8 
 962 
		        f = fill(x_,y_,c_); 
  0.01 
      8 
 963 
		        set(f,'linestyle','none'); 
 964 
	        	% top centromere.
      8 
 965 
		        x_ = [x1-dx   x1        x2        x2+dx]; 
      8 
 966 
		        y_ = [maxY    maxY-dy   maxY-dy   maxY]; 
  0.03 
      8 
 967 
	        	f = fill(x_,y_,c_); 
      8 
 968 
		        set(f,'linestyle','none'); 
 969 
		        % bottom centromere.
      8 
 970 
	        	x_ = [x1-dx   x1   x2   x2+dx]; 
      8 
 971 
		        y_ = [0       dy   dy   0    ]; 
      8 
 972 
		        f = fill(x_,y_,c_); 
      8 
 973 
	        	set(f,'linestyle','none'); 
 974 
        
 975 
			% draw outlines of chromosome cartoon.   (drawn after horizontal lines to that cartoon edges are not interrupted by horiz lines.
  0.01 
      8 
 976 
			plot([leftEnd   leftEnd   leftEnd+dx   x1-dx   x1        x2        x2+dx   rightEnd-dx   rightEnd   rightEnd   rightEnd-dx   x2+dx   x2   x1   x1-dx   leftEnd+dx   leftEnd],... 
 977 
			     [dy        maxY-dy   maxY         maxY    maxY-dy   maxY-dy   maxY    maxY          maxY-dy    dy         0             0       dy   dy   0       0            dy     ],...
 978 
			     'Color',[0 0 0]);
      8 
 979 
		end; 
 980 
		%end show centromere.
 981 
    
 982 
		%show annotation locations
      8 
 983 
		if (show_annotations) && (length(annotations) > 0) 
 984 
			plot([leftEnd rightEnd], [-maxY/10*1.5 -maxY/10*1.5],'color',[0 0 0]);
 985 
			hold on;
 986 
			annotation_location = (annotation_start+annotation_end)./2;
 987 
			for annoteID = 1:length(annotation_location)
 988 
				if (annotation_chr(annoteID) == chr)
 989 
					annotationloc   = annotation_location(annoteID)/bases_per_bin-0.5*(5000/bases_per_bin);
 990 
					annotationStart = annotation_start(   annoteID)/bases_per_bin-0.5*(5000/bases_per_bin);
 991 
					annotationEnd   = annotation_end(     annoteID)/bases_per_bin-0.5*(5000/bases_per_bin);
 992 
					if (strcmp(annotation_type{annoteID},'dot') == 1)
 993 
						plot(annotationloc,-maxY/10*1.5,'k:o','MarkerEdgeColor',annotation_edgecolor{annoteID}, ...
 994 
						     'MarkerFaceColor',annotation_fillcolor{annoteID}, ...
 995 
						     'MarkerSize',     annotation_size(annoteID));
 996 
					elseif (strcmp(annotation_type{annoteID},'block') == 1)
 997 
						fill([annotationStart annotationStart annotationEnd annotationEnd], ...
 998 
						     [-maxY/10*(1.5+0.75) -maxY/10*(1.5-0.75) -maxY/10*(1.5-0.75) -maxY/10*(1.5+0.75)], ...
 999 
						     annotation_fillcolor{annoteID},'EdgeColor',annotation_edgecolor{annoteID});
1000 
					end;
1001 
				end;
1002 
			end;
1003 
			hold off;
1004 
		end;
1005 
		%end show annotation locations.
1006 

1007 

1008 
		%% =========================================================================================
1009 
		% Draw angleplots to left of main chromosome cartoons.
1010 
		%-------------------------------------------------------------------------------------------
      8 
1011 
		apply_phasing = true; 
  0.95 
      8 
1012 
		angle_plot_subfigures; 
1013 

1014 

1015 
%%%%%%%%%%%%%%%% Linear figure draw section
1016 

1017 

1018 
		%% Linear figure draw section
      8 
1019 
		if (Linear_display == true) 
      8 
1020 
			figure(Linear_fig); 
      8 
1021 
			Linear_width = Linear_Chr_max_width*chr_size(chr)/Linear_genome_size; 
  0.04 
      8 
1022 
			subplot('Position',[Linear_left Linear_base Linear_width Linear_height]); 
      8 
1023 
			Linear_left = Linear_left + Linear_width + Linear_chr_gap; 
      8 
1024 
			hold on; 
1025 

1026 
			%% linear : draw colorbars.
      8 
1027 
			for chr_bin = 1:ceil(chr_size(chr)/bases_per_bin) 
  0.04 
   3140 
1028 
				x_ = [chr_bin chr_bin chr_bin-1 chr_bin-1]; 
  0.01 
   3140 
1029 
				y_ = [0 maxY maxY 0]; 
   3140 
1030 
				c_post(1) = colors(chr_bin,1); 
  0.01 
   3140 
1031 
				c_post(2) = colors(chr_bin,2); 
   3140 
1032 
				c_post(3) = colors(chr_bin,3); 
1033 
				% makes a colorBar for each bin, using local smoothing
   3140 
1034 
				if (c_(1) > 1); c_(1) = 1; end; 
   3140 
1035 
				if (c_(2) > 1); c_(2) = 1; end; 
  0.01 
   3140 
1036 
				if (c_(3) > 1); c_(3) = 1; end; 
   3140 
1037 
				if (blendColorBars == false) 
  3.13 
   3140 
1038 
					f = fill(x_,y_,c_); 
1039 
				else
1040 
					f = fill(x_,y_,c_/2+c_prev/4+c_post/4);
1041 
				end;
   3140 
1042 
				c_prev = c_; 
   3140 
1043 
				c_     = c_post; 
  0.26 
   3140 
1044 
				set(f,'linestyle','none'); 
   3140 
1045 
			end; 
1046 
			% linear : end draw colorbars.
1047 

1048 
			%% linear : show segmental anueploidy breakpoints.
      8 
1049 
			if (Linear_displayBREAKS == true) && (show_annotations == true) 
1050 
				chr_length = ceil(chr_size(chr)/bases_per_bin);
1051 
				for segment = 2:length(chr_breaks{chr})-1
1052 
					bP = chr_breaks{chr}(segment)*chr_length;
1053 
					plot([bP bP], [(-maxY/10*2.5) 0],  'Color',[1 0 0],'LineWidth',2);
1054 
				end;
1055 
			end;
1056 

1057 
			%% linear : show centromere.
      8 
1058 
			x1 = cen_start(chr)/bases_per_bin; 
      8 
1059 
			x2 = cen_end(chr)/bases_per_bin; 
      8 
1060 
			leftEnd  = 0.5*5000/bases_per_bin; 
      8 
1061 
			rightEnd = (chr_size(chr) - 0.5*5000)/bases_per_bin; 
      8 
1062 
			if (Centromere_format == 0) 
1063 
				% standard chromosome cartoons in a way which will not cause segfaults when running via commandline.
      8 
1064 
				dx = cen_tel_Xindent; %5*5000/bases_per_bin; 
      8 
1065 
				dy = cen_tel_Yindent; %maxY/10; 
1066 
				% draw white triangles at corners and centromere locations.
1067 
				% top left corner.
      8 
1068 
				c_ = [1.0 1.0 1.0]; 
      8 
1069 
				x_ = [leftEnd   leftEnd   leftEnd+dx]; 
      8 
1070 
				y_ = [maxY-dy   maxY      maxY      ]; 
      8 
1071 
				f = fill(x_,y_,c_); 
      8 
1072 
				set(f,'linestyle','none'); 
1073 
				% bottom left corner.     
      8 
1074 
				x_ = [leftEnd   leftEnd   leftEnd+dx]; 
      8 
1075 
				y_ = [dy        0         0         ]; 
  0.03 
      8 
1076 
				f = fill(x_,y_,c_); 
      8 
1077 
				set(f,'linestyle','none'); 
1078 
				% top right corner.
      8 
1079 
				x_ = [rightEnd   rightEnd   rightEnd-dx];    
      8 
1080 
				y_ = [maxY-dy    maxY       maxY      ];   
      8 
1081 
				f = fill(x_,y_,c_); 
      8 
1082 
				set(f,'linestyle','none'); 
1083 
				% bottom right corner.
      8 
1084 
				x_ = [rightEnd   rightEnd   rightEnd-dx]; 
      8 
1085 
				y_ = [dy         0          0         ]; 
  0.01 
      8 
1086 
				f = fill(x_,y_,c_); 
      8 
1087 
				set(f,'linestyle','none'); 
1088 
				% top centromere.
      8 
1089 
				x_ = [x1-dx   x1        x2        x2+dx]; 
      8 
1090 
				y_ = [maxY    maxY-dy   maxY-dy   maxY]; 
      8 
1091 
				f = fill(x_,y_,c_); 
      8 
1092 
				set(f,'linestyle','none'); 
1093 
				% bottom centromere.
      8 
1094 
				x_ = [x1-dx   x1   x2   x2+dx]; 
      8 
1095 
				y_ = [0       dy   dy   0    ]; 
  0.01 
      8 
1096 
				f = fill(x_,y_,c_); 
      8 
1097 
				set(f,'linestyle','none'); 
1098 

1099 
				% draw outlines of chromosome cartoon.   (drawn after horizontal lines to that cartoon edges are not interrupted by horiz lines.
  0.01 
      8 
1100 
				plot([leftEnd   leftEnd   leftEnd+dx   x1-dx   x1        x2        x2+dx   rightEnd-dx   rightEnd   rightEnd   rightEnd-dx   x2+dx   x2   x1   x1-dx   leftEnd+dx   leftEnd],... 
1101 
				     [dy        maxY-dy   maxY         maxY    maxY-dy   maxY-dy   maxY    maxY          maxY-dy    dy         0             0       dy   dy   0       0            dy],...
1102 
				     'Color',[0 0 0]);
      8 
1103 
			end; 
1104 
			% linear : end show centromere.
1105 

1106 
			%% linear : show annotation locations
      8 
1107 
			if (show_annotations) && (length(annotations) > 0) 
1108 
				plot([leftEnd rightEnd], [-maxY/10*1.5 -maxY/10*1.5],'color',[0 0 0]);
1109 
				hold on;
1110 
				annotation_location = (annotation_start+annotation_end)./2;
1111 
				for annoteID = 1:length(annotation_location)
1112 
					if (annotation_chr(annoteID) == chr)
1113 
						annotationloc   = annotation_location(annoteID)/bases_per_bin-0.5*(5000/bases_per_bin);
1114 
						annotationStart = annotation_start(   annoteID)/bases_per_bin-0.5*(5000/bases_per_bin);
1115 
						annotationEnd   = annotation_end(     annoteID)/bases_per_bin-0.5*(5000/bases_per_bin);
1116 
						if (strcmp(annotation_type{annoteID},'dot') == 1)
1117 
							plot(annotationloc,-maxY/10*1.5,'k:o','MarkerEdgeColor',annotation_edgecolor{annoteID}, ...
1118 
							     'MarkerFaceColor',annotation_fillcolor{annoteID}, ...
1119 
							     'MarkerSize',     annotation_size(annoteID));
1120 
						elseif (strcmp(annotation_type{annoteID},'block') == 1)
1121 
							fill([annotationStart annotationStart annotationEnd annotationEnd], ...
1122 
							     [-maxY/10*(1.5+0.75) -maxY/10*(1.5-0.75) -maxY/10*(1.5-0.75) -maxY/10*(1.5+0.75)], ...
1123 
							     annotation_fillcolor{annoteID},'EdgeColor',annotation_edgecolor{annoteID});
1124 
						end;
1125 
					end;
1126 
				end;
1127 
				hold off;
1128 
			end;
1129 
			% linear : end show annotation locations.
1130 

1131 
			%% linear : Final formatting stuff.
      8 
1132 
			xlim([0,chr_size(chr)/bases_per_bin]); 
1133 
			% modify y axis limits to show annotation locations if any are provided.
      8 
1134 
			if (length(annotations) > 0) 
1135 
				ylim([-maxY/10*1.5,maxY]);
      8 
1136 
			else 
  0.01 
      8 
1137 
				ylim([0,maxY]); 
      8 
1138 
			end; 
      8 
1139 
			set(gca,'TickLength',[(Linear_TickSize*chr_size(largestChr)/chr_size(chr)) 0]); %ensures same tick size on all subfigs. 
      8 
1140 
			set(gca,'YTick',[]); 
      8 
1141 
        		set(gca,'YTickLabel',[]); 
      8 
1142 
			set(gca,'XTick',0:(40*(5000/bases_per_bin)):(650*(5000/bases_per_bin))); 
      8 
1143 
			set(gca,'XTickLabel',[]); 
      8 
1144 
			set(gca,'FontSize',linear_gca_font_size); 
1145 
			% end final reformatting.
1146 
			% adding title in the middle of the cartoon
1147 
			% note: adding title is done in the end since if placed upper
1148 
			% in the code somehow the plot function changes the title position			
      8 
1149 
			if (rotate == 0 && chr_size(chr) ~= 0 ) 
  0.20 
      8 
1150 
				title(chr_label{chr},'Interpreter','none','FontSize',linear_chr_font_size,'Rotation',rotate); 
1151 
			else
1152 
				text((chr_size(chr)/bases_per_bin)/2,maxY+0.5,chr_label{chr},'Interpreter','none','FontSize',linear_chr_font_size,'Rotation',rotate);
1153 
			end;
1154 
	        
1155 
			% shift back to main figure generation.
  0.01 
      8 
1156 
			figure(fig); 
  0.01 
      8 
1157 
			hold on; 
      8 
1158 
			first_chr = false; 
      8 
1159 
		end; 
      8 
1160 
	end; 
      9 
1161 
end; 
1162 

1163 
%% Save figures.
      1 
1164 
set(fig,'PaperPosition',[0 0 stacked_fig_width stacked_fig_height]); 
  7.56 
      1 
1165 
saveas(fig,        [projectDir 'fig.SNP-map.1.eps'], 'epsc'); 
  6.55 
      1 
1166 
saveas(fig,        [projectDir 'fig.SNP-map.1.png'], 'png'); 
  0.19 
      1 
1167 
delete(fig); 
1168 

      1 
1169 
set(Linear_fig,'PaperPosition',[0 0 linear_fig_width linear_fig_height]); 
  2.81 
      1 
1170 
saveas(Linear_fig, [projectDir 'fig.SNP-map.2.eps'], 'epsc'); 
  3.78 
      1 
1171 
saveas(Linear_fig, [projectDir 'fig.SNP-map.2.png'], 'png'); 
  0.20 
      1 
1172 
delete(Linear_fig); 
1173 

1174 
%% ========================================================================
1175 
% end stuff
1176 
%==========================================================================
      1 
1177 
end