Mac Unix & Perl

  1. On a Mac, plugged in drives appear as subdirectories in the special 'Volumes' directory.
  2. cd ~ and cd achieve the same thing: go to home directory (/Users/whatever)
  3. ls -l (L): list, the long version. ls -a: list all file(include hidden one).
  4. Space view next page, b view previous page,q to quit. j scroll down a line, k scroll up a line.(man man). h to bring up a help page.
  5. rm: remove file; rm -i *.txt: ask for confirmation before deleting; rmdir: remove a directory(empty), mkdir: generate a directory. mkdir -p Temp1/Temp2
  6. touch: to create a new, empty file. eg: touch heaven.txt.
  7. mv: move files. mv heaven.txt Temp/
  8. wildcard: ? caracterizing a single character, and * means everything that matches.
  9. cp: copy file. the default behavior of copy is to overwrite. cp -R Storage Storage1: copy directory.
  10. less: to view text files, not edit.
  11. ls -l: add a letter d to specify that it is a directory. ls -p use trailing slash.
  12. $ alias ls='ls -p' make ls equals ls -p. Aliases only exist in the current terminal session.
  13. nano profile: create a text editor file named profile. write into it and save(Ctrl + O):
    # some useful command line short-cuts
    alias ls='ls -p'
    alias rm='rm -i'
  14. $ source profile
    

      to tell Unix to read the contents of a file and treat it as a series of Unix commands

  15. to show hidden files on mac:
    1. defaults write com.apple.finder AppleShowAllFiles 1
  16. turn off hidden files on mac: ("killall finder" to see effect)
    1. defaults write com.apple.finder AppleShowAllFiles 0
  17. Type: source /Volumes/USB/Unix_and_Perl_course/.profile each time when you open terminal.
  18. grep: search for matched lines, -v to invert. -i to ignore case. -c to count.
  19. $ grep "ATGTGA" intro_IME_data.fasta | less
    $ grep -i ACGTC * | head    #show first 10 lines of matched item
    $ head -n 1 chr1.fasta | sed 's/Chr1/Chromosome 1/' # head -n 1 means the first line. sed to substitute.

      concept of pipe. then press "/" to search some kind of pattern say "ATGTGA", "?" to search backward


  20. wc At_genes.gff; wc -l At_genes.gff: wc count lines, words, bytes, -l count only lines.
  21. #!/usr/bin/perl
    # scalar.pl by Wade
    use warnings;
    
    $x = 3;
    print($x,"\n");
    

      the first line tell us that we can type: $ scalar.pl instead of $ perl scalar.pl.

  22. Any text between single quotes will print exactly as shown: print '$x $s\n'  #=> $x $s\n
  23. if we turen on strict: use strict; it becomes mandatory to say whether the variable is a local or global variable.
    1. my $pi = 3.14;
      

        my: means this is a local variable.

  24. eq (equal to); ne (not equal to); gt (greater than); lt (less than); . (concatenation); cmp (comparison);
  25. print $x == $y ? "yes\n" : "no\n";
  26. method, length(); ord() : convert to number; chr() : convert to letters;
  27. =~ m// equals =~ // : matching; != // : not matching; =~ s/// : substitution(the first met) =~ s///g(global); =~ tr/// : transliteration; 
  28. die "non-DNA character in input\n" if ($input =~ /[efijlopqxz]/i);
    

      die ... if syntax : to stop perl if necessary.

  29. $sequence =~ tr/A-Z/a-z/;
    push @animals, "fox";
    my $length = @animals;
    my @gene_names = qw(unc-10 cyc-1 act-1 let-7 dyf-2);
    my $joined_names = join(", ", @gene_names);

    my @digest = split("", $dna); # split at every possible position at $dna (string);
    1.   If you assign a list to a scalar variable, then the scalar variable becomes the length of the list.
  30. difference between :
    1. $length = @animals;    # variable $length means the size of the array;
    2. ($length) = @animals; # list ($length) contains one element of array @animals;
  31. Array: 
    • pop(@array); shift(@array); push(@array, "element"); unshift(@array, "element"); splice();
    • scalar(@array) : function that calculate the length of the array;
    • index at 1.2, 1.7, .. rounded to 2.  -1 means count from tail.
  1. @sorted_list = sort{$a <=> $b or uc($a) cmp uc($b)} @list;
  2. foreach $animal (@animals) {print "$animal\n"}
  3. for my $i (0..5) {print "$i\n"}
  4. 0 ""(null string) be considered false.
  5. next redo last => continue, redo, break;
  6. while(<>) equals while($_ = <>). chomp() function removes a \n character from the end of a line if present.
  7. #!/usr/bin/perl
    # filemunge.pl
    use strict; use warnings;
    
    open(IN, "<$ARGV[0]") or die "error reading $ARGV[0] for reading";
    open(OUT, ">$ARGV[0].munge") or die "error creating $ARGV[0].munge";
    while(<IN>) {
      chomp;
      my $rev = reverse $_;
      print OUT "$rev\n";
    }
    close IN;
    close OUT:
  8. how to do I/Os: http://www.ualberta.ca/~hquamen/303/filehandles.html ; $! to store error messages. select handle. Perl now allows you to use a regular scalar as a filehandle.
  9. reverse() function both reverse arrays and strings.
  10. #hash
    %genetic_code = (
        ATG => 'Met';
        AAA => 'Lys';
        CCA => 'Pro';        
    );
    foreach $key (keys %genetic_code) {
        print "$key $genetic_code{key}\n";    
    }
    if (exists $genetic_code{AAA}) {print "AAA codon has a value\n"}
    else    {print "No values set for AAA codon\n"}
    delete $genetic_code{AAA};

      The keys() function returns an array of keys, function values() returns an array of values.

  11. sort function: http://perldoc.perl.org/functions/sort.html
  12. varible $&: The string matched by the last successful pattern match.
  13. uc(), lc() function: make string uppercase or lowercase:
    my $str = "What is Perl Language for";
    lc($str);
    print $str, "\n";
    # displays: What is Perl Language for
    $str = lc($str);
    print $str, "\n";
    # displays: what is perl language for
  14. \s matches whitespaces \S matches non-whitespaces. "=~ m/[^ATGC]/i" (negated character class)
  15. if($text =~ m/A{1,3}/) {...} # matches between 1 and 3 As
    if($text =~ m/C{42}/) {...} # matches exactly 42 Cs
    if($text =~ m/T{6,}/) {...} # matches at least 6 Ts
    # to match a "." you have to use backslash "\." eg. $sequence =~ m/A\. thaliana/ {...}
    my @fields = split; # \s+ or $_ are assumed, and space-delimited
  16. Regular expressions in list context return values from parenthesized patterns: my ($beg, $end) = $line =~ /(\d+)\.\.(\d+)/;
  17. file handler can only be processed one at a time. "If your inner loop is a filehandle iterator, then you will need to reset it."
  18. f
  19. f
  20. f
  21. f
  22. f
posted @ 2013-02-09 12:12  wxwcase  阅读(257)  评论(0编辑  收藏  举报