forked from chaolinzhanglab/czplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlign.pm
1287 lines (1023 loc) · 27.8 KB
/
Align.pm
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
#===============================================================================
#
# FILE: Bed.pm
#
# DESCRIPTION: Package to handle sequence alignment
# BUGS: ---
# NOTES: The package is spun off from the AnnotationIO.pm and Common.pm
# AUTHOR: Chaolin Zhang (cz), [email protected]
# COMPANY: Rockefeller University
# VERSION: 1.0
# CREATED: 12/17/2010
# REVISION: ---
#===============================================================================
package Align;
require Exporter;
@ISA = qw (Exporter);
@EXPORT = qw (
blat
skipPslHeader
calcPslPercentIdentity
lineToPsl
readPslFile
printPsl
pslToBed
pslToLine
writePslFile
readNextPslLine
checkSim4AlignQuality
printSim4Align
readSim4File
sim4ToBed
lineToVulgar
readVulgarFile
vulgarToBed
vulgarToLine
writeVulgarFile
lineToSam
samToBed
);
=head1 NAME
Align - subroutines that deal with sequence alignment
subroutines starting with a hyphen should not be called outside
=cut
use strict;
use warnings;
use Data::Dumper;
use Carp;
use Common;
use Sam;
=head2 blat
my $psl = blat ($blat, $db, $query, $cache, $options);
$blat : path to the blat binary
$db : target database for blat
$query : query sequence for blat
$cache : cache dir; should be created before this subroutine is called
$options: a hash ref. with options to be passed on to blat
=cut
sub blat
{
Carp::croak "3-5 arguments expected\n" unless (@_ >= 4 && @_ <= 5);
my ($blat, $db, $query, $cache, $options) = @_;
#my $cache = "/tmp";
my $optPair = {
t=>1,
q=>1,
occ=>1,
tileSize=>1,
stepSize=>1,
oneOff=>1,
minMatch=>1,
minScore=>1,
minIdentity=>1,
maxGap=>1,
makeOcc=>1,
repMatch=>1,
mask=>1,
qMask=>1,
repeats=>1,
minRepDivergence=>1,
dots=>1,
out=>1,
maxIntron=>1
};
my $optLeft = {
prot=>1,
noHead=>1,
trimT=>1,
noTrimA=>1,
trimHardA=>1,
fastMap=>1,
fine=>1,
extendThroughN=>1
};
my $optStr = "";
foreach my $opt (keys %$options)
{
if (exists $optPair->{$opt})
{
$optStr .= " -$opt=" . $options->{$opt};
}
elsif (exists $optLeft->{$opt})
{
$optStr .= " -$opt";
}
else
{
Carp::croak "bad options for blat: $opt\n";
}
}
my $out = "$cache/blatout.".time ().rand();
Carp::croak "the output $out already exists\n" if -f $out;
my $cmd = "$blat $db $query $optStr $out >& /dev/null";
#print $cmd, "\n";
my $ret = system ($cmd);
Carp::croak "Command has been crashed ($cmd): $?\n" unless $ret == 0;
my $result = readPslFile ($out);
unlink $out if -f $out;
return $result;
}
=head2 skipPslHeader
my $fin = skipPslHeader ($fin);
=cut
sub skipPslHeader
{
my $fin = $_[0];
my $line = <$fin>;
if ($line=~/^psLayout/) # there is a header
{
while ($line = <$fin>)
{
chomp $line;
last if ($line =~/^\-\-\-/);
}
}
elsif ($line=~/^track/)
{
}
elsif ($line=~/^browser/)
{
}
else
{
seek ($fin, 0, 0); # no header line, go to the very beginnning
}
return $fin;
}
=head2 readNextPslLine
my $psl = readNextPslLine ($fin);
=cut
sub readNextPslLine
{
my $fin = $_[0];
while (my $line = <$fin>)
{
chomp $line;
next if $line =~/^\s*$/;
return lineToPsl ($line);
}
return "";
}
=head2 lineToPsl
parse a Psl line
my $psl = lineToPsl ($line);
=cut
sub lineToPsl
{
my $line = $_[0];
my @cols = split (/\s+/, $line);
shift @cols if ($#cols == 21); #the first column is the bin id
#print join ("|\n", @cols), "|\n";
Carp::croak "the number of columns is incorrect:", Dumper (\@cols), "\n" if $#cols != 20;
my @blockSizes = split (/\,/, $cols[18]); #pop @blockSizes;
my @qStarts = split (/\,/, $cols[19]); #pop @qStarts;
#print "qStarts=", join ("|\t|", @qStarts), "|\n";
my @tStarts = split (/\,/, $cols[20]); #pop @tStarts;
my $ret = {
matches=>$cols[0],
misMatches=>$cols[1],
repMatches=>$cols[2],
nCount=>$cols[3],
qNumInsert=>$cols[4],
qBaseInsert=>$cols[5],
tNumInsert=>$cols[6],
tBaseInsert=>$cols[7],
strand=>$cols[8],
qName=>$cols[9],
qSize=>$cols[10],
qStart=>$cols[11], #the convention is the same as BED file, 0 to len
qEnd=>$cols[12] - 1,
tName=>$cols[13],
tSize=>$cols[14],
tStart=>$cols[15],
tEnd=>$cols[16] - 1,
blockCount=>$cols[17],
blockSizes=>\@blockSizes,
qStarts=> \@qStarts,
tStarts=>\@tStarts};
return $ret;
}
=head2 readPslFile
Read UCSC PSL file
my $ret = readPslFile ($pslFile)
header line will be ignored
$ret = {
matches=>$cols[0],
misMatches=>$cols[1],
repMatches=>$cols[2],
nCount=>$cols[3],
qNumInsert=>$cols[4],
qBaseInsert=>$cols[5],
tNumInsert=>$cols[6],
tBaseInsert=>$cols[7],
strand=>$cols[8],
qName=>$cols[9],
qSize=>$cols[10],
qStart=>$cols[11], #the convention is the same as BED file, 0 to len
qEnd=>$cols[12] - 1,
tName=>$cols[13],
tSize=>$cols[14],
tStart=>$cols[15],
tEnd=>$cols[16] - 1,
blockCount=>$cols[17],
blockSizes=>\@blockSizes,
qStarts=> \@qStarts,
tStarts=>\@tStarts};
}
=cut
sub readPslFile
{
my ($in, $verbose) = @_;
my $fin;
my @results;
open ($fin, "<$in") || Carp::croak "can not open file $in to read\n";
$fin = skipPslHeader ($fin);
my $i = 0;
while (my $psl = readNextPslLine ($fin))
{
print "$i ...\n" if $verbose && $i % 100000 == 0;
$i++;
push @results, $psl;
}
close ($fin);
return \@results;
}
=head2 obsolete
sub readPslFile
{
my ($in, $verbose) = @_;
my $fin;
my @results;
open ($fin, "<$in") || Carp::croak "can not open file $in to read\n";
$fin = skipPslHeader ($fin);
my $i = 0;
while ($line =<$fin>)
{
chomp $line;
next if $line =~/^\s*$/;
my @cols = split (/\t/, $line);
shift @cols if ($#cols == 21); #the first column is the bin id
#print join ("|\n", @cols), "|\n";
Carp::croak ("the number of columns is incorrect\n") if $#cols != 20;
$i++;
print "$i ...\n" if $verbose && $i % 100000 == 0;
my @blockSizes = split (/\,/, $cols[18]); #pop @blockSizes;
my @qStarts = split (/\,/, $cols[19]); #pop @qStarts;
#print "qStarts=", join ("|\t|", @qStarts), "|\n";
my @tStarts = split (/\,/, $cols[20]); #pop @tStarts;
push @results, {
matches=>$cols[0],
misMatches=>$cols[1],
repMatches=>$cols[2],
nCount=>$cols[3],
qNumInsert=>$cols[4],
qBaseInsert=>$cols[5],
tNumInsert=>$cols[6],
tBaseInsert=>$cols[7],
strand=>$cols[8],
qName=>$cols[9],
qSize=>$cols[10],
qStart=>$cols[11], #the convention is the same as BED file, 0 to len
qEnd=>$cols[12] - 1,
tName=>$cols[13],
tSize=>$cols[14],
tStart=>$cols[15],
tEnd=>$cols[16] - 1,
blockCount=>$cols[17],
blockSizes=>\@blockSizes,
qStarts=> \@qStarts,
tStarts=>\@tStarts};
}
close ($fin);
return \@results;
}
=cut
=head2 pslToBed
my $bed = pslToBed ($psl, $chrom=0, $useQuery = 0)
convert target coordinates to the BED format by assuming the
query sequence is the genomic sequence. vice versa if $useQuery == 1
if $chrom == 0, its value is determined according to $useQuery
otherwise, use the specified $chrom value
=cut
#note Psl used absolute coordinates
sub pslToBed
{
my ($align, $chrom, $useQuery) = @_;
my ($chromStart, $chromEnd, $name, @blockStarts);
if ($useQuery)
{
$chrom = $align->{'qName'} unless $chrom;
$chromStart = $align->{"qStart"};
$chromEnd = $align->{"qEnd"};
$name = $align->{"tName"};
@blockStarts = ();
foreach my $s (@{$align->{"qStarts"}})
{
push @blockStarts, $s - $chromStart;
}
}
else
{
#default
$chrom = $align->{'tName'} unless $chrom;
$chromStart = $align->{"tStart"};
$chromEnd = $align->{"tEnd"};
$name = $align->{"qName"};
foreach my $s (@{$align->{"tStarts"}})
{
push @blockStarts, $s - $chromStart;
}
}
my $score = calcPslPercentIdentity ($align, 1);
#my $strand = '+';#always set to '+', the gene strand, although a few transcripts were aligned to the reverse strand
my $strand = $align->{'strand'};
$strand=~/(\S)$/; #for translated alignment, we get the genomic strand
$strand = $1;
my $blockCount = $align->{"blockCount"};
my @blockSizes = @{$align->{"blockSizes"}};
my $cov = $align->{'matches'} / $align->{'qSize'};
my $region = {chrom=>$chrom, #gene id
chromStart=>$chromStart, #start on gene contig
chromEnd=>$chromEnd, #end on gene contig
name=>$name,
score=>$score,
strand=> $strand,
thickStart=>$chromStart,
thickEnd=>$chromEnd,
itemRgb=>"0,0,0",
blockCount=>$blockCount,
blockSizes=>\@blockSizes,
blockStarts=>\@blockStarts,
cov=>$cov
};
return $region;
}
=head2 printPsl
Note: original name printPslAlign
printPsl ($psl);
=cut
sub printPslAlign
{
Carp::croak "obsolete function, call printPsl instead\n";
}
sub printPsl
{
my $psl = $_[0];
print pslToLine ($psl), "\n";
}
=head2 pslToLine
my $str = pslToLine ($psl);
=cut
sub pslToLine
{
my ($psl) = @_;
return join ("\t",
$psl->{"matches"},
$psl->{"misMatches"},
$psl->{"repMatches"},
$psl->{"nCount"},
$psl->{"qNumInsert"},
$psl->{"qBaseInsert"},
$psl->{"tNumInsert"},
$psl->{"tBaseInsert"},
$psl->{"strand"},
$psl->{"qName"},
$psl->{"qSize"},
$psl->{"qStart"},
$psl->{"qEnd"} + 1,
$psl->{"tName"}, #tName
$psl->{"tSize"},
$psl->{"tStart"},#tStart
$psl->{"tEnd"} + 1, #tEnd
$psl->{"blockCount"},
join (",", @{$psl->{"blockSizes"}}) . ",", #blockSizes
join (",", @{$psl->{"qStarts"}}) . ",", #qStarts
join (",", @{$psl->{"tStarts"}}) . ","); #tStarts
}
=head2 writePslFile
writePslFile ($psls, $outFile, $printHeader = 0);
=cut
sub writePslFile
{
my ($aligns, $out, $printHeader) = @_;
my $header = "psLayout version 3\n";
$header .= "match mis- rep. N's Q gap Q gap T gap T gap strand Q Q Q Q T T T T block blockSizes qStarts tStarts\n";
$header .= " match match count bases count bases name size start end name size start end count\n";
$header .= "---------------------------------------------------------------------------------------------------------------------------------------------------------------\n";
my $fout;
open ($fout, ">$out") || Carp::croak "can not open file $out to write\n";
print $fout $header if $printHeader;
my $stdout = select ($fout);
foreach my $align (@$aligns)
{
print pslToLine ($align), "\n";
}
close ($fout);
select ($stdout);
}
=head2 calcPslPercentIdentity
calculate percent identity from psl line
according to the pslCalcMilliBad function at http://genome.ucsc.edu/FAQ/FAQblat.html
my $identity = calcPslPercentIdentity ($psl, $isMrna);
if $isMrna == 0, $psl is treated as amino acids
=cut
sub calcPslPercentIdentity
{
my ($psl, $isMrna) = @_;
my $sizeMul = 1; #3 for protein
my $milliBad = 0;
my $qAliSize = $sizeMul * ($psl->{"qEnd"} - $psl->{"qStart"} + 1);
my $tAliSize = $psl->{"tEnd"} - $psl->{"tStart"} + 1;
my $aliSize = Common::min($qAliSize, $tAliSize);
return 0 if $aliSize <= 0;
my $sizeDif = $qAliSize - $tAliSize;
if ($sizeDif < 0)
{
$sizeDif = 0 - $sizeDif;
$sizeDif = 0 if $isMrna;
}
my $insertFactor = $psl->{"qNumInsert"};
$insertFactor += $psl->{"tNumInsert"} if ($isMrna == 0);
my $total = $sizeMul * ($psl->{"matches"} + $psl->{"repMatches"} + $psl->{"misMatches"});
if ($total != 0)
{
$milliBad = $psl->{"misMatches"} * $sizeMul + $insertFactor + int(3*log(1+$sizeDif)+0.5);
$milliBad /= $total;
}
return (1- $milliBad);
}
#assume seq1 is genomic sequence
#seq2 is transcript (from a db)
sub _splitSim4AlignText
{
my ($seq1Text, $matchText, $seq2Text) = @_;
my @seq1blocksText;
my @matchBlocksText;
my @seq2blocksText;
my $start = 0;
while ($matchText =~/\>\>\>\.\.\.\>\>\>/g)
{
push @seq1blocksText, substr ($seq1Text, $start, pos($matchText) - 9 - $start);
push @matchBlocksText, substr ($matchText, $start, pos($matchText) - 9 - $start);
push @seq2blocksText, substr ($seq2Text, $start, pos($matchText) - 9 - $start);
$start = pos($matchText);
}
push @seq1blocksText, substr ($seq1Text, $start);
push @matchBlocksText, substr ($matchText, $start);
push @seq2blocksText, substr ($seq2Text, $start);
return {seq1text=>\@seq1blocksText, matchtext=>\@matchBlocksText, seq2text=>\@seq2blocksText};
}
=head2 readSim4File
note: original name: readsim4File
=cut
sub readSim4File
{
my $in = $_[0];
my @ret;
my ($seq1, $seq2, $seq2id, $strand) = ("", "", "", '+');
my ($seq1len, $seq2len) = (0, 0);
my $seq1blocks = [];
my $seq2blocks = [];
my $blockscore = [];
my $alignText = 0;
my $alignSeq1 = "";
my $alignMatch = "";
my $alignSeq2 = "";
my $match = 0; #total length of matches
my $identity = 0; #number of correct bases
my $fin;
open ($fin, "<$in") || Carp::croak "can not open file $in to read\n";
while (my $line=<$fin>)
{
chomp $line;
next if $line=~/^\s*$/;
if ($line=~/^seq1/)
{
if (@$seq1blocks > 0)
{
$identity = int ($identity/100 + 0.5);
Carp::croak "inconsistency found near line $line\n"
unless length ($alignSeq1) == length ($alignMatch) && length ($alignSeq2) == length ($alignMatch);
my $item = {seqfile1=>$seq1, seqfile2=>$seq2, seq2id=>$seq2id,
seq1len=>$seq1len, seq2len=>$seq2len,
match=> $match, identity=>$identity, strand=>$strand,
seq1blocks=>$seq1blocks,
seq2blocks=>$seq2blocks,
blockscore=>$blockscore};
if (length ($alignSeq1) > 0)
{
my $align = _splitSim4AlignText ($alignSeq1, $alignMatch, $alignSeq2);
$item->{"seq1text"} = $align->{"seq1text"};
$item->{"matchtext"} = $align->{"matchtext"};
$item->{"seq2text"} = $align->{"seq2text"};
my $nblock = @{$align->{"seq1text"}};
if ($nblock != @{$item->{"seq1blocks"}})
{
print "inconsistent block number for gene = $seq1, acc = $seq2id\n";
print Dumper ($item), "\n";
$seq2 = "";
$seq2id = "";
$seq2len = 0;
$seq1blocks = [];
$seq2blocks = [];
$blockscore = [];
$match = 0;
$identity = 0;
$strand = '+';
$alignText = 0;
$alignSeq1 = "";
$alignSeq2 = "";
$alignMatch = "";
next;
}
for (my $i = 0; $i < $nblock; $i++)
{
my $b1Len = $seq1blocks->[$i]->{"end"} - $seq1blocks->[$i]->{"start"} + 1;
my $b2Len = $seq2blocks->[$i]->{"end"} - $seq2blocks->[$i]->{"start"} + 1;
my $b1Text = $align->{"seq1text"}->[$i];
my $b2Text = $align->{"seq2text"}->[$i];
$b1Text=~s/\s//g;
$b2Text=~s/\s//g;
if ($b1Len != length ($b1Text) || $b2Len != length ($b2Text))
{
print "inconsistency size of block $i: seq1 = $b1Len, seq1text = ", length ($b1Text), ", seq2=$b2Len, seq2text=", length($b2Text), "\n";
print Dumper ($item), "\n";
$seq2 = "";
$seq2id = "";
$seq2len = 0;
$seq1blocks = [];
$seq2blocks = [];
$blockscore = [];
$match = 0;
$identity = 0;
$strand = '+';
$alignText = 0;
$alignSeq1 = "";
$alignSeq2 = "";
$alignMatch = "";
next;
}
}
}
push @ret, $item;
}
$line=~/\=\s+(\S+)\,\s(\d+)\sbp$/;
$seq1 = $1; $seq1len = $2;
#initialize
$seq2 = "";
$seq2id = "";
$seq2len = 0;
$seq1blocks = [];
$seq2blocks = [];
$blockscore = [];
$match = 0;
$identity = 0;
$strand = '+';
$alignText = 0;
$alignSeq1 = "";
$alignSeq2 = "";
$alignMatch = "";
}
elsif ($line=~/^seq2/)
{
$line=~/\=\s+(\S+)\s\((.*?)\)\,\s(\d+)\sbp$/;
$seq2 = $1; $seq2id = $2; $seq2len = $3;
}
elsif ($line=~/^\(complement\)/)
{
$strand = '-';
}
elsif ($line=~/^(\d+)\-(\d+)\s+\((\d+)\-(\d+)\)\s+(\d+)\%/)
{
# print join ("\t", $1, $2, $3, $4, $5), "\n";
my %s1b = (start=>$1-1, end=>$2-1); #convert to 0-based coordinates
my %s2b = (start=>$3-1, end=>$4-1);
push @$seq1blocks, \%s1b;
push @$seq2blocks, \%s2b;
push @$blockscore, $5;
#my $c = @$seq1blocks;
#print "block num = $c\n";
$match += ($2 - $1 + 1);
$identity += ($2 - $1 + 1) * $5;
}
elsif ($line =~/^\s*\d+\s/)
{
$alignText = 1;
#do not want to chop space in the end
$line = <$fin>;
chop $line;
$line =~/^\s*\d+\s(.*?)$/;
$alignSeq1 .= $1;
my $textLen = length ($1);
$line = <$fin>;
chop $line;
$line =~/^\s*(\S.*?)$/;
my $spaceLen = $textLen - length ($1);
$alignMatch .= "" . (" "x $spaceLen) . $1;
$line = <$fin>;
chop $line;
$line =~/^\s*\d+\s(.*?)$/;
$alignSeq2 .= $1;
}
else
{
Carp::croak "$in has been disrupted\n$line\n";
}
}
if (@$seq2blocks > 0)
{
$identity = int ($identity/100 + 0.5);
Carp::croak "inconsistency found near file end\n"
unless length ($alignSeq1) == length ($alignMatch) && length ($alignSeq2) == length ($alignMatch);
my $item = {seqfile1=>$seq1, seqfile2=>$seq2, seq2id=>$seq2id,
seq1len=>$seq1len, seq2len=>$seq2len,
match=> $match, identity=>$identity, strand=>$strand,
seq1blocks=>$seq1blocks,
seq2blocks=>$seq2blocks,
blockscore=>$blockscore};
if (length ($alignSeq1) > 0)
{
my $align = _splitSim4AlignText ($alignSeq1, $alignMatch, $alignSeq2);
my $nblock = @{$align->{"seq1text"}};
$item->{"seq1text"} = $align->{"seq1text"};
$item->{"matchtext"} = $align->{"matchtext"};
$item->{"seq2text"} = $align->{"seq2text"};
if ($nblock != @{$item->{"seq1blocks"}})
{
print "inconsistent block number for gene = $seq1, acc = $seq2id\n";
print Dumper ($item), "\n";
$seq2 = "";
$seq2id = "";
$seq2len = 0;
$seq1blocks = [];
$seq2blocks = [];
$blockscore = [];
$match = 0;
$identity = 0;
$strand = '+';
$alignText = 0;
$alignSeq1 = "";
$alignSeq2 = "";
$alignMatch = "";
next;
}
for (my $i = 0; $i < $nblock; $i++)
{
my $b1Len = $seq1blocks->[$i]->{"end"} - $seq1blocks->[$i]->{"start"} + 1;
my $b2Len = $seq2blocks->[$i]->{"end"} - $seq2blocks->[$i]->{"start"} + 1;
my $b1Text = $align->{"seq1text"}->[$i];
my $b2Text = $align->{"seq2text"}->[$i];
$b1Text=~s/\s//g;
$b2Text=~s/\s//g;
if ($b1Len != length ($b1Text) || $b2Len != length ($b2Text))
{
print "inconsistency size of block $i: seq1 = $b1Len, seq1text = ", length ($b1Text), ", seq2=$b2Len, seq2text=", length($b2Text), "\n";
print Dumper ($item), "\n";
$seq2 = "";
$seq2id = "";
$seq2len = 0;
$seq1blocks = [];
$seq2blocks = [];
$blockscore = [];
$match = 0;
$identity = 0;
$strand = '+';
$alignText = 0;
$alignSeq1 = "";
$alignSeq2 = "";
$alignMatch = "";
next;
}
}
}
push @ret, $item;
}
close ($fin);
return \@ret;
}
sub printSim4Align
{
my $align = $_[0];
print "\n\n";
print "seq1 = ", $align->{"seqfile1"}, ", ", $align->{"seq1len"}, " bp\n";
print "seq2 = ", $align->{"seqfile2"}, " (", $align->{"seq2id"}, "), ", $align->{"seq2len"}, " bp\n";
print "\n";
my $seq1blocks = $align->{"seq1blocks"};
my $seq2blocks = $align->{"seq2blocks"};
my $nblocks = @$seq1blocks;
for (my $i = 0; $i < $nblocks; $i++)
{
print "", ($seq1blocks->[$i]->{"start"} + 1), "-", ($seq1blocks->[$i]->{"end"} + 1), "\t";
print "(", ($seq2blocks->[$i]->{"start"} + 1), "-", ($seq2blocks->[$i]->{"end"} + 1), ")\t";
print $align->{"blockscore"}->[$i], "%", "\n";
}
}
sub sim4ToBed
{
my ($align, $gene) = @_;
my @blockSizes;
my @blockStarts;
my $score = $align->{"identity"} / $align->{"match"};
$score = sprintf ("%.2f", $score);
my $geneBlocks = $align->{"seq1blocks"};
my $nblocks = @$geneBlocks;
my $chromStart = $geneBlocks->[0]->{"start"};
my $chromEnd = $geneBlocks->[$nblocks - 1]->{"end"};
for (my $i = 0; $i < $nblocks; $i++)
{
$blockSizes[$i] = $geneBlocks->[$i]->{"end"} - $geneBlocks->[$i]->{"start"} + 1;
$blockStarts[$i] = $geneBlocks->[$i]->{"start"} - $chromStart;
}
my $region = {chrom=>$gene, #gene id
chromStart=>$chromStart, #start on gene contig
chromEnd=>$chromEnd, #end on gene contig
name=>$align->{"seq2id"},
score=>$score,
strand=> '+', #always set to '+', the gene strand, although a few transcripts were aligned to the reverse strand
thickStart=>$chromStart,
thickEnd=>$chromEnd,
itemRgb=>"0,0,0",
blockCount=>$nblocks,
blockSizes=>\@blockSizes,
blockStarts=>\@blockStarts};
return $region;
#removeSmallGap ($region, 0);
}
#need to add code to handle alignment text
sub checkSim4AlignQuality
{
my ($aligns, $gene, $coverage, $identity, $verbose) = @_;
my @goodAligns;
foreach my $a (@$aligns)
{
#print "align = ", Dumper ($a), "\n";
my @seq1blocks = @{$a->{"seq1blocks"}};#genomic
my @seq2blocks = @{$a->{"seq2blocks"}};#transcript
my @blockscore = @{$a->{"blockscore"}};
my $nblocks = @{$a->{"seq2blocks"}};
if ($nblocks < 2)
{
print "gene=$gene vs transcript=", $a->{"seq2id"}, ": no splice junction spaned, failed\n" if $verbose;
next;
}
#OK now we have at least two blocks, so there should be no error in the following
#code to check the first and last blocks
#remove the first block if less than 25 nt
my $firstBlockSize = $seq1blocks[0]->{"end"} - $seq1blocks[0]->{"start"} + 1;
#############################################################
#TO BE FIXIED
#use the while loop in the next version
#
#############################################################
#
while ($nblocks >= 2 && $firstBlockSize < 25)
#if ($firstBlockSize < 25)
{
$a->{"match"} -= $firstBlockSize;
$a->{"identity"} -= $firstBlockSize * $blockscore[0] / 100;
shift @seq1blocks;
shift @seq2blocks;
shift @blockscore;
$nblocks -= 1;
#$firstBlockSize = $seq1blocks[0]->{"end"} - $seq1blocks[0]->{"start"} + 1;
}
#remove the last block if less than 25 nt
my $lastBlockSize = $seq1blocks[$nblocks-1]->{"end"} - $seq1blocks[$nblocks-1]->{"start"} + 1;
#############################################################
#TO BE FIXIED
#use the while loop in the next version
#
#############################################################
#
while ($nblocks >= 2 && $lastBlockSize < 25)
#if ($lastBlockSize < 25)
{
$a->{"match"} -= $lastBlockSize;
$a->{"identity"} -= $lastBlockSize * $blockscore[$nblocks - 1] / 100;
pop @seq1blocks;
pop @seq2blocks;
pop @blockscore;
$nblocks -= 1;
#$lastBlockSize = $seq1blocks[$nblocks-1]->{"end"} - $seq1blocks[$nblocks-1]->{"start"} + 1;
}