File: Monarch

package info (click to toggle)
trinityrnaseq 2.11.0%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 417,528 kB
  • sloc: perl: 48,420; cpp: 17,749; java: 12,695; python: 3,124; sh: 1,030; ansic: 983; makefile: 688; xml: 62
file content (382 lines) | stat: -rwxr-xr-x 8,804 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/usr/bin/env perl

use strict;
use warnings;

use lib ("/usr/lib/trinityrnaseq/PerlLib");

use Fasta_reader;

use ColorGradient;

use KmerNode;
use KmerGraph;
use Carp;

use Getopt::Long qw(:config no_ignore_case bundling);

no warnings 'recursion';

my $usage = <<_EOUSAGE_;

#####################################################################################
#
#  Required:
#
#  --reads <string>         fasta files containing reads (comma-separated list of files:  fileA.reads,fileB.reads,...
#                                   basic black coloring in graph.) 
#    and/or
#
#  --misc_seqs <string>           fasta files containing reads (comma-separated list of files:  fileA.reads,fileB.reads,...) 
#                                    each sequence is colored differently and identified in the graph @ edges
#  and:
#       --assemble
#    and/or
#       --graph [filename]    dot output file for viewing structure in GraphViz
#
#  Optional:
#  -K               Kmer length (default: 24)
#  -R               maximum recursive search depth from terminus for extension (default: 5) # note can exponentially increases runtime
#
#
#  -L                    minimum assembled sequence length to report (default: 100)
#  -E                    minimum Kmer seed entropy (default: 1.5;  note maximum entropy = 2 for nucleotide sequences)
#  --min_coverage        minimum kmer coverage to report kmer from reads in graph.
#
#
#  -v                    verbose
#
#  --no_compact          do NOT compact the graph
#
#  --min_edge_threshold    default(0.1)
#  --min_leaf_node_length  default(76)
#  --min_leaf_node_avg_cov default(1.2)
#
#  -N                    max assemblies to report (default: infinity)
#
#####################################################################################

_EOUSAGE_

	;



my $help_flag;

my $fasta_file;
my $KmerLength = 24;

my $max_recurse_depth = 5;

my $min_length = 100;
my $min_cov = 1;

our $VERBOSE =0;

my $MIN_KMER_ENTROPY = 1.5;
my $graph_file;
my $assemble_flag;
my $cds_file;
my $iworm_file;

my $MIN_EDGE_THRESHOLD = 0.1;
my $MIN_LEAF_NODE_LENGTH = 76;
my $MIN_LEAF_NODE_AVG_COV = 1.2;


my $NO_COMPACT = 0;
my $max_asmbls_report = -1;

my $reads_files;
my $misc_seqs_files;

&GetOptions ( 'h' => \$help_flag,
                          
			  ## required
			  'misc_seqs=s' => \$misc_seqs_files,
              'reads=s' => \$reads_files,
              
			  'K=i' => \$KmerLength,
			  'R=i' => \$max_recurse_depth,
			  
			  ## options
			  'L=i' => \$min_length,
			  'E=f' => \$MIN_KMER_ENTROPY,
			  "min_coverage=i" => \$min_cov,
			  "graph=s" => \$graph_file,
			  "assemble" => \$assemble_flag,
			  "iworm=s" => \$iworm_file,

			  "CDS=s" => \$cds_file,

			  "min_edge_threshold=f" => \$MIN_EDGE_THRESHOLD,
			  "min_leaf_node_length=i" => \$MIN_LEAF_NODE_LENGTH,
			  "min_leaf_node_avg_cov=f" =>  \$MIN_LEAF_NODE_AVG_COV,
			  
			  "no_compact" => \$NO_COMPACT,
			  'N=i' => \$max_asmbls_report,

			  # hidden option for debugging
			  'v' => \$VERBOSE,
			  

	);

if ($help_flag) {
	die $usage;
}

unless ( ($reads_files || $misc_seqs_files) && ($graph_file || $assemble_flag)) {
	die $usage;
}


our $SEE = $VERBOSE;


