Tuesday, September 11, 2012

Perl - USE [ Copy | Move | Constant ]


#!/usr/bin/perl
use strict;
use warnings;

use  File::Copy;
use constant WORK=>'/Users/als/';

copy(WORK."tmp.txt",WORK."tmp.txt.copy") or die "Copy failed $!";

move(WORK."tmp.txt",WORK."Desktop/tmp.txt") or die "Move failed $!";

print "Print constant\t:",WORK;
                           

Monday, September 10, 2012

Perl - [ Create INDEX.HTML ]

 #!/usr/bin/perl
use strict;
use warnings;

&Usage if $#ARGV<0;

my $REGEX = shift;

print "
<html>
    <head>
        <title></title>
        </head>
        <body>
                <center><h1></h1></center>

                                                        ";
foreach my $file (glob ("*.html")){

        my $text = $file;
        $text =~ s/\.$REGEX.*//g;

        print "\t\t<a href=\"$file\">$text</a><br/>\n";
}
print "
        </body>

</html>

";

sub Usage(){

    print "\nUsage: perl CreatePageIndex.pl fastq > index.html\n";
        exit(1);
}

----------------------------------------------------------------------------------------------------

-bash-4.0$ ll
total 2356
-rw-r--r-- 1 andre users 326585 2012-09-10 10:57 Phix.sort.bam.html.html
-rw-r--r-- 1 andre users 350366 2012-09-10 10:57 polipo11p.sort.bam.html.html
-rw-r--r-- 1 andre users 336857 2012-09-10 10:57 polipo17p.sort.bam.html.html
-rw-r--r-- 1 andre users 345978 2012-09-10 10:57 polipo45p.sort.bam.html.html
-rw-r--r-- 1 andre users 342493 2012-09-10 10:57 tumor11t.sort.bam.html.html
-rw-r--r-- 1 andre users 344666 2012-09-10 10:57 tumor17t.sort.bam.html.html
-rw-r--r-- 1 andre users 351703 2012-09-10 10:57 tumor45t.sort.bam.html.html
-bash-4.0$perl CreatePageIndex.pl

Usage: perl CreatePageIndex.pl fastq > index.html

-bash-4.0$ perl CreatePageIndex.pl sort

<html>
    <head>
        <title></title>
        </head>
        <body>
                <center><h1></h1></center>

                                                                        <a href="Phix.sort.bam.html.html">Phix</a><br/>
                <a href="polipo11p.sort.bam.html.html">polipo11p</a><br/>
                <a href="polipo17p.sort.bam.html.html">polipo17p</a><br/>
                <a href="polipo45p.sort.bam.html.html">polipo45p</a><br/>
                <a href="tumor11t.sort.bam.html.html">tumor11t</a><br/>
                <a href="tumor17t.sort.bam.html.html">tumor17t</a><br/>
                <a href="tumor45t.sort.bam.html.html">tumor45t</a><br/>

        </body>

</html>

Perl - [ VIM | headers | pl ]


 less ~/.vim/headers/pl

#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;

my ($output_dir,$number,$input_dir);

&Usage("Too few arguments") if $#ARGV<0;

GetOptions(

        'o|out_dir=s'    => \$output_dir,
        'i|input_dir=s'    => \$input_dir,
        'n|number=i'     => \$number

)or &Usage("Wrong arguments");


&process();

sub process(){

        foreach my $file (glob("*.")){

                open(IN,"$file")or die $!;

                while(<IN>){
                        chomp;
                        my @line = split(/\t/,$_);
                        print "$line[1]\n";
                        die;
                }
        }
}

sub Usage(){
       
        my($msg) = @_;
                  
        print "\nUsage: perl script.pl -o out_dir/ -i input_dir/ -n 1
                                       
        $msg!

        Argument(s):   

        -out_dir|-o    
        -number|-n
        -input_dir|-i\n";
        exit(1);
}

Perl [ Net::FTP ]

#!/usr/bin/perl
use strict;
use warnings;

use Net::FTP;

my $ftp = Net::FTP->new("ftp.suse.com", Debug => 0,  Timeout => 3) or die "Cannot connect to ftp.suse.com: $@";

if ( $ftp->login('anonymous','')){

    my @dirs = $ftp->dir();

    foreach my $dir (@dirs){

            my ($permission,$number1,$user,$group,$number2,$months,$day,$year,$name_path) = split(/\s+/,$dir);

            print "Name path:\t$name_path\n";

            my @sub_dir = $ftp->ls("$name_path/*.txt");

            print "Name file *.txt:\t$sub_dir[0]\n";

            my $file_txt = $ftp->get($sub_dir[0]);

            print "Name file GET $file_txt:\t$sub_dir[0]\n";

            print "Copy $file_txt to LocalHost\n";

            `cp $file_txt ftp.txt`;

            $ftp->quit();
    }
}
-----------------------------------------------------------------------------------------------
Result
perl ftp.pl
Name path:      pub
Name file *.txt:        pub/README.txt
Name file GET README.txt:       pub/README.txt
Copy README.txt to LocalHost
less ftp.txt

Thursday, September 6, 2012