-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileKit.pm
More file actions
executable file
·436 lines (339 loc) · 9.55 KB
/
FileKit.pm
File metadata and controls
executable file
·436 lines (339 loc) · 9.55 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
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
# POD documentation - main docs before the code
=head1 NAME
FuhaoPerl5Lib::FileKit
=head1 SYNOPSIS
File operations
=head1 Requirements
Cwd
File::Copy
=head1 DESCRIPTION
=over 4
=item AddFilePath($file1, $file2)
* Get the full path/basename for a list of files
* Return: $full_name for single file
@=($full_path_file1, $full_path_file2...) for a list file
=item CloseHandlerIfOpen( *FileHandler1, *FileHandler2 )
* Close a list of File handlers if it's open
* Return: 1=success, 0=failure
=item CountLines($file)
* Count file lines
* Return: INT: >0 lines; 0= invalid/empty file
=item DeletePath($path);
* Delete Path and its its content
* Return: 1=Success, 0=Failure
=item MergeFiles ($out, $in1, $in2, ...)
* Merge >=1 file to 1
* Return: 1=Success, 0=Failure
=item RetrieveBasename ($file)
* Retrieve file basename
* Return: $file_basename
=item RetrieveName ($file)
* Retrieve file name
* Return: $file_name
=back
=head1 FEEDBACK
=head2 Support
Please send you questions or bug reports to Email:
I<lufuhao@gmail.com>
=head1 AUTHORS - Fu-Hao Lu
Email: lufuhao@gmail.com (Always)
Fu-Hao.Lu@jic.ac.uk (2012-2016)
=head1 CONTRIBUTORS
None
=head1 APPENDIX
Fight against Bioinformatics with Perl ^_^
=cut
#Coding starts
package FuhaoPerl5Lib::FileKit;
use strict;
use warnings;
use Exporter;
use File::Copy;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
$VERSION = '20171113';
@ISA = qw(Exporter);
@EXPORT = qw(RetrieveDir RetrieveName AddFilePath RetrieveBasename DeletePath MoveFile CopyFile MergeFiles SubsetExtraction CountLines);
@EXPORT_OK = qw();
%EXPORT_TAGS = ( DEFAULT => [qw(RetrieveDir RetrieveName AddFilePath RetrieveBasename DeletePath &MoveFile CopyFile MergeFiles SubsetExtraction CountLines)],
ALL => [qw(RetrieveDir RetrieveName AddFilePath RetrieveBasename DeletePath &MoveFile CopyFile MergeFiles SubsetExtraction CountLines)]);
my $FileKit_success=1;
my $FileKit_failure=0;
### NOT working
### close file handler if exists
### CloseHandlerIfOpen (Handlers)
### Global:
### Dependency:
### Note:
sub CloseHandlerIfOpen {
my @CHIOhanlers=@_;
my $CHIOsubinfo='SUB(FileKit::LuCloseHandlerIfOpen)';
foreach my $CHIOindv_handler (@CHIOhanlers) {
if (defined fileno($CHIOindv_handler)) {
unless (close $CHIOindv_handler) {
print STDERR "${CHIOsubinfo}Error: can not close filehandle for $CHIOindv_handler\n";
return $FileKit_failure;
}
}
}
return $FileKit_success;
}
### Add Path before file
### &AddFilePath ( file1, file2 , ... )
### Global:
### Dependency: Cwd
### Note:
sub AddFilePath {
my @AFPfiles=@_;
use Cwd 'realpath';
foreach (@AFPfiles) {
$_=realpath($_);
}
if (scalar(@AFPfiles)==1) {
return $AFPfiles[0];
}
else {
return (@AFPfiles);
}
}
### Retrieve filename
### &RetrieveName ($file)
### Global:
### Dependency: &AddFilePath
### Note:
sub RetrieveName {
my $RB_ori=shift @_;
chomp $RB_ori;
$RB_ori=&AddFilePath($RB_ori);
my $RB_new='';
($RB_new=$RB_ori)=~ s/.*\///s;
return $RB_new;
}
### Retrieve filebasename without extension
### &RetrvNoExt( file )
### Global:
### Dependency:
### Note:
sub RetrieveBasename {
my $RNE_ori=shift @_;
chomp $RNE_ori;
my $RNE_new='';
my $RNE_base='';
($RNE_base=$RNE_ori)=~ s/.*\///s;
($RNE_new=$RNE_base)=~s/^(\S+)\.\w+$/$1/;
return $RNE_new;
}
### Retrive path for a file
### &RetrvDir(file)
### Global:
### Depedency: &AddFilePath
### Note:
sub RetrieveDir {
my $RD_ori=shift @_;
chomp $RD_ori;
($RD_ori)=&AddFilePath($RD_ori);
(my $RD_new=$RD_ori)=~ s/(.*)\/.*$/$1/s;
return ($RD_new);
}
###delete oath and its contents
#&DeletePath(PATH);
#chdir $curDir || die "Error when deleting directory: $curDir\n";
sub DeletePath {
my $DPpath = shift @_;
my $DPsubinfo="'SUB(FileKit::DeletePath)";
#get all the files in that directory.
my @DPfiles=glob "$DPpath/*";
foreach my $DPind_file (@DPfiles){
if(-d $DPind_file){
#if the destination file is a directory, go recursion.
unless (&DeletePath($DPind_file)) {
print $DPsubinfo, "Error: can not delete PATH: $DPind_file\n";
return $FileKit_failure;
}
}
else{
unless (unlink $DPind_file) {
print $DPsubinfo, "Error: can not delete File: $DPind_file\n";
return $FileKit_failure;
}
}
}
#del the destination directory.
unless (rmdir $DPpath) {
print $DPsubinfo, "Error: can not delete PATH: $DPpath\n";
return $FileKit_failure;
}
return $FileKit_success;
}
sub BackupFile {
my $BUori_name=shift @_;
my $BU_new_name='';
$BU_new_name=$BUori_name.".bak.".int(rand(10000));
if (-e "$BU_new_name") {
&BackupFile($BUori_name);
}
rename("$BUori_name" , "$BU_new_name");
print "The Original file $BUori_name was renamed to $BU_new_name\n";
return $FileKit_success;
}
### Move file using File::Copy
sub MoveFile {
my ($MFori, $MFtar)=@_;
my $MFsubinfo='SUB(FileKit::MoveFile)';
unless (defined $MFori and $MFori=~/^\S+$/ and -s $MFori) {
print STDERR "${MFsubinfo}Error: invalid original name to move\n";
return $FileKit_failure;
}
unless (defined $MFtar and $MFtar=~/^\S+$/) {
print STDERR "${MFsubinfo}Error: invalid target name to move\n";
return $FileKit_failure;
}
unlink ($MFtar) if (-e $MFtar);
unless (move($MFori, $MFtar)) {
return $FileKit_failure;
}
return $FileKit_success;
}
### Copy file using File::Copy
sub CopyFile {
my ($CFori, $CFtar)=@_;
my $CFsubinfo='SUB(FileKit::CopyFile)';
unless (defined $CFori and $CFori=~/^\S+$/ and -s $CFori) {
print STDERR "${CFsubinfo}Error: invalid original name to move\n";
return $FileKit_failure;
}
unless (defined $CFtar and $CFtar=~/^\S+$/) {
print STDERR "${CFsubinfo}Error: invalid target name to copy\n";
return $FileKit_failure;
}
unlink ($CFtar) if (-e $CFtar);
unless (copy($CFori, $CFtar)) {
return $FileKit_failure;
}
return $FileKit_success;
}
### Merge >=1 file to 1
### MergeFiles ($out, $in1, $in2, ...)
### Global:
### Dependency: $FileKit_success;$FileKit_failure;
### Note:
sub MergeFiles {
my $MFout=shift;
my @MFfiles2merge=@_;
local *MFIN; local *MFOUT;
my $MFsubinfo='SUB(FileKit::MergeFiles)';
unlink $MFout if (-e $MFout);
close MFOUT if (defined fileno(MFOUT));
unless (open(MFOUT, "> $MFout")) {
print STDERR "Error: unable to write $MFout\n";
return $FileKit_failure;
}
foreach my $MFindfile (@MFfiles2merge) {
# print $MFsubinfo, "Test: Mergeing file: $MFindfile\n"; ### For test ###
unless (-s $MFindfile) {
print STDERR $MFsubinfo, "Warnings: not existed file to be merged: $MFindfile\n" unless (-e $MFindfile); ### For test ###
next;
}
close MFIN if (defined fileno(MFIN));
unless (open (MFIN, "< $MFindfile")) {
print STDERR $MFsubinfo, "Error: can not open $MFindfile\n";
return $FileKit_failure;
}
while (my $MFline=<MFIN>) {
print MFOUT $MFline;
}
close MFIN;
}
# print $MFsubinfo, "Test: merging file finished: $MFout\n"; ### For test ###
close MFOUT;
if (-s $MFout) {
# print $MFsubinfo, "Test: $MFout\n"; ### For test ###
return $FileKit_success;
}
else {
return $FileKit_failure;
}
}
### Extracta subset from a file if 1st colomn match ID file;
### ID file: one ID/line; lines starting with # or having spaces will be ignored
### SubsetExtraction($input, $ids, $output)
### Global:
### Dependency: $FileKit_success; $FileKit_failure;
### Note:
sub SubsetExtraction {
my ($SEinput, $SEidfile, $SEoutput)=@_;
my $SEsubinfo='SUB(FileKit::SubsetExtraction)';
local *SEINPUT; local *SEIDFILE; local *SEOUTPUT;
my %SEidhash=();
my $SElinenum=0;
my $SEnumout=0;
close SEIDFILE if (defined fileno(SEIDFILE));
unless (open (SEIDFILE, "< $SEidfile")) {
print STDERR $SEsubinfo, "Error: invalid ID file: $SEidfile\n";
return $FileKit_failure;
}
while (my $SEline=<SEIDFILE>) {
chomp $SEline;
$SElinenum++;
if ($SEline=~/^#/ or $SEline=~/\s+/) {
print $SEsubinfo, "Info: ignored line ($SElinenum): $SEline\n";
next;
}
$SEidhash{$SEline}++;
}
close SEIDFILE;
print $SEsubinfo, "### Info: read lines: $SElinenum\n";
print $SEsubinfo, "### Info: invalid ids: ", scalar(keys %SEidhash), "\n";
$SElinenum=0;
close SEINPUT if (defined fileno(SEINPUT));
close SEOUTPUT if (defined fileno(SEOUTPUT));
unless (open (SEINPUT, " < $SEinput")) {
print STDERR $SEsubinfo, "Error: invalid input file: $SEinput\n";
return $FileKit_failure;
}
unless (open (SEOUTPUT, " > $SEoutput")) {
print STDERR $SEsubinfo, "Error: can not write output file: $SEoutput\n";
return $FileKit_failure;
}
while (my $SEline=<SEINPUT>) {
chomp $SEline;
$SElinenum++;
if ($SEline=~/^#/) {
print $SEsubinfo, "Info: ignored line ($SElinenum): $SEline\n";
next;
}
my @SEarr=split(/\s+/, $SEline);
# print $SEsubinfo, "Test: this ID: $SEarr[0]\n"; ### For test ###
if (exists $SEidhash{$SEarr[0]}) {
print SEOUTPUT $SEline, "\n";
$SEnumout++;
}
}
close SEINPUT;
close SEOUTPUT;
print $SEsubinfo, "### Info: reading input lines: $SElinenum\n";
print $SEsubinfo, "### Info: total output lines: $SEnumout\n";
return $FileKit_success;
}
### Count line
sub CountLines {
my $CLfile=shift;
my $CLsubinfo="SUB(FileKit::CountLines)";
local *CLFILEIN;
my $CLlines=0;
unless (defined $CLfile and -s $CLfile) {
print STDERR $CLsubinfo, "Error: invalid file\n";
return $FileKit_failure;
}
close CLFILEIN if(defined fileno(CLFILEIN));
unless (open CLFILEIN, "< $CLfile ") {
print STDERR $CLsubinfo, "Error: failed to open file: $CLfile\n";
return $FileKit_failure;
}
while (<CLFILEIN>) {
$CLlines++;
}
close CLFILEIN;
return $CLlines;
}
#$FileKit_success; $FileKit_failure;
1;