Commit bfcb15fc authored by Blaise Li's avatar Blaise Li
Browse files

Trying to fix garbage collection issues.

When R crashes, rpy doesn't seem to handle this properly.
parent c04ad30a
...@@ -14,6 +14,7 @@ as_df = r("as.data.frame") ...@@ -14,6 +14,7 @@ as_df = r("as.data.frame")
from rpy2.rinterface import RRuntimeError from rpy2.rinterface import RRuntimeError
from rpy2.robjects.packages import importr from rpy2.robjects.packages import importr
deseq2 = importr("DESeq2") deseq2 = importr("DESeq2")
#import gc
def do_deseq2(cond_names, conditions, counts_data, def do_deseq2(cond_names, conditions, counts_data,
...@@ -65,6 +66,7 @@ def do_deseq2(cond_names, conditions, counts_data, ...@@ -65,6 +66,7 @@ def do_deseq2(cond_names, conditions, counts_data,
# raise # raise
try: try:
dds = deseq2.estimateSizeFactors_DESeqDataSet(dds, type="ratio") dds = deseq2.estimateSizeFactors_DESeqDataSet(dds, type="ratio")
#gc.collect()
except RRuntimeError as e: except RRuntimeError as e:
if sum(counts_data.prod(axis=1)) == 0: if sum(counts_data.prod(axis=1)) == 0:
msg = "".join(["Error occurred in estimateSizeFactors:\n%s\n" % e, msg = "".join(["Error occurred in estimateSizeFactors:\n%s\n" % e,
...@@ -73,10 +75,12 @@ def do_deseq2(cond_names, conditions, counts_data, ...@@ -73,10 +75,12 @@ def do_deseq2(cond_names, conditions, counts_data,
warnings.warn(msg) warnings.warn(msg)
try: try:
dds = deseq2.estimateSizeFactors_DESeqDataSet(dds, type="poscounts") dds = deseq2.estimateSizeFactors_DESeqDataSet(dds, type="poscounts")
#gc.collect()
except RRuntimeError as e: except RRuntimeError as e:
msg = "".join(["Error occurred in estimateSizeFactors:\n%s\n" % e, msg = "".join(["Error occurred in estimateSizeFactors:\n%s\n" % e,
"We give up."]) "We give up."])
warnings.warn(msg) warnings.warn(msg)
#gc.collect()
raise raise
#print(counts_data.dtypes) #print(counts_data.dtypes)
#print(counts_data.columns) #print(counts_data.columns)
...@@ -91,24 +95,29 @@ def do_deseq2(cond_names, conditions, counts_data, ...@@ -91,24 +95,29 @@ def do_deseq2(cond_names, conditions, counts_data,
#pd.DataFrame({cond : size_factors.loc[cond][0] for cond in COND_NAMES}, index=('size_factor',)) #pd.DataFrame({cond : size_factors.loc[cond][0] for cond in COND_NAMES}, index=('size_factor',))
try: try:
dds = deseq2.estimateDispersions_DESeqDataSet(dds, fitType="parametric") dds = deseq2.estimateDispersions_DESeqDataSet(dds, fitType="parametric")
#gc.collect()
except RRuntimeError as e: except RRuntimeError as e:
msg = "".join(["Error occurred in estimateDispersions:\n%s\n" % e, msg = "".join(["Error occurred in estimateDispersions:\n%s\n" % e,
"We will try with fitType=\"local\"."]) "We will try with fitType=\"local\"."])
warnings.warn(msg) warnings.warn(msg)
try: try:
dds = deseq2.estimateDispersions_DESeqDataSet(dds, fitType="local") dds = deseq2.estimateDispersions_DESeqDataSet(dds, fitType="local")
#gc.collect()
except RRuntimeError as e: except RRuntimeError as e:
msg = "".join(["Error occurred in estimateDispersions:\n%s\n" % e, msg = "".join(["Error occurred in estimateDispersions:\n%s\n" % e,
"We will try with fitType=\"mean\"."]) "We will try with fitType=\"mean\"."])
warnings.warn(msg) warnings.warn(msg)
try: try:
dds = deseq2.estimateDispersions_DESeqDataSet(dds, fitType="mean") dds = deseq2.estimateDispersions_DESeqDataSet(dds, fitType="mean")
#gc.collect()
except RRuntimeError as e: except RRuntimeError as e:
msg = "".join(["Error occurred in estimateDispersions:\n%s\n" % e, msg = "".join(["Error occurred in estimateDispersions:\n%s\n" % e,
"We give up."]) "We give up."])
warnings.warn(msg) warnings.warn(msg)
#gc.collect()
raise raise
dds = deseq2.nbinomWaldTest(dds, betaPrior=deseq2_args["betaPrior"]) dds = deseq2.nbinomWaldTest(dds, betaPrior=deseq2_args["betaPrior"])
#gc.collect()
res = pandas2ri.ri2py(as_df(deseq2.results( res = pandas2ri.ri2py(as_df(deseq2.results(
dds, dds,
contrast=contrast, contrast=contrast,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment