0

I am using a python package https://pypi.python.org/pypi/mygene to do some gene id mapping. I have code below:

#! /usr/bin/env python

# ID mapping using mygene
# https://pypi.python.org/pypi/mygene
# http://nbviewer.ipython.org/gist/newgene/6771106
# http://mygene-py.readthedocs.org/en/latest/
# 08/30/14

__author__ = 'tommy'

import mygene
import fileinput
import sys


mg = mygene.MyGeneInfo()

# mapping gene symbols to Entrez gene ids and Ensemble gene ids.
# fileinput will loop through all the lines in the input specified as file names given in command-line arguments,
# or the standard input if no arguments are provided.

# build a list from an input file with one gene name in each line
def get_gene_symbols():
    gene_symbols = []
    for line in fileinput.input():
        gene_symbol = line.strip()  # assume each line contains only one gene symbol
        gene_symbols.append(gene_symbol)
    fileinput.close()
    return gene_symbols


Entrez_ids = mg.querymany(get_gene_symbols(), scopes='symbol', fields='entrezgene', species='human', as_dataframe=True)


# set as_dataframe to True will return a pandas dataframe object

Entrez_ids.to_csv(sys.stdout, sep="\t")  # write the dataframe to stdout

# sys.stdout.write()  expects the character buffer object

Entrez_ids.to_csv("Entrez_ids.txt", sep="\t")    # write the pandas dataframe to csv

My question is that if I use Entrez_ids.to_csv("Entrez_ids.txt", sep="\t"), the result file will not include the stderr like ' finished...), but Entrez_ids.to_csv(sys.stdout, sep="\t") will print out both the stderr message and also the dataframe.

How can I redirect the stderr if I use Entrez_ids.to_csv(sys.stdout, sep="\t") ? Thank you!

crazyhottommy
  • 310
  • 1
  • 3
  • 8
  • On commandline, you can use `python your-file.py 2>error.log.txt` to redirect the output. Do you want something else? – doptimusprime Sep 01 '14 at 12:00
  • Thanks, I know this trick. what I did: python my-file.py input.txt 2> /dev/null but the stderr is still printed out in the screen. the input file is like this: CCDC83 MAST3 FLOT1 RPL11 ZDHHC20 LUC7L3 SNORD49A CTSH ACOT8 with one gene name per line. you can test it out by yourself. I think it is because the mg.querymany function will return both the error message and the dataframe. – crazyhottommy Sep 01 '14 at 13:10

1 Answers1

1

This can help you:

In python, can I redirect the output of print function to stderr?

import sys
sys.stderr = to_your_file_object

You can use devnull to suppress the output:

import os
f = open(os.devnull,"w")
sys.stderr = f
Community
  • 1
  • 1
doptimusprime
  • 9,115
  • 6
  • 52
  • 90
  • I tried this and it did not work. Entrez_ids.to_csv(sys.stdout, sep="\t") will write both the error message and dataframe returned by the mg.querymany function to the screen. – crazyhottommy Sep 01 '14 at 13:46
  • Does it mean that error is written on stdout? Try redirecting stdout only. – doptimusprime Sep 01 '14 at 15:29
  • could you please show me how exactly to do this (with code) ? Thank you. I saw this [link]http://stackoverflow.com/questions/1956142/how-to-redirect-stderr-in-python but it seems a little bit too advanced for me. – crazyhottommy Sep 02 '14 at 01:58
  • You can use `sys.stdout = f` to redirect `stdout` before calling your function. If nothing prints on the console, it means that error is being printed on `stdout`. – doptimusprime Sep 02 '14 at 03:58