开发者

Gzip problem,traceback and IOError: [Errno 2] No such file or directory

I'm new to python and bioinformatics field. I'm using python-2.6. Now I'm trying to select all fastq.gz files, then gzip.open(just a few lines because it's too huge and time-wasting), then count 'J' , then pick out those files with 'J' count NOT equal to 0.

The following is my code:

#!/usr/bin/python

import os,sys,re,gzip

path = "/home/XXX/nearline"

for file in os.listdir(path):
  if re.match('.*\.recal.fastq.gz', file):
    text = gzip.open(file,'r').readlines()[:10]
    word_list = text.split()
    number = word_list.count('J') + 1
    if number !== 0:
      print file

But I got some errors:

Traceback (most recent call last):
  File "fastqfilter.py", line 9, in <module>
    text = gzip.open(file,'r').readlines()[:10]
  File "/share/lib/python2.6/gzip.py", line 33, in open
    return GzipFile(filename, mode, compresslev开发者_如何转开发el)
  File "/share/lib/python2.6/gzip.py", line 79, in __init__
    fileobj = self.myfileobj = __builtin__.open(filename, mode or 'rb')
IOError: [Errno 2] No such file or directory: 'ERR001268_1.recal.fastq.gz'

What's this traceback: File...... Is there anything wrong with gzip here? And why can't it find ERR001268_1.recal.fastq.gz? It's the first fastq file in the list, and DOES exist there.

Hope give me some clues, and any point out any other errors in the script.

THanks a lot.

Edit: thx everyone. I followed Dan's suggestion. And I tried on ONE fastq file first. My script goes like:

#!/usr/bin/python

import os,sys
import gzip
import itertools

file = gzip.open('/home/xug/nearline/ERR001274_1.recal.fastq.gz','r')
list(itertools.islice(file.xreadlines(),10))
word_list = list.split()
number = word_list.count('J') + 1
if number != 0:
  print 'ERR001274_1.recal.fastq.gz'

Then errors are:

Traceback (most recent call last):
  File "try2.py", line 8, in <module>
    list(itertools.islice(text.xreadlines(),10))
AttributeError: GzipFiles instance has no attribute 'xreadlines'

Edit again: Thx Dan, I've solved the problem yesterday. Seems GzipFiles don't support xreadlines. So I tried the similar way as you suggested later. And it works. See below:

#!/usr/bin/python

import os,sys,re
import gzip
from itertools import islice

path = "/home/XXXX/nearline"

for file in os.listdir(path):
  if re.match('.*\.recal.fastq.gz', file):
    fullpath = os.path.join(path, file)
    myfile = gzip.open(fullpath,'r')
    head = list(islice(myfile,1000))
    word_str = ";".join(str(x) for x in head)
    number = word_str.count('J')
    if number != 0:
      print file


on this line:

text = gzip.open(file,'r').read()

file is a filename not a full path so

fullpath = os.path.join(path, file)
text = gzip.open(fullpath,'r').read()

about F.readlines()[:10] will read the whole file in to a list of lines and then take the first 10

import itertools
list(itertools.islice(F.xreadlines(),10))

this will not read the whole file into memory and will only read the first 10 lines into a list


but as gzip.open returns an object that doesn't have .xreadlines() and but as files are iterable on their lines just:

list(itertools.islice(F,10))

would work as this test shows:

>>> import gzip,itertools
>>> list(itertools.islice(gzip.open("/home/dan/Desktop/rp718.ps.gz"),10))
['%!PS-Adobe-2.0\n', '%%Creator: dvips 5.528 Copyright 1986, 1994 Radical Eye Software\n', '%%Title: WLP-94-int.dvi\n', '%%CreationDate: Mon Jan 16 16:24:41 1995\n', '%%Pages: 6\n', '%%PageOrder: Ascend\n', '%%BoundingBox: 0 0 596 842\n', '%%EndComments\n', '%DVIPSCommandLine: dvips -f WLP-94-int.dvi\n', '%DVIPSParameters: dpi=300, comments removed\n']


Change your code to:

#!/usr/bin/python

import os,sys,re,gzip

path = "/home/XXX/nearline"

for file in os.listdir(path):
  if re.match('.*\.recal.fastq.gz', file):
    text = gzip.open(os.path.join(path,file),'r').readlines()[:10]
    word_list = text.split()
    number = word_list.count('J') + 1
    if number !== 0:
      print file


It's trying to open ERR001268_1.recal.fastq.gz from the working directory, not from /home/XXX/nearline.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