How to...



  • How to pipe output from a shell command to Perl to a shell command within a Perl script:

    $com='samtools view -h '.$infile;
    my $zom='samtools view -bS - > q64converted.bam';
    open(SBAM, "$com|");
    open SBOUT, "|$zom";
    while (<SBAM>){
    	print SBOUT "$_";
    }
    close(SBAM);
    close SBOUT;
    


  • How to find out which pages are visited according to user country in Google Analytics:

    how to view visited pages by country.jpg



  • How to horizontally split a file view in sublime text editor:

    Split the view by using View -> Layout -> Rows: 2. Then select Row 1 and go to File -> New View into File. It will open second tab with the same file. Then you can drag that tab to the Row 2.



  • How to push a docker image to Docker Hub:

    • You need to have an account on Docker Hub. In this example the account is named 'ning'.
    • The image must be built so that the account name is within the image name

    Eg. for docker command:

    docker tag nextflow_docker:202004201512  ning/nextflow_docker:202004201512
    
    • For a docker-compose file:
    version: '3'
    services:
      nextflow_docker:
        build: ./nextflow_docker
        image: ning/nextflow_docker:latest
    
    • Then you need to push:
    docker logout
    docker login
    #login with your username and password (if you are on ubuntu you can ignore password keyring (agree but dont enter a password)
    #NB. If you are on ubuntu and get the error message "Error calling StartServiceByName for org.freedesktop.secrets: Timeout was reached" , try the following command "sudo apt remove gnome-keyring golang-docker-credential-helpers"
    docker push ning/nextflow_docker:latest
    


  • How to translate a singularity recipe into a Dockerfile:

    • First install spython:
    git clone https://www.github.com/singularityhub/singularity-cli.git
    cd singularity-cli
    python3 setup.py install
    
    • Then perform the conversion:
    spython recipe /input/path/to/singularity/recipe/Singularity /output/path/to/Dockerfile
    


  • How to resize the left and right padding in large screens with CSS. (NB. Probably better to use bootstrap with 3 columns instead):

    • In the head section:
    <head>
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
      <style>   
        @media screen and (min-width: 768px) {
            body {
                padding-left: 100px;
                padding-right: 100px;
            }
        }
      </style>
    
    </head>
    


  • How to horizontally write the address on an envelope in Japan:

    〒106-0044東京都港区東麻布1-8-1 ISビル4F GPlusMedia



  • How to find the meanings of symbols in R:

    ?"@"
    ?"[["
    


  • How to overcome the following error message in Rstudio:

    WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:
    

    In the R console:

    #Find where Rtools is installed:
    Sys.which("make")
    
    #Find where .Rprofile or .Rprofile.site is (my .Rprofile.site was in C:\R\R-3.6.2\etc):
    Sys.getenv("HOME")
    

    Add these two lines to .Rprofile.site (or .Rprofile):

    Sys.setenv(PATH = paste("C:/rtools40/usr/bin", Sys.getenv("PATH"), sep=";"))
    Sys.setenv(BINPREF = "C:/rtools40/mingw_$(WIN)/bin/")
    


  • How to use caret library from R to create folds for k-fold validation:

    # Import caret
    library(caret)
    # 10-Fold cross validation
    valid_sets = createFolds(1:nrow(data), k = 10)
    

    How to create a vector with 10 zeroes in R:

    vector = numeric(length(1:10))
    


  • How to find the most common element in a vector in R:

    mostCommon <- function(x){
         un <- unique(x)
         un[which.max(tabulate(match(x,un)))]
    }
    


  • How to normalise data in R:

    
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    ####FUNCTION THAT CAN BE USED TO SCALE TO ANY RANGE 
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    #define the function
        #x is the input vector
        #y is the scale (eg. you can use eg 1:100 or c(1,100))
    normaliza <- function(x,y){
      xin<-as.numeric(x)
      yin<-as.numeric(y)
      xmin<-min(xin)
      xmax<-max(xin)
      ymin<-min(yin)
      ymax<-max(yin)
      nza<-rep(0,length(xin))
      counta<-1
      for(i in xin){
        new<-(((i - xmin)/(xmax - xmin))*(ymax-ymin))+ymin
        nza[counta]<-new
        counta=counta+1
      }
      return(nza)
    }
    
    #define the scale
    y<-c(0,10)
    
    #an example with a vector:
    
    #define a vector
    test_vector<-1:100
    #test the function on a vector
    nzt<-normaliza(test_vector,y)
    sort(nzt)
    
    #an example with a dataframe
    
    #define a dataframe
    test_frame<-as.data.frame(cbind(test_vector,test_vector))
    #test the function on a dataframe
    nzt_frame <- as.data.frame(lapply(test_frame, normaliza,y=y))
    nzt_frame
    
    
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    #### FUNCTION THAT CAN SCALE FROM 0 to 1  
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    
    #define the function:
    normalize <- function(x) {
        return ((x - min(x)) / (max(x) - min(x)))
    }
    
    #test on a data frame:
    nzt_frame <- lapply(test_frame, normalize)
    nzt_frame
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    


  • How to apply a function with multiple parameters in R:

    mylist <- list(a=1,b=2,c=3)
    myfxn <- function(var1,var2){
      var1*var2
    }
    var2 <- 2
    
    sapply(mylist,myfxn,var2=var2)
    


  • How to manually scale data in R exactly the same as the base 'scale' function:

    #define the data
    vec1<-1:20
    vec2<-21:40
    vecz<-as.matrix(cbind(vec1,vec2))
    
    #define the function
    scalez <- function(q,sc){
      #if not scaling then give 0 for sc
      #you could also simplify below as:
      #return(q/sd(q))   
      if(sc == 0){
        return(q/sqrt(sum(q^2)/(length(q)-1)))
      }
      else{
        #you could also simplify below as:
        #return((q-mean(q))/sd(q))
        return((q-mean(q))/(sqrt((sum((q-mean(q))^2))/(length(q)-1))))
      }
    }
    
    #test our scale function on a vector without centering
    uncentered_vec1<-scalez(vec1,sc=0)
    uncentered_vec1
    
    #test our scale function on a vector with centering
    centered_vec1<-scalez(vec1, sc=1)
    centered_vec1
    
    #test the base scale function on a vector without centering
    scale_nocenter_vec1<-scale(vec1, center = FALSE)
    scale_nocenter_vec1
    
    #test our scale function on a matrix without centering
    uncentered_vecz<-apply(vecz,2, scalez, sc=0)
    uncentered_vecz
    
    #test the base scale function on a matrix without centering
    scale_nocenter_vecz<-scale(vecz, center = FALSE)
    scale_nocenter_vecz
    
    #test our scale function on a matrix with centering
    centered_vecz<-apply(vecz,2, scalez, sc=1)
    centered_vecz
    
    #test the base scale function on a matrix with centering
    scale_center_vecz<-scale(vecz, center = TRUE)
    scale_center_vecz
    
    


  • How to do reverse scaling of scaled data in R:

    #define the data
    vec1<-1:20
    vec2<-21:40
    vecz<-as.matrix(cbind(vec1,vec2))
    
    
    #######################################################################
    #define the function for unscaling
    
    unscalez <- function(z,s,c){
      if(is.vector(z)){
        #if scaling was performed with the scale function then the scaled:scale attribute will exist
        if(length(attr(z,'scaled:scale'))>0){
          #if scaling was performed with the scale function and center option then the scaled:center attribute will exist
          if(length(attr(z, 'scaled:center'))>0){
            #if centering occurred 'scaled:scale'=sd(unscaled vector)
            return((z*(attr(z, 'scaled:scale')))+attr(z, 'scaled:center'))
          }
          else{
            #if centering did not occur 'scaled:scale'=sqrt(sum(q^2)/(length(q)-1)), where q is the unscaled vector
            return(z*attr(z, 'scaled:scale'))
          } 
        }
        else{
          if(length(c)>0){
            #if centering occurred s=sd(unscaled vector),c=mean(unscaled vector)
            return((z*s)+c)
          }
          else{
            #if centering did not occur, s=sqrt(sum(q^2)/(length(q)-1)), where q is the unscaled vector
            return(z*s)
          }
        }
      }
      else{
        columnz <- list()
        for(t in 1:ncol(z)){
          #if scaling was performed with the scale function then the scaled:scale attribute will exist
          if(length(attr(z,'scaled:scale'))>0){
            #if scaling was performed with the scale function and center option then the scaled:center attribute will exist
            if(length(attr(z, 'scaled:center'))>0){
              #if centering occurred 'scaled:scale'=sd(unscaled vector)
              columnz[[t]]<-(z[,t]*(attr(z, 'scaled:scale')[t]))+attr(z, 'scaled:center')[t]
            }
            else{
              #if centering did not occur 'scaled:scale'=sqrt(sum(q^2)/(length(q)-1)), where q is the unscaled vector
              columnz[[t]]<-(z[,t]*attr(z, 'scaled:scale')[t])
            } 
          }
          else{
            if(length(c)>0){
              #if centering occurred s=sd(unscaled vector),c=mean(unscaled vector)
              columnz[[t]]<-((z[,t]*s[t])+c[t])
            }
            else{
              #if centering did not occur, s=sqrt(sum(q^2)/(length(q)-1)), where q is the unscaled vector
              columnz[[t]]<-(z[,t]*s[t])
            }
          }          
        }
        matrixz<-do.call(cbind, columnz)
        return(matrixz)
      }
    }
    
    #######################################################################
    
    #test the unscalez function on a vector created with the base scale function without centering
    scale_nocenter_vec1<-scale(vec1, center = FALSE)
    scale_nocenter_vec1
    unscalez(scale_nocenter_vec1)
    
    #test the unscalez function on a vector created with the base scale function with centering
    scale_center_vec1<-scale(vec1, center = TRUE)
    scale_center_vec1
    unscalez(scale_center_vec1)
    
    #test the unscalezfunction on a matrix created with the base scale function without centering
    scale_nocenter_vecz<-scale(vecz, center = FALSE)
    scale_nocenter_vecz
    unscalez(scale_nocenter_vecz)
    
    #test the unscalez function on a matrix created with the base scale function with centering
    scale_center_vecz<-scale(vecz, center = TRUE)
    scale_center_vecz
    unscalez(scale_center_vecz)
    
    #######################################################################
    
    #test our unscalez function on a vector created with the scalez function (see previous post) without centering
    uncentered_vec1<-scalez(vec1,sc=0)
    uncentered_vec1
    unscalez(uncentered_vec1,s=12.29035,c=NULL)
    
    #test our unscalez function on a vector created with the scalez function (see previous post) with centering
    centered_vec1<-scalez(vec1, sc=1)
    centered_vec1
    unscalez(centered_vec1,s=5.91608,c=10.5)
    
    #test our unscalez function on a matrix created with the scalez function (see previous post) with centering
    centered_vecz<-apply(vecz,2, scalez, sc=1)
    centered_vecz
    unscalez(centered_vecz,s=c(5.91608,5.916080),c=c(10.5,30.5))
    
    #test our unscalez function on a matrix created with the scalez function (see previous post) without centering
    uncentered_vecz<-apply(vecz,2, scalez, sc=0)
    uncentered_vecz
    unscalez(uncentered_vecz,s=c(12.29035,31.84667),c=NULL)
     
    
    


  • How to calculate R-squared in R:

    #where x is the fitted values and y is the actual values
    rsq <- function (x, y) {cor(x, y) ^ 2}
    
    #when given the R-squared value:
    
    adj_rsqz<- function (r,n,p) {
      1-(((1-r)*(n-1))/(n-p-1))
    }
    #where:
    #r=r-squared value
    #n=total sample size
    #p=number of predictors
    


  • How to get all the attributes and variables associated with an object in R:

    str(your_object)
    


  • How to continue a loop in R when there is an error for one of the elements:

      for (j in 1:10){
        tryCatch({ 
           print(j)
        }, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
      }
    


  • How to do matrix multiplication:

    matrix_multiplication_explained2.png



  • How to grab a still / screenshot of an image from a video in DaVinci Resolve:

      1. Click on the 'picture' icon at the bottom.
      1. Right click on the image that appears at the top and select 'Grab still'.
        grab_still1.jpg
      1. Right click on the image that appears in the left pane and select 'Export'.
        grab_still2.jpg
      1. Choose the picture format you wish to use and save.
        grab_still3.jpg

Log in to reply
 

Powered by ShareZomics