Skip to content

syntax error in presplit_map.py #3

Open

Description

@hwick

Hello,
I have tried both python 3.4 and 3.7 with all of the dependencies loaded (using conda environments), but I keep getting this error in presplit_map.py:

    with open_handle(init_split) as splitted, \
                                            ^
SyntaxError: invalid syntax

I tried combining this command into a single line, but still got an error. Is there a work-around for this? Or a specific version of python 3 that I need to be using? I have tried both 3.4 and 3.7.

Activity

  1. LTLA commented on Apr 5, 2019

    @LTLA
    Collaborator

    That should be valid python. Running python3 -m py_compile ./presplit_map.py yields no syntax errors for me (Python 3.7.3, also on Python 2.7.10).

    This small example works for me - make up any file and call it thing1.txt, and do the same with a gzip-compressed file to make thing2.txt.gz:

    import re
    import gzip
    
    is_gz=re.compile("\\.gz$")
    def open_handle(fname, write=False):
        """A function that returns a Gzip file handle or a normal file handle, depending on the file name."""    
        if (is_gz.search(fname)):
            if write:
                mode='wt'
            else:
                mode='rt'
    
            return gzip.open(fname, mode)
        else:
            if write:
                mode='w'
            else:
                mode='rU'
    
            return open(fname, mode)
    
    my_file="thing1.txt"
    my_file2="thing2.txt.gz"
    with open_handle(my_file) as f, \
             open_handle(my_file2) as g:
         print(f)
         print(g)
  2. hwick commented on Apr 5, 2019

    @hwick
    Author

    Thank you for your response. I made a simple text file, thing1.txt with vim, copied and gzipped it to create thing2.txt.
    The output I get when I run this script is:

    (py3.4) [hwick@zappalogin scripts]$ python ALun_pytest.py 
    <_io.TextIOWrapper name='thing1.txt' mode='rU' encoding='UTF-8'>
    <_io.TextIOWrapper name='thing2.txt.gz' encoding='UTF-8'>
    

    Which is not what is in the files.

    When I try adding -m py_compile to run the test script you provided, as you mentioned above, it throws no error but also prints nothing.

    When I try to run the code interactively with python, I get an error after the last "with" statement:

    >>> with open_handle(my_file) as f, \
    ...             open_handle(my_file2) as g:
    ...     print(f)
    ...     print(g)
    ... 
    Traceback (most recent call last):
      File "<stdin>", line 2, in <module>
    AttributeError: __exit__
    

    When I google this error, it seems as though "with" requires an enter and exit method and complains if it doesn't find one.

    Am I running presplit_map.py incorrectly? I ran this 2 years ago on python2 with no issue with the following command:
    python /home/hwick/top2b/master/interaction/scripts/presplit_map.py -G /home/reference/Human/hg19/bowtie2index/hg19 -1 $R1fastq -2 $R2fastq --sig AAGCTAGCTT -o $presplitbam= --cmd bowtie2 --cut cutadapt

    If I add -m py_compile to run presplit_map.py, it tries to use python 2.6:

    Traceback (most recent call last):
      File "/usr/lib64/python2.6/runpy.py", line 122, in _run_module_as_main
        "__main__", fname, loader, pkg_name)
      File "/usr/lib64/python2.6/runpy.py", line 34, in _run_code
        exec code in run_globals
      File "/usr/lib64/python2.6/py_compile.py", line 168, in <module>
        sys.exit(main())
      File "/usr/lib64/python2.6/py_compile.py", line 160, in main
        compile(filename, doraise=True)
      File "/usr/lib64/python2.6/py_compile.py", line 115, in compile
        f = open(file, 'U')
    IOError: [Errno 2] No such file or directory: '-G'
    

    removing -G does not solve the issue, it then claims my reference doesn't exist (it does)

    I'm left scratching my head.

  3. LTLA commented on Apr 5, 2019

    @LTLA
    Collaborator
    1. The io.TextIOWrapper output is correct. The script prints the file handles, not the file contents.
    2. Compiling won't print anything if there are no syntax errors.
    3. Interactively running the script won't work as the open_handle function has a newline in its definition. This means that it can't be properly defined by copy-pasting.
    4. Just force the use of Python 3 to compile presplit_map.py. This causes no problems:
    python3 -m py_compile presplit_map.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions