-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathperl_commands
More file actions
200 lines (140 loc) · 6.3 KB
/
Copy pathperl_commands
File metadata and controls
200 lines (140 loc) · 6.3 KB
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
PERL THINGS TO KNOW
CPAN
sudo cpan install Module::name
@INC
The @INC Array is a special Perl variable that is equivalent to the shell's PATH variable. Whereas PATH contains a list of directories to search for executables, @INC contains a list of directories from which Perl modules and libraries can be loaded.
When you do use(), require(), or do() a filename or a module, Perl gets a list of directories from the @INC variable and searches them for the file it was requested to load. If the file that you want to load is not located in one of the listed directories, then you have to tell Perl where to find the file. You can either provide a path relative to one of the directories in @INC, or you can provide the full path to the file.
LIST THE DIRECTORIES OF THE @INC ARRAY
perl -e "print join(\"\n\", @INC);"
ADD MODULE TO THE @INC ARRAY
1. Add in the script:
use lib 'path/to/module';
or
2. Type in the console:
export PERL5LIB=/path/to/module
or
3. Add 'PERL5LIB=/path/to/module' or 'PERL5LIB=PERL5LIB:/path/to/module' to your .bashrc file. NOTE: The':' in this context means append, so each time you load your PERL5LIB it will grow. So add 'PERL5LIB=""' to your .bashrc file as well, before the 'PERL5LIB=PERL5LIB:/path/to/module' line, so it will reset it and this prevents the growth.
EXECUTE A SHORT PERL SCRIPT DIRECTLY ON THE COMMAND LINE
perl -e "SCRIPT;"
4. perl -I /path/to/lib <script>
#### MODULES AND LIBRARIES ####
Installed modules can usually be found under
/usr/lib/perl5/site_perl/5.16.0/
Different directory levels within modules are separated by :: when calling modules
For example,
Bio::SeqIO can be found in
/usr/lib/perl5/site_perl/5.16.0/Bio/SeqIO
Find out where a module is installed:
perldoc -l Bio::SeqIO
returns
/usr/lib/perl5/site_perl/5.16.0/Bio/SeqIO.pm
#### PERL DEBUGGING ####
Check script functionality line by line
perl -d script.pl -a flag1 -b flag2 etc..
Then, in the prompt, if you hit 'c' and then Enter, it will execute the script until it breaks
#### DATASTRUCTURES ####
ARRAY
@array = ('moo', 'woof', 'miauw');
@array = (1..10);
#Return length of an array
my $length = @array;
HASH
# sort keys of a hash numerically
sort {$a<=>$b} keys
#### FUNCTIONS ####
SPRINTF
sprintf("%03d", $count) Print the count with up to 3 leading zeroes
sprintf("%.3f", $count) Round count to 3 digits after decimal point
MAP
A function that applies another function on an array.
Each element of the array is captured in '$_'
Similar to foreach loop to access the array, but more efficient.
'map{}' will calculate the function for all elements, and thén store the results, whereas
'foreach' will calculate the function and store the result immediately.
This makes 'map{}' faster.
@results = map{ $string.$_ } @array Concatenates $string to each element in the @array and stores it in @results
## GENERAL PERL SCRIPTING ##
REFERENCES
A reference of an array or a hash does not contain the contents of an array or hash, but the 'address', which is a hexadecimal code.
A reference is in datastructure the same as a string.
ARRAY REFERENCE
If you have the following array:
my @array=("moo","woof","miauw")
Then
@array = moo, woof, miauw Normal array
\@array = ARRAY(0x2c25d68) Hexadecimal code
[@array] = ARRAY(0x2c25d68) Hexadecimal code
$array = ARRAY(0x2c25d68) Hexadecimal code
So, \@array = [@array] = $array
To retrieve the original array back, do
@\@array = moo, woof, miauw Normal array
@[@array] = moo, woof, miauw Normal array
@$array = moo, woof, miauw Normal array
Also, when you have a function that returns an array, you can reference it
\{keys %hash} = ARRAY(0x2c25d68)
[keys %hash] = ARRAY(0x2c25d68)
This reference thing is handy when you work with subroutines or functions:
function(\@array, $foo, $bar);
sub function {
my ($var, $foo2, $bar2) = @_;
}
because \@array is effectively a string, $var will be able to take it over.
function(@array, $foo, $bar);
sub function {
my ($var, $foo2, $bar2) = @_;
}
this will not work, because now, @array, $foo and $bar will be concatenated, and put into $var
DOCUMENTATION
perl handbook: perl best practices
perldoc the perlstyle guide, only 15 minutes to read through
perl.com ten essential development practices
lionels perl package:
Phylogenomics::taxa, to standardize taxon names
lionel newperl script
newperl.sh Creates a perldoc template for a new perl script
Template is described in perlTemplate.pl (ettemalab/templates)
POD2USAGE
use Pod::Usage;
pod2usage(-exitval => 1, -verbose => 2) if $help;
-exitval 1
If $help is called, pod2usage will exit the script with exit status 1.
Exit status can be whatever number that you like. When checking the exit status of the script, it can tell you why the script failed.
-verbose 2
Verbose level is 2, meaning the whole perldocumentation in the script is printed
-message => "Message\n"
Message printed to screen when script exits through pod2usage
-msg => "Message\n"
Same as -message
GETOPT
use Getopt::Long;
GetOptions eats the @ARGV; So each stated option reduces @ARGV by 1
GetOptions(
"t|threads=i" => \$threads, # =i states that argument has to be an integer
"f|file=s" => \$file, # =s states that argument has to be a string
"p|probability=f => \$prob, # =f states that argument has to be a float
);
BIO::ALIGNIO
use Bio::AlignIO;
Module for parsing alignments and doing stuff with them
Some useful methods and code:
my ($fasta) = @ARGV;
my $alnio = Bio::AlignIO->new(-format => 'fasta',
-file => $fasta);
my $aln = $alnio->next_aln;
BIO::SIMPLEALIGN
use Bio::SimpleAlign;
Module for parsing alignments and doing stuff with them
Some useful methods and code:
I think you need to read in an AlignIO object (see above)
my $col = $aln->slice(10,10,1);
# here $aln is the aln object parsed from the AlignIO object
# slice() slices a range of sites from the alignments to create a new alignment object
# (10,10,1) Start at 10, End at 10, keep gap-only sites if 1, remove gap-only sites if 0
foreach my $seq ($col->each_seq) {
$colseq .= $seq->seq;
}
# each_seq() parses the alignment object $col, sequence by sequence
# in this example, $col is a single site, so the sequence is the character at that site
# this code creates $colseq, which contains all the characters of that site
$aln->length()
# returns the length of the alignment