Showing posts with label foreach. Show all posts
Showing posts with label foreach. Show all posts

Wednesday, December 26, 2018

foreach run on R needs large RAM

turn off Dropbox, OneDrive, and GoogleDrive can clear off some large chunks of RAM.

foreach ms02 run, yeast PIN, GO terms, lowRAM implementation

This version of foreach write.csv ms02 counts of each ms02 null network. This way use very ~180M RAM for each session.

Yeast PIN foreach MS02 run for GO - dang CR,
each session takes ~180M RAM







ms02files = list.files(path='yeastMS02')
if (debug > 0 ) {ms02files = ms02files[1: 5] }
start = Sys.time()
CPUs = 3
registerDoMC( CPUs )

tmpbuffer= foreach( fi =1:length(ms02files) ) %dopar% {
  file = ms02files[fi]
 #for (file in ms02files ){
  ms02_pairs= read.csv(paste("yeastMS02/", file, sep=''),
                       colClasses = c("character", "character"))
  ms02_pairs = ms02_pairs[,1:2]
  if ( debug > 5 ) { ms02_pairs = ms02_pairs[1:1000, ];  print(paste("foreach:fi=",fi))  }

  tagbufferMS02 = c()
  for ( i in 1:length(ms02_pairs[,1])){
    sub1A = cats[ cats$id == ms02_pairs$id1[i], ]
    sub2A = cats[ cats$id == ms02_pairs$id2[i], ]
    els1A = sub1A$GO
    els2A = sub2A$GO
    if ( is.null(sub1A) ) { els1A = c("NA") }
    if ( is.null(sub2A) ) { els2A = c("NA") }
   
    els1B = as.character( g.dang.Q[ms02_pairs$id1[i]] )  #B for 2nd data set
    els2B = as.character( g.dang.Q[ms02_pairs$id2[i]] )  #B for 2nd data set
    els1B = ifelse( is.na(els1B), "NA", els1B)
    els2B = ifelse( is.na(els2B), "NA", els2B)
     
    tagbuffer1 = allCombinationsOfTwoVectors ( els1A, els2B )
    tagbuffer2 = allCombinationsOfTwoVectors ( els2A, els1B )
    tagbufferMS02 = c( tagbufferMS02, tagbuffer1,tagbuffer2 ) #combine with dataframe buffer
  } #i loop

  F.ms02current = data.frame( table(tagbufferMS02))
  write.csv(F.ms02current, file=paste("tmp/_Fms02tag_", file, sep = ""), quote = T, row.names = F)
}#tmpbuffer

Tuesday, November 28, 2017

RLS P-value evaluation using ms02 permutation

rm(list=ls())
setwd("~/github/0.network.aging.ms02/1.Fraser02")
source("../network.r")
set.seed(2017)
debug = 1; 
list.files(path="../data/")
## [1] "ken-RLS-byORF.csv"                             
## [2] "SummaryRegressionHetHomFactorized2015Oct13.csv"
## [3] "unique_biogrid_ScePPI.csv"
rls = read.csv("../data/ken-RLS-byORF.csv");
biogrid = read.csv("../data/unique_biogrid_ScePPI.csv");
fit = read.csv("../data/SummaryRegressionHetHomFactorized2015Oct13.csv")
ppi = biogrid[, c("Systematic.Name.Interactor.A","Systematic.Name.Interactor.B")];
names(ppi) = c("ORF1", "ORF2" )
#First, define a function to calculate V difference in pairs of proteins
 diff.RLS = function( inpairs ) {
   inpairs$rls1 = rls$avgLS[match( inpairs$ORF1, rls$ORF ) ];
   inpairs$rls2 = rls$avgLS[match( inpairs$ORF2, rls$ORF ) ];
   
   inpairs$essen1 = fit$essenflag[match(inpairs$ORF1, fit$orf)];
   inpairs$essen2 = fit$essenflag[match(inpairs$ORF2, fit$orf)];
   
   inpairs$rls1 = ifelse( inpairs$essen1=='essential', 0, inpairs$rls1);
   inpairs$rls2 = ifelse( inpairs$essen2=='essential', 0, inpairs$rls2);
   
   ret = mean( abs( inpairs$rls1 - inpairs$rls2 ), na.rm=T );
 } 
 # calculate the observed difference in RLS
 diff.RLS.obs = diff.RLS ( ppi );
 paste( "Observed deltaRLS = ", diff.RLS.obs); 
## [1] "Observed deltaRLS =  12.759632586095"
#permutation of pairs, and their difference in Ka
 Nsims = 100; #number of permutations
 permutated.diff.RLS = numeric( Nsims ); #empty vector to store calculations

library(foreach)
library(doMC)
## Loading required package: iterators
## Loading required package: parallel
registerDoMC(cores=5) #Intel i7 has 6 cores

permutated.diff.RLS = foreach(i=1:Nsims) %dopar% {
   new.pairs = ms02_singlerun(ppi ) #generate a new MS02 random network
   new.pairs = new.pairs[,1:2] #reformating into two-columns
   names(new.pairs) = c("ORF1", "ORF2")
   diff.RLS( new.pairs ); 
  }
p-value
permutated.diff.RLS = unlist(permutated.diff.RLS)

summary(permutated.diff.RLS)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   14.14   14.20   14.23   14.23   14.25   14.31
sub = permutated.diff.RLS[ permutated.diff.RLS < diff.RLS.obs ]
paste("pvalue = ", length(sub)/Nsims)
## [1] "pvalue =  0"
hist(permutated.diff.RLS)