-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsa.txt
1568 lines (1109 loc) · 36.1 KB
/
sa.txt
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
############################################################
#
# BeginPerlBioinfo.pm
# - a library of subroutines
# from the examples and text in the book:
#
# Beginning Perl for Bioinformatics
# by James Tisdall
#
# published by O'Reilly & Associates
# (c) 2001 James Tisdall
#
# These subroutines are arranged alphabetically
#
# To use this module, add the line (without the comment):
# use BeginPerlBioinfo;
# to your code, making sure the module is in the same directory
# as the program you are using it from, or another place where
# Perl can find it (see the discussion in the book for other locations).
#
# Version 20011230
# incorporates a few errata and bug fixes
#
############################################################
# From example6-1.pl
sub addACGT {
my($dna) = @_;
$dna .= 'ACGT';
return $dna;
}
# From example6-2.pl
sub A_to_T {
my($input) = @_;
$dna = $input;
$dna =~ s/A/T/g;
return $dna;
}
# From example11-7.pl
# call_stride
#
# -given a PDB filename, return the output from the "stride"
# secondary structure prediction program
sub call_stride {
use strict;
use warnings;
my($filename) = @_;
# The stride program options
my($stride) = '/usr/local/bin/stride';
my($options) = '';
my(@results) = ( );
# Check for presence of PDB file
unless ( -e $filename ) {
print "File \"$filename\" doesn\'t seem to exist!\n";
exit;
}
# Start up the program, capture and return the output
@results = `$stride $options $filename`;
return @results;
}
# From Chapter 8
# codon2aa
#
# A subroutine to translate a DNA 3-character codon to an amino acid
# Version 1
# This subroutine is commented out because a preferred version of it
# follows.
# sub codon2aa {
# my($codon) = @_;
#
# if ( $codon =~ /TCA/i ) { return 'S' } # Serine
# elsif ( $codon =~ /TCC/i ) { return 'S' } # Serine
# elsif ( $codon =~ /TCG/i ) { return 'S' } # Serine
# elsif ( $codon =~ /TCT/i ) { return 'S' } # Serine
# elsif ( $codon =~ /TTC/i ) { return 'F' } # Phenylalanine
# elsif ( $codon =~ /TTT/i ) { return 'F' } # Phenylalanine
# elsif ( $codon =~ /TTA/i ) { return 'L' } # Leucine
# elsif ( $codon =~ /TTG/i ) { return 'L' } # Leucine
# elsif ( $codon =~ /TAC/i ) { return 'Y' } # Tyrosine
# elsif ( $codon =~ /TAT/i ) { return 'Y' } # Tyrosine
# elsif ( $codon =~ /TAA/i ) { return '_' } # Stop
# elsif ( $codon =~ /TAG/i ) { return '_' } # Stop
# elsif ( $codon =~ /TGC/i ) { return 'C' } # Cysteine
# elsif ( $codon =~ /TGT/i ) { return 'C' } # Cysteine
# elsif ( $codon =~ /TGA/i ) { return '_' } # Stop
# elsif ( $codon =~ /TGG/i ) { return 'W' } # Tryptophan
# elsif ( $codon =~ /CTA/i ) { return 'L' } # Leucine
# elsif ( $codon =~ /CTC/i ) { return 'L' } # Leucine
# elsif ( $codon =~ /CTG/i ) { return 'L' } # Leucine
# elsif ( $codon =~ /CTT/i ) { return 'L' } # Leucine
# elsif ( $codon =~ /CCA/i ) { return 'P' } # Proline
# elsif ( $codon =~ /CCC/i ) { return 'P' } # Proline
# elsif ( $codon =~ /CCG/i ) { return 'P' } # Proline
# elsif ( $codon =~ /CCT/i ) { return 'P' } # Proline
# elsif ( $codon =~ /CAC/i ) { return 'H' } # Histidine
# elsif ( $codon =~ /CAT/i ) { return 'H' } # Histidine
# elsif ( $codon =~ /CAA/i ) { return 'Q' } # Glutamine
# elsif ( $codon =~ /CAG/i ) { return 'Q' } # Glutamine
# elsif ( $codon =~ /CGA/i ) { return 'R' } # Arginine
# elsif ( $codon =~ /CGC/i ) { return 'R' } # Arginine
# elsif ( $codon =~ /CGG/i ) { return 'R' } # Arginine
# elsif ( $codon =~ /CGT/i ) { return 'R' } # Arginine
# elsif ( $codon =~ /ATA/i ) { return 'I' } # Isoleucine
# elsif ( $codon =~ /ATC/i ) { return 'I' } # Isoleucine
# elsif ( $codon =~ /ATT/i ) { return 'I' } # Isoleucine
# elsif ( $codon =~ /ATG/i ) { return 'M' } # Methionine
# elsif ( $codon =~ /ACA/i ) { return 'T' } # Threonine
# elsif ( $codon =~ /ACC/i ) { return 'T' } # Threonine
# elsif ( $codon =~ /ACG/i ) { return 'T' } # Threonine
# elsif ( $codon =~ /ACT/i ) { return 'T' } # Threonine
# elsif ( $codon =~ /AAC/i ) { return 'N' } # Asparagine
# elsif ( $codon =~ /AAT/i ) { return 'N' } # Asparagine
# elsif ( $codon =~ /AAA/i ) { return 'K' } # Lysine
# elsif ( $codon =~ /AAG/i ) { return 'K' } # Lysine
# elsif ( $codon =~ /AGC/i ) { return 'S' } # Serine
# elsif ( $codon =~ /AGT/i ) { return 'S' } # Serine
# elsif ( $codon =~ /AGA/i ) { return 'R' } # Arginine
# elsif ( $codon =~ /AGG/i ) { return 'R' } # Arginine
# elsif ( $codon =~ /GTA/i ) { return 'V' } # Valine
# elsif ( $codon =~ /GTC/i ) { return 'V' } # Valine
# elsif ( $codon =~ /GTG/i ) { return 'V' } # Valine
# elsif ( $codon =~ /GTT/i ) { return 'V' } # Valine
# elsif ( $codon =~ /GCA/i ) { return 'A' } # Alanine
# elsif ( $codon =~ /GCC/i ) { return 'A' } # Alanine
# elsif ( $codon =~ /GCG/i ) { return 'A' } # Alanine
# elsif ( $codon =~ /GCT/i ) { return 'A' } # Alanine
# elsif ( $codon =~ /GAC/i ) { return 'D' } # Aspartic Acid
# elsif ( $codon =~ /GAT/i ) { return 'D' } # Aspartic Acid
# elsif ( $codon =~ /GAA/i ) { return 'E' } # Glutamic Acid
# elsif ( $codon =~ /GAG/i ) { return 'E' } # Glutamic Acid
# elsif ( $codon =~ /GGA/i ) { return 'G' } # Glycine
# elsif ( $codon =~ /GGC/i ) { return 'G' } # Glycine
# elsif ( $codon =~ /GGG/i ) { return 'G' } # Glycine
# elsif ( $codon =~ /GGT/i ) { return 'G' } # Glycine
# else {
# print STDERR "Bad codon \"$codon\"!!\n";
# exit;
# }
# }
# From Chapter 8
# codon2aa
#
# A subroutine to translate a DNA 3-character codon to an amino acid
# Version 2
# This subroutine is commented out because a preferred version of it
# follows.
# sub codon2aa {
# my($codon) = @_;
#
# if ( $codon =~ /GC./i) { return 'A' } # Alanine
# elsif ( $codon =~ /TG[TC]/i) { return 'C' } # Cysteine
# elsif ( $codon =~ /GA[TC]/i) { return 'D' } # Aspartic Acid
# elsif ( $codon =~ /GA[AG]/i) { return 'E' } # Glutamic Acid
# elsif ( $codon =~ /TT[TC]/i) { return 'F' } # Phenylalanine
# elsif ( $codon =~ /GG./i) { return 'G' } # Glycine
# elsif ( $codon =~ /CA[TC]/i) { return 'H' } # Histidine
# elsif ( $codon =~ /AT[TCA]/i) { return 'I' } # Isoleucine
# elsif ( $codon =~ /AA[AG]/i) { return 'K' } # Lysine
# elsif ( $codon =~ /TT[AG]|CT./i) { return 'L' } # Leucine
# elsif ( $codon =~ /ATG/i) { return 'M' } # Methionine
# elsif ( $codon =~ /AA[TC]/i) { return 'N' } # Asparagine
# elsif ( $codon =~ /CC./i) { return 'P' } # Proline
# elsif ( $codon =~ /CA[AG]/i) { return 'Q' } # Glutamine
# elsif ( $codon =~ /CG.|AG[AG]/i) { return 'R' } # Arginine
# elsif ( $codon =~ /TC.|AG[TC]/i) { return 'S' } # Serine
# elsif ( $codon =~ /AC./i) { return 'T' } # Threonine
# elsif ( $codon =~ /GT./i) { return 'V' } # Valine
# elsif ( $codon =~ /TGG/i) { return 'W' } # Tryptophan
# elsif ( $codon =~ /TA[TC]/i) { return 'Y' } # Tyrosine
# elsif ( $codon =~ /TA[AG]|TGA/i) { return '_' } # Stop
# else {
# print STDERR "Bad codon \"$codon\"!!\n";
# exit;
# }
# }
# From Chapter 8
#
# codon2aa
#
# A subroutine to translate a DNA 3-character codon to an amino acid
# Version 3, using hash lookup
sub codon2aa {
my($codon) = @_;
$codon = uc $codon;
my(%genetic_code) = (
'TCA' => 'S', # Serine
'TCC' => 'S', # Serine
'TCG' => 'S', # Serine
'TCT' => 'S', # Serine
'TTC' => 'F', # Phenylalanine
'TTT' => 'F', # Phenylalanine
'TTA' => 'L', # Leucine
'TTG' => 'L', # Leucine
'TAC' => 'Y', # Tyrosine
'TAT' => 'Y', # Tyrosine
'TAA' => '_', # Stop
'TAG' => '_', # Stop
'TGC' => 'C', # Cysteine
'TGT' => 'C', # Cysteine
'TGA' => '_', # Stop
'TGG' => 'W', # Tryptophan
'CTA' => 'L', # Leucine
'CTC' => 'L', # Leucine
'CTG' => 'L', # Leucine
'CTT' => 'L', # Leucine
'CCA' => 'P', # Proline
'CCC' => 'P', # Proline
'CCG' => 'P', # Proline
'CCT' => 'P', # Proline
'CAC' => 'H', # Histidine
'CAT' => 'H', # Histidine
'CAA' => 'Q', # Glutamine
'CAG' => 'Q', # Glutamine
'CGA' => 'R', # Arginine
'CGC' => 'R', # Arginine
'CGG' => 'R', # Arginine
'CGT' => 'R', # Arginine
'ATA' => 'I', # Isoleucine
'ATC' => 'I', # Isoleucine
'ATT' => 'I', # Isoleucine
'ATG' => 'M', # Methionine
'ACA' => 'T', # Threonine
'ACC' => 'T', # Threonine
'ACG' => 'T', # Threonine
'ACT' => 'T', # Threonine
'AAC' => 'N', # Asparagine
'AAT' => 'N', # Asparagine
'AAA' => 'K', # Lysine
'AAG' => 'K', # Lysine
'AGC' => 'S', # Serine
'AGT' => 'S', # Serine
'AGA' => 'R', # Arginine
'AGG' => 'R', # Arginine
'GTA' => 'V', # Valine
'GTC' => 'V', # Valine
'GTG' => 'V', # Valine
'GTT' => 'V', # Valine
'GCA' => 'A', # Alanine
'GCC' => 'A', # Alanine
'GCG' => 'A', # Alanine
'GCT' => 'A', # Alanine
'GAC' => 'D', # Aspartic Acid
'GAT' => 'D', # Aspartic Acid
'GAA' => 'E', # Glutamic Acid
'GAG' => 'E', # Glutamic Acid
'GGA' => 'G', # Glycine
'GGC' => 'G', # Glycine
'GGG' => 'G', # Glycine
'GGT' => 'G', # Glycine
);
if(exists $genetic_code{$codon}) {
return $genetic_code{$codon};
}else{
print STDERR "Bad codon \"$codon\"!!\n";
exit;
}
}
# From example6-3.pl
sub countG {
# return a count of the number of G's in the argument $dna
# initialize arguments and variables
my($dna) = @_;
my($count) = 0;
# Use the fourth method of counting nucleotides in DNA, as shown in
# Chapter Four, "Motifs and Loops"
$count = ( $dna =~ tr/Gg//);
return $count;
}
# From Chapter 8
# dna2peptide
#
# A subroutine to translate DNA sequence into a peptide
sub dna2peptide {
my($dna) = @_;
use strict;
use warnings;
use BeginPerlBioinfo; # see Chapter 6 about this module
# Initialize variables
my $protein = '';
# Translate each three-base codon to an amino acid, and append to a protein
for(my $i=0; $i < (length($dna) - 2) ; $i += 3) {
$protein .= codon2aa( substr($dna,$i,3) );
}
return $protein;
}
# From example11-5.pl
# extractSEQRES
#
#-given an scalar containing SEQRES lines,
# return an array containing the chains of the sequence
sub extractSEQRES {
use strict;
use warnings;
my($seqres) = @_;
my $lastchain = '';
my $sequence = '';
my @results = ( );
# make array of lines
my @record = split ( /\n/, $seqres);
foreach my $line (@record) {
# Chain is in column 12, residues start in column 20
my ($thischain) = substr($line, 11, 1);
my($residues) = substr($line, 19, 52); # add space at end
# Check if a new chain, or continuation of previous chain
if("$lastchain" eq "") {
$sequence = $residues;
}elsif("$thischain" eq "$lastchain") {
$sequence .= $residues;
# Finish gathering previous chain (unless first record)
}elsif ( $sequence ) {
push(@results, $sequence);
$sequence = $residues;
}
$lastchain = $thischain;
}
# save last chain
push(@results, $sequence);
return @results;
}
# From example12-2.pl
# extract_HSP_information
#
# -parse a HSP from a BLAST output alignment section
# - return array with elements:
# Expect value
# Query string
# Query range
# Subject string
# Subject range
sub extract_HSP_information {
my($HSP) = @_;
# declare and initialize variables
my($expect) = '';
my($query) = '';
my($query_range) = '';
my($subject) = '';
my($subject_range) = '';
($expect) = ($HSP =~ /Expect = (\S+)/);
$query = join ( '' , ($HSP =~ /^Query.*\n/gm) );
$subject = join ( '' , ($HSP =~ /^Sbjct.*\n/gm) );
$query_range = join('..', ($query =~ /(\d+).*\D(\d+)/s));
$subject_range = join('..', ($subject =~ /(\d+).*\D(\d+)/s));
$query =~ s/[^acgt]//g;
$subject =~ s/[^acgt]//g;
return ($expect, $query, $query_range, $subject, $subject_range);
}
# From Chapter 8
# extract_sequence_from_fasta_data
#
# A subroutine to extract FASTA sequence data from an array
sub extract_sequence_from_fasta_data {
my(@fasta_file_data) = @_;
use strict;
use warnings;
# Declare and initialize variables
my $sequence = '';
foreach my $line (@fasta_file_data) {
# discard blank line
if ($line =~ /^\s*$/) {
next;
# discard comment line
} elsif($line =~ /^\s*#/) {
next;
# discard fasta header line
} elsif($line =~ /^>/) {
next;
# keep line, add to sequence string
} else {
$sequence .= $line;
}
}
# remove non-sequence data (in this case, whitespace) from $sequence string
$sequence =~ s/\s//g;
return $sequence;
}
# From example10-5.pl
# get_annotation_and_dna
#
# - given filehandle to open GenBank library file, get next record
sub get_annotation_and_dna {
my($record) = @_;
my($annotation) = '';
my($dna) = '';
# Now separate the annotation from the sequence data
($annotation, $dna) = ($record =~ /^(LOCUS.*ORIGIN\s*\n)(.*)\/\/\n/s);
# clean the sequence of any whitespace or / characters
# (the / has to be written \/ in the character class, because
# / is a metacharacter, so it must be "escaped" with \)
$dna =~ s/[\s\/\d]//g;
return($annotation, $dna)
}
# From Chapter 8
# A Subroutine to Read FASTA Files
# get_file_data
#
# A subroutine to get data from a file given its filename
sub get_file_data {
my($filename) = @_;
use strict;
use warnings;
# Initialize variables
my @filedata = ( );
unless( open(GET_FILE_DATA, $filename) ) {
print STDERR "Cannot open file \"$filename\"\n\n";
exit;
}
@filedata = <GET_FILE_DATA>;
close GET_FILE_DATA;
return @filedata;
}
# From example10-5.pl
# get_next_record
#
# - given GenBank record, get annotation and DNA
sub get_next_record {
my($fh) = @_;
my($offset);
my($record) = '';
my($save_input_separator) = $/;
$/ = "//\n";
$record = <$fh>;
$/ = $save_input_separator;
return $record;
}
# From example11-5.pl
# iub3to1
#
#-change string of 3-character IUB amino acid codes (whitespace separated)
# into a string of 1-character amino acid codes
sub iub3to1 {
my($input) = @_;
my %three2one = (
'ALA' => 'A',
'VAL' => 'V',
'LEU' => 'L',
'ILE' => 'I',
'PRO' => 'P',
'TRP' => 'W',
'PHE' => 'F',
'MET' => 'M',
'GLY' => 'G',
'SER' => 'S',
'THR' => 'T',
'TYR' => 'Y',
'CYS' => 'C',
'ASN' => 'N',
'GLN' => 'Q',
'LYS' => 'K',
'ARG' => 'R',
'HIS' => 'H',
'ASP' => 'D',
'GLU' => 'E',
);
# clean up the input
$input =~ s/\n/ /g;
my $seq = '';
# This use of split separates on any contiguous whitespace
my @code3 = split(' ', $input);
foreach my $code (@code3) {
# A little error checking
if(not defined $three2one{$code}) {
print "Code $code not defined\n";
next;
}
$seq .= $three2one{$code};
}
return $seq;
}
# From example9-1.pl
# Example 9-1 Translate IUB ambiguity codes to regular expressions
# IUB_to_regexp
#
# A subroutine that, given a sequence with IUB ambiguity codes,
# outputs a translation with IUB codes changed to regular expressions
#
# These are the IUB ambiguity codes
# (Eur. J. Biochem. 150: 1-5, 1985):
# R = G or A
# Y = C or T
# M = A or C
# K = G or T
# S = G or C
# W = A or T
# B = not A (C or G or T)
# D = not C (A or G or T)
# H = not G (A or C or T)
# V = not T (A or C or G)
# N = A or C or G or T
sub IUB_to_regexp {
my($iub) = @_;
my $regular_expression = '';
my %iub2character_class = (
A => 'A',
C => 'C',
G => 'G',
T => 'T',
R => '[GA]',
Y => '[CT]',
M => '[AC]',
K => '[GT]',
S => '[GC]',
W => '[AT]',
B => '[CGT]',
D => '[AGT]',
H => '[ACT]',
V => '[ACG]',
N => '[ACGT]',
);
# Remove the ^ signs from the recognition sites
$iub =~ s/\^//g;
# Translate each character in the iub sequence
for ( my $i = 0 ; $i < length($iub) ; ++$i ) {
$regular_expression
.= $iub2character_class{substr($iub, $i, 1)};
}
return $regular_expression;
}
# From example11-3.pl
# list_recursively
#
# list the contents of a directory,
# recursively listing the contents of any subdirectories
sub list_recursively {
my($directory) = @_;
my @files = ( );
# Open the directory
unless(opendir(DIRECTORY, $directory)) {
print "Cannot open directory $directory!\n";
exit;
}
# Read the directory, ignoring special entries "." and ".."
#
@files = grep (!/^\.\.?$/, readdir(DIRECTORY));
closedir(DIRECTORY);
# If file, print its name
# If directory, recursively print its contents
# Notice that we need to prepend the directory name!
foreach my $file (@files) {
# If the directory entry is a regular file
if (-f "$directory/$file") {
print "$directory/$file\n";
# If the directory entry is a subdirectory
}elsif( -d "$directory/$file") {
# Here is the recursive call to this subroutine
list_recursively("$directory/$file");
}
}
}
# From example7-3.pl
# and
# From example7-4.pl
# make_random_DNA
#
# Make a string of random DNA of specified length.
#
# WARNING: make sure you call srand to seed the
# random number generator before you call this function.
sub make_random_DNA {
# Collect arguments, declare variables
my($length) = @_;
my $dna;
for (my $i=0 ; $i < $length ; ++$i) {
$dna .= randomnucleotide( );
}
return $dna;
}
# From example7-3.pl
# and
# From example7-4.pl
# make_random_DNA_set
#
# Make a set of random DNA
#
# Accept parameters setting the maximum and minimum length of
# each string of DNA, and the number of DNA strings to make
#
# WARNING: make sure you call srand to seed the
# random number generator before you call this function.
sub make_random_DNA_set {
# Collect arguments, declare variables
my($minimum_length, $maximum_length, $size_of_set) = @_;
# length of each DNA fragment
my $length;
# DNA fragment
my $dna;
# set of DNA fragments
my @set;
# Create set of random DNA
for (my $i = 0; $i < $size_of_set ; ++$i) {
# find a random length between min and max
$length = randomlength ($minimum_length, $maximum_length);
# make a random DNA fragment
$dna = make_random_DNA ( $length );
# add $dna fragment to @set
push( @set, $dna );
}
return @set;
}
# From example9-3.pl
#
# Find locations of a match of a regular expression in a string
#
#
# return an array of positions where the regular expression
# appears in the string
#
sub match_positions {
my($regexp, $sequence) = @_;
use strict;
use BeginPerlBioinfo; # see Chapter 6 about this module
#
# Declare variables
#
my @positions = ( );
#
# Determine positions of regular expression matches
#
while ( $sequence =~ /$regexp/ig ) {
push ( @positions, pos($sequence) - length($&) + 1);
}
return @positions;
}
# From example7-4.pl
# matching_percentage
#
# Subroutine to calculate the percentage of identical bases in two
# equal length DNA sequences
sub matching_percentage {
my($string1, $string2) = @_;
# we assume that the strings have the same length
my($length) = length($string1);
my($position);
my($count) = 0;
for ($position=0; $position < $length ; ++$position) {
if(substr($string1,$position,1) eq substr($string2,$position,1)) {
++$count;
}
}
return $count / $length;
}
# From example7-2.pl
# A subroutine to perform a mutation in a string of DNA
#
# WARNING: make sure you call srand to seed the
# random number generator before you call this function.
sub mutate {
my($dna) = @_;
my(@nucleotides) = ('A', 'C', 'G', 'T');
# Pick a random position in the DNA
my($position) = randomposition($dna);
# Pick a random nucleotide
my($newbase) = randomnucleotide(@nucleotides);
# Insert the random nucleotide into the random position in the DNA
# The substr arguments mean the following:
# In the string $dna at position $position change 1 character to
# the string in $newbase
substr($dna,$position,1,$newbase);
return $dna;
}
# From Chapter 7
# mutate_better
#
# Subroutine to perform a mutation in a string of DNA-version 2, in which
# it is guaranteed that one base will change on each call
#
# WARNING: make sure you call srand to seed the
# random number generator before you call this function.
sub mutate_better {
my($dna) = @_;
my(@nucleotides) = ('A', 'C', 'G', 'T');
# Pick a random position in the DNA
my($position) = randomposition($dna);
# Pick a random nucleotide
my($newbase);
do {
$newbase = randomnucleotide(@nucleotides);
# Make sure it's different than the nucleotide we're mutating
}until ( $newbase ne substr($dna, $position,1) );
# Insert the random nucleotide into the random position in the DNA
# The substr arguments mean the following:
# In the string $dna at position $position change 1 character to
# the string in $newbase
substr($dna,$position,1,$newbase);
return $dna;
}
# From example11-4.pl
# Example 11-4 Demonstrate File::Find
sub my_sub {
-f and (print $File::Find::name, "\n");
}
# From example10-5.pl
# open_file
#
# - given filename, set filehandle
sub open_file {
my($filename) = @_;
my $fh;
unless(open($fh, $filename)) {
print "Cannot open file $filename\n";
exit;
}
return $fh;
}
# From example10-1.pl
# parse1
#
# -parse annotation and sequence from GenBank record
sub parse1 {
my($annotation, $dna, $filename) = @_;
# $annotation-reference to array
# $dna -reference to scalar
# $filename -scalar
# declare and initialize variables
my $in_sequence = 0;
my @GenBankFile = ( );
# Get the GenBank data into an array from a file
@GenBankFile = get_file_data($filename);
# Extract all the sequence lines
foreach my $line (@GenBankFile) {
if( $line =~ /^\/\/\n/ ) { # If $line is end-of-record line //\n,
last; #break out of the foreach loop.
} elsif( $in_sequence) { # If we know we're in a sequence,
$$dna .= $line; # add the current line to $$dna.
} elsif ( $line =~ /^ORIGIN/ ) { # If $line begins a sequence,
$in_sequence = 1; # set the $in_sequence flag.
} else{ # Otherwise
push( @$annotation, $line); # add the current line to @annotation.
}
}