main: {
	
	my $KmerGraph = KmerGraph->new($KmerLength);

    if ($reads_files) {
        my $seq_counter = 0;
        my @files = split(/,/, $reads_files);
        foreach my $file (@files) {
            my $fasta_reader = new Fasta_reader($file);
            
            while (my $seq_obj = $fasta_reader->next()) {
                my $acc = $seq_obj->get_accession();
                my $sequence = $seq_obj->get_sequence();
                
                $seq_counter++;
                
                print STDERR "\r-parsing seq($seq_counter)        ";
                
                $KmerGraph->add_sequence_to_graph($acc, $sequence, 1, 'black');
                
            }
        }
    }
    
	if ($misc_seqs_files) {
			
        my @seqs;
        
        my @files = split(/,/, $misc_seqs_files);
        foreach my $file (@files) {
            my $fr = new Fasta_reader($file);
            
            
            
            while (my $seq_obj = $fr->next()) {
                my $seq = $seq_obj->get_sequence();
                my $acc = $seq_obj->get_accession();
                
                if (length($seq) >= $min_length) {
                    push (@seqs, [$acc, $seq]);
                }
                
            }
        }
		
		my @colors;
		if (scalar @seqs > 1) {
			@colors = &ColorGradient::get_RGB_gradient(scalar @seqs);
		
			@colors = &ColorGradient::convert_RGB_hex(@colors);
		}
		else {
			@colors = ('green');
		}
		
		while (@seqs) {
			my $seq_info = shift @seqs;
			my ($acc, $seq) = @$seq_info;
			my $color = shift @colors;
			print STDERR "Adding additional seq trace: $acc, $color\n";
			$KmerGraph->add_sequence_to_graph($acc, $seq, 0, $color);
			
		}
	}
	

	
	if ($min_cov > 1) {
		print STDERR "\n-removing kmer nodes below coverage: $min_cov\n";
		
		$KmerGraph->purge_nodes_below_count($min_cov);
	}
	


	print STDERR "\nDone adding sequences\n";



	unless ($NO_COMPACT) {
		
		my $round = 0;

		my ($compacted_graph_flag, $number_dangling_nodes_pruned, $number_pruned_edges);
		
		do {


			$compacted_graph_flag = $KmerGraph->compact_graph();
						   
			$number_dangling_nodes_pruned = $KmerGraph->prune_dangling_nodes(min_leaf_node_length => $MIN_LEAF_NODE_LENGTH,
																				min_leaf_node_avg_cov => $MIN_LEAF_NODE_AVG_COV);

			$number_pruned_edges = $KmerGraph->prune_low_weight_edges(edge_weight_threshold => $MIN_EDGE_THRESHOLD);
			
			$round++;
			
			# no op
			print STDERR "Cycling through graph compaction, round: $round\n";
			print STDERR "\tnodes_pruned: $number_dangling_nodes_pruned\n"
				. "\tedges_pruned: $number_pruned_edges\n";
			
			
		} while ($compacted_graph_flag && ($number_dangling_nodes_pruned || $number_pruned_edges));
		
		
		
	}
	
	print STDERR "done.\n";
	
	
	if ($assemble_flag) {

		my @path = $KmerGraph->score_nodes();
		
		my $assembled_seq = $KmerGraph->extract_sequence_from_path(@path);
		
		unless ($assembled_seq) {
			confess "Error, no assembled sequence form path: @path";
		}
		

		my $score = &sum_node_counts(@path);
		
		my $counter = 0;
		if (length($assembled_seq) >= $min_length) {
			$counter++;
			
			my $length_normalized_score = sprintf("%.2f", $score / length($assembled_seq));
			
			print ">assembly_$counter;$score;$length_normalized_score len: " . length($assembled_seq) . "\n$assembled_seq\n";
		}
		
		
		while (! $KmerGraph->all_nodes_visited()) {
			
			if ($max_asmbls_report > 0 && $counter >= $max_asmbls_report) { 
				last;
			}
			
			my @path = $KmerGraph->extract_path_from_unvisited_node(@path);
			
			#$KmerGraph->print_path(@path);
			
			#foreach my $node (@path) {
			#	print $node->toString();
			#}
			
			my $assembled_seq = $KmerGraph->extract_sequence_from_path(@path);  # marks as visited.

			unless ($assembled_seq) {
			    confess "Error, no sequence extracted from path: @path";
			}
			
			if ( length($assembled_seq) >= $min_length) {
				$counter++;
				
				my $score = &sum_node_counts(@path);
				my $length_normalized_score = sprintf("%.2f", $score / length($assembled_seq));
				
				print ">assembly_$counter;$score;$length_normalized_score len: " . length($assembled_seq) . "\n$assembled_seq\n";
			}
		}
	}
	
	## Write graph file in dot format for GraphViz
	if ($graph_file) {
		open (my $ofh, ">$graph_file") or die "Error, cannot write graph file to $graph_file ";
		print $ofh $KmerGraph->toGraphViz( no_short_singletons => 100);
		close $ofh;
	}

		
	
	exit(0);
}


####
sub sum_node_counts {
	my @path = @_;
	
	my $sum = 0;

	foreach my $node (@path) {
		my $count = $node->get_count();
		$sum += $count;
	}

	return($sum);
}

	
####
sub compute_entropy {
	my ($string) = @_;
        
	my @chars = split(//, $string);
        
	my %char_counter;
	foreach my $char (@chars) {
		$char_counter{$char}++;
	}
        
	my $entropy = 0;
        
	my $num_chars = length($string);
	foreach my $char (keys %char_counter) {
		my $count = $char_counter{$char};
		my $prob = $count/$num_chars;
                
		my $val = $prob * log(1/$prob)/log(2);
		$entropy += $val;
	}
        
	return($entropy);
}


####
sub get_highest_scoring_iworm_assembly {
	my ($iworm_file) = @_;
	
	my @entries;

	my $fasta_reader = new Fasta_reader($iworm_file);
	while (my $seq_obj = $fasta_reader->next()) {
		my $seq = $seq_obj->get_sequence();
		my $acc = $seq_obj->get_accession();
		
		my @parts = split(/;/, $acc);
		my $cov = pop @parts;

		my $score = $cov * length($seq);
		
		push (@entries, [$score, $seq]);
	}
	
	@entries = reverse sort {$a->[0]<=>$b->[0]} @entries;
	
	my $best_entry = shift @entries;

	return($best_entry->[1]);
}