I have these files that I want to make table of (https://github.com/learnseq/RNAseqfiles01.git), the table that I want (since all the files have the same ID column),, I want merge the ID column with the count column from each file:

ID    File 1 count column     File 2 count column     File 3 count column   File 4 count column

I cleaned up the files from extra lines using:

grep -vE '^{' GSM2580324_counts.txt | grep -vE '^\\' | grep -v '^ *$' | sed '/^{/d'  | tr '\\' ' ' | sed '/}/d' | grep -vE '^__' > GSM2580324_counts04.txt

But when I try to make a table in R using this:

%R files = list.files(path=".", pattern="GSM")
%R file1 = read.table("GSM2580319_counts01.txt" , col.names=c("gene_id","GSM2580319_counts01.txt" ))
%R file2 = read.table("GSM2580320_counts02.txt" , col.names=c("gene_id","GSM2580320_counts02.txt" ))
%R file3 = read.table("GSM2580323_counts03.txt", col.names=c("gene_id","GSM2580323_counts03.txt" ))
%R file4 = read.table("GSM2580324_counts04.txt", col.names=c("gene_id","GSM2580324_counts04.txt"))
%R out.file = merge (file1, file2, by=c("gene_id"))
%R out.file2 <- merge(out.file, file3, by=c(\"gene_id\"))
%R out.file3 <- merge(out.file2, file4, by=c(\"gene_id\"))
%R outtable01 <- write.table(out.file, file = "out.file3",sep="\t", row.names = FALSE)
%R outtable01                        

I get this error:

RParsingError                             Traceback (most recent call last)
<ipython-input-179-2c471cf65113> in <module>
      7 get_ipython().run_line_magic('R', 'file4 = read.table("GSM2580324_counts04.txt", col.names=c("gene_id","GSM2580324_counts04.txt"))')
      8 get_ipython().run_line_magic('R', 'out.file = merge (file1, file2, by=c("gene_id"))')
----> 9 get_ipython().run_line_magic('R', 'out.file2 <- merge(out.file, file3, by=c(\\"gene_id\\"))')
     10 get_ipython().run_line_magic('R', 'out.file3 <- merge(out.file2, file4, by=c(\\"gene_id\\"))')
     11 get_ipython().run_line_magic('R', 'outtable01 <- write.table(out.file, file = "out.file3",sep="\\t", row.names = FALSE)')

/opt/applications/python/3.6.3/gnu/lib/python3.6/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line, _stack_depth)
   2315                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2316             with self.builtin_trap:
-> 2317                 result = fn(*args, **kwargs)
   2318             return result

<decorator-gen-160> in R(self, line, cell, local_ns)

/opt/applications/python/3.6.3/gnu/lib/python3.6/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    189         if callable(arg):

/opt/applications/python/3.6.3/gnu/lib/python3.6/site-packages/rpy2/ipython/rmagic.py in R(self, line, cell, local_ns)
    729             if line_mode:
    730                 for line in code.split(';'):
--> 731                     text_result, result, visible = self.eval(line)
    732                     text_output += text_result
    733                 if text_result:

/opt/applications/python/3.6.3/gnu/lib/python3.6/site-packages/rpy2/ipython/rmagic.py in eval(self, code)
    262             try:
    263                 # Need the newline in case the last line in code is a comment.
--> 264                 value, visible = ro.r("withVisible({%s\n})" % code)
    265             except (ri.embedded.RRuntimeError, ValueError) as exception:
    266                 # Otherwise next return seems to have copy of error.

/opt/applications/python/3.6.3/gnu/lib/python3.6/site-packages/rpy2/robjects/__init__.py in __call__(self, string)
    386     def __call__(self, string):
--> 387         p = rinterface.parse(string)
    388         res = self.eval(p)
    389         return conversion.rpy2py(res)

/opt/applications/python/3.6.3/gnu/lib/python3.6/site-packages/rpy2/rinterface_lib/conversion.py in _(*args, **kwargs)
     38 def _cdata_res_to_rinterface(function):
     39     def _(*args, **kwargs):
---> 40         cdata = function(*args, **kwargs)
     41         # TODO: test cdata is of the expected CType
     42         return _cdata_to_rinterface(cdata)

/opt/applications/python/3.6.3/gnu/lib/python3.6/site-packages/rpy2/rinterface.py in parse(text, num)
     44         raise TypeError('text must be a string.')
     45     robj = StrSexpVector([text])
---> 46     return _rinterface._parse(robj.__sexp__._cdata, num)

/opt/applications/python/3.6.3/gnu/lib/python3.6/site-packages/rpy2/rinterface_lib/_rinterface_capi.py in _parse(cdata, num)
    556         if status[0] != openrlib.rlib.PARSE_OK:
    557             raise RParsingError('Parsing status not OK',
--> 558                                 PARSING_STATUS(status[0]))
    559     return res

RParsingError: Parsing status not OK

What should I do?


Regarding your grep, why not just grep for what you want, i.e. ^ENSG.*? (These last few lines that htseq-count outputs - which is the software that produces your files - are really important though. You should absolutely look into them to see if something strange is going on with one or more samples.)

Your error comes from the escaped quotes \" you use here:

out.file2 <- merge(out.file, file3, by=c(\"gene_id\"))

Before, you used:

out.file = merge (file1, file2, by=c("gene_id"))

which is the correct way to quote here. On a sidenote, you don't need c() if it's only a vector with one element. You can just write by = "gene_id".

Here's a different way you could read the files:

files = list.files(path=".", pattern="GSM")
count_files <- lapply(files, read.table, col.names = c("GeneID", "Count"))
count_matrix <- do.call(cbind, lapply(count_files, "[[", "Count"))
dimnames(count_matrix) <- list(count_files[[1]]["GeneID"], files)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.