Skip to content

Instantly share code, notes, and snippets.

@YulongNiu
YulongNiu / learnS4.R
Last active January 4, 2017 09:03
Example codes in R S4.
##~~~~~~~~~~~~~~~~~~~~~~~~~define class~~~~~~~~~~~~~~~~~~~~~~~~~
## new class
setClass(Class = 'trick',
slots = c(id = 'character', time = 'matrix'),
validity = function(object) {
if (length(object@id) != nrow(object@time)) {
warns <- paste('length of id is', length(object@id), 'is not equal to row number of time', nrow(object@time))
return(warns)
} else {
return(TRUE)
@YulongNiu
YulongNiu / simulated_annealing_max.R
Created July 23, 2017 09:13
Find maximum value by the simulated annealing
SSIndex <- function(i, len, wise = 5) {
min <- i - wise
min <- ifelse(min < 1, 1, min)
max <- i + wise
max <- ifelse(max > len, len, max)
return(seq(min, max))
}
@YulongNiu
YulongNiu / test_sumdouble.cpp
Created October 5, 2017 03:19
Sum from 0.01 to 1 by using double
#include <iostream>
int main() {
double sum = 0.0;
double step = 0.01;
double each = 0.01;
for (int i = 0; i < 100; ++i) {
sum += step;
step += each;
@YulongNiu
YulongNiu / prokaryo_anno_download.R
Last active October 5, 2017 14:19
Download gff/fna/ptt/rnt annotation files of a prokaryotic genome
###Download gff/fna/ptt/rnt annotation files of a prokaryotic genome ####
## Escherichia coli str. K-12 substr. MG1655 (E. coli)
## KEGG ID is 'eco' (http://www.genome.jp/kegg-bin/show_organism?org=eco)
## NCBI assembly ID is 'GCF_000005845.2' (https://www.ncbi.nlm.nih.gov/assembly/GCF_000005845.2)
library('ProGenome') ## version >= 0.06
library('magrittr')
@YulongNiu
YulongNiu / summary_count.sh
Created September 12, 2017 13:30
Summary read counts at each nucleotide position for forward and reverse strands
samtools view -b Example_Condition1.sam -o Example_Condition1.bam
samtools view -b Example_Condition2.sam -o Example_Condition2.bam
## reverse
samtools view -h -f 16 Example_Condition1.bam -o Example_Condition1_rev.bam
## forward
samtools view -h -F 16 Example_Condition1.bam -o Example_Condition1_forw.bam
## reverse
samtools view -h -f 16 Example_Condition2.bam -o Example_Condition2_rev.bam
@YulongNiu
YulongNiu / testmaxint.cpp
Created October 24, 2017 15:13
Test the maximum and minimum int value in C++11
#include <iostream>
#include <limits>
using namespace std;
int main() {
int a = numeric_limits<int>::min();
int b = numeric_limits<int>::max();
@YulongNiu
YulongNiu / testmaxintC.c
Created October 24, 2017 15:06
Test the maximum and minimum int value in C99
#include <stdio.h>
#include <limits.h>
#include <float.h>
int main() {
int a = INT_MIN;
int b = INT_MAX;
double c = DBL_MIN;
@YulongNiu
YulongNiu / QAP_test.R
Created December 16, 2017 08:24
QAP test of three networks
## set working path
setwd('/home/Yulong/RESEARCH/QAPTest/')
## to symmetric matrix
SymMat <- function(mat) {
## example
## tmp1 <- matrix(c(1, '-', '-', 2, 4, '-', 3, 5, 6), ncol = 3, byrow = TRUE)
mat <- apply(mat, 1:2, as.character)
mat <- ifelse(mat == '-', 0, mat)
mat <- apply(mat, 1:2, as.numeric)
@YulongNiu
YulongNiu / testrcpp.cpp
Last active July 23, 2018 06:38
Test Rcpp parallel with C++ versions
#include <RcppArmadillo.h>
#include <RcppParallel.h>
#include <algorithm>
#include <cmath>
using namespace Rcpp;
using namespace RcppParallel;
using namespace arma;
// [[Rcpp::depends(RcppArmadillo, RcppParallel)]]
@YulongNiu
YulongNiu / testRcppparallel.R
Last active July 23, 2018 06:38
Test Rcpp parallel with R versions
library('microbenchmark')
library('Rcpp')
library('RcppParallel')
library('foreach')
library('parallel')
library('doParallel')
library('iterators')
sourceCpp('testrcpp.cpp')