title: How to calculate and add an FDR column to file in python
body: <p>I have the following input file (Input.xls):</p>
  
  <pre>
  Mouse   No_neigh_mouse  Human   No_neigh_hum    Intersection    TotalGeneTested
  Gm20645 1   lnc3    2   1   8
  Gm20645 1   lnc2    1   0   8
  Gm20645 1   lnc1    2   1   8
  Gm26549 2   lnc3    2   1   8
  Gm26549 2   lnc2    1   1   8
  Gm26549 2   lnc1    2   1   8
  </pre>
  
  <p>I would like to:</p>
  
  <ol>
  <li>Calculate the hypergeom p-value for each row (done successfully)</li>
  <li>Then calculate the fdr (which is same as BH) for p-value corrections </li>
  <li>Add the adjusted p-value as the last column.</li>
  </ol>
  
  <p>My expected output file will have four columns. First is the value of "Mouse", second is the value of "Human", third is "Hypergeom-pvalue", fourth is the "Adjusted-pvalue". I am able to generate the first 3 columns using the following code:</p>
  
  <pre><code>output=open("Hypergeom.xls", "w")
  output.write("Mouse\tHuman\tHypergeom-pvalue\tAdjusted-pvalue\n")
  Input = pd.read_table("Input.xls", sep="\t")
  
  for i in range (0, len(Input.index)):
      hyperg= scipy.stats.hypergeom.sf(Input.ix[i,4], Input.ix[i,5], Input.ix[i,1], Input.ix[i,3],1) #calculates hypergeom p value without a problem
      newline = Input.ix[i,0], Input.ix[i,2], str(hyper)
      output.write('\t'.join(newline)+'\n')
      output.close()
  </code></pre>
  
  <p>Till here, the scripts works fine and I get the following output file ("Hypergeom.xls"):</p>
  
  <pre>
  Mouse   Human   Hypergeom-pvalue    Adjusted-pvalue
  Gm20645 lnc3    0.25
  Gm20645 lnc2    1
  Gm20645 lnc1    0.25
  Gm26549 lnc3    0.464285714
  Gm26549 lnc2    0.25
  Gm26549 lnc1    0.464285714
  </pre>
  
  <p>I then aimed at reopening the output file as an input and then calculate the fdr based on a command suggested by one of the users which utilizes R: <a href="https://stackoverflow.com/questions/7450957/how-to-implement-rs-p-adjust-in-python">How to implement R&#39;s p.adjust in Python</a></p>
  
  <p>My code then:</p>
  
  <pre><code>import rpy2.robjects as R
  pvaluefile = pd.read_table("Hypergeom.xls", sep="\t")
  pvalue_list = pvaluefile.ix[:,2].tolist()  #converts the value column series to a list
  #Now, i try to apply the command from the SO link above
  p_adjusted = R['p.adjust'](R.FloatVector(pvalue_list),method='BH')
  for v in p_adjusted:
      print v
  </code></pre>
  
  <p>I get an error at the step p_adjusted = R [...]. Error is:
  TypeError: 'module' object has no attribute '<strong>getitem</strong>'</p>
  
  <p>Hence, I have two problems:</p>
  
  <ol>
  <li>I cant figure out how to calculate the fdr by overcoming this
  error</li>
  <li>How can i add the fdr column at the end of the file as a fourth column?</li>
  </ol>