Answers.Body: <p>Ok, it turned out that some rows and/or columns contained all values as zeros and that was the reason for the error. I wrote a function to fix that:</p>
  
  <pre><code>def fix_rawcounts(rawcounts, cell_ids, gene_symbols):
      '''The function checks whether there are 
      any rows or columns with all zeros 
      and removes them'''
      print('Checking whether rows or columns contain all zeros')
  
      # Checking columns
      count_arr = np.apply_along_axis(np.count_nonzero, axis=0, arr=rawcounts )
      rawcounts = rawcounts[:,count_arr != 0]
  
      # Checking rows
      count_arr = np.apply_along_axis(np.count_nonzero, axis=1, arr=rawcounts )
      rawcounts = rawcounts[count_arr != 0,:]
  
      print('finished checking zero rows and columns')
      return rawcounts
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-26T02:25:47.120Z
Answers.LastActivityDate: 2018-10-26T02:25:47.120Z
Answers.UserDisplayName: Nikita Vlasenko
Body: <p>I am trying to run <code>TMM</code> normalization using <code>rpy2</code> and when I run <code>calcNormFactors()</code> function:</p>
  
  <pre><code>dge_list = edgeRLib.DGEList(counts=rawcounts)
  dge_list = edgeRLib.calcNormFactors(dge_list, method="TMM")
  </code></pre>
  
  <p>I am getting the error:</p>
  
  <blockquote>
    <p>RRuntimeError: Error in quantile.default(x, p = p) : 
      missing values and NaN's not allowed if 'na.rm' is FALSE</p>
  </blockquote>
  
  <p>So, I thought that maybe my <code>rawcounts</code> <code>2D</code> <code>numpy</code> array contains <code>nan</code> values but it does not. I checked it with both <code>pd.isnull()</code> and <code>np.isnan()</code> functions. So, I suspect now that the <code>2D</code> array itself can not be normalized for some reason and I do not know why. The error is most probably not related to <code>rpy2</code> set up and <code>R</code> installation because it was working before for similar <code>numpy</code> arrays but failed this time. Any suggestions would be greatly appreciated.</p>
  
Tags: <edger><tmm>
Title: Error in quantile.default(x, p = p) in EgdeR calcNormFactors
ThreadText: # Error in quantile.default(x, p = p) in EgdeR calcNormFactors
  
  (Asked by: username_0 on 2018-10-26)
  
  I am trying to run `TMM` normalization using `rpy2` and when I run `calcNormFactors()` function:
  
  ```
  dge_list = edgeRLib.DGEList(counts=rawcounts)
  dge_list = edgeRLib.calcNormFactors(dge_list, method="TMM")
  
  ```
  
  I am getting the error:
  
  > RRuntimeError: Error in quantile.default(x, p = p) :
  > missing values and NaN's not allowed if 'na.rm' is FALSE
  
  So, I thought that maybe my `rawcounts` `2D` `numpy` array contains `nan` values but it does not. I checked it with both `pd.isnull()` and `np.isnan()` functions. So, I suspect now that the `2D` array itself can not be normalized for some reason and I do not know why. The error is most probably not related to `rpy2` set up and `R` installation because it was working before for similar `numpy` arrays but failed this time. Any suggestions would be greatly appreciated.
  
  **Tags:** edger, tmm
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2018-10-26. Score: 4
  
  Ok, it turned out that some rows and/or columns contained all values as zeros and that was the reason for the error. I wrote a function to fix that:
  
  ```
  def fix_rawcounts(rawcounts, cell_ids, gene_symbols):
      '''The function checks whether there are 
      any rows or columns with all zeros 
      and removes them'''
      print('Checking whether rows or columns contain all zeros')
  
      # Checking columns
      count_arr = np.apply_along_axis(np.count_nonzero, axis=0, arr=rawcounts )
      rawcounts = rawcounts[:,count_arr != 0]
  
      # Checking rows
      count_arr = np.apply_along_axis(np.count_nonzero, axis=1, arr=rawcounts )
      rawcounts = rawcounts[count_arr != 0,:]
  
      print('finished checking zero rows and columns')
      return rawcounts
  
  ```
  
  
  ---
  
  