#!/usr/bin/env python
# sff_splitter 1.0.0
# Generated by dx-app-wizard.
#
# Basic execution pattern: Your app will run on a single machine from
# beginning to end.
#
# See http://wiki.dnanexus.com/Developer-Portal for documentation and
# tutorials on how to modify this file.
#
# DNAnexus Python Bindings (dxpy) documentation:
#   http://autodoc.dnanexus.com/bindings/python/current/

import os
import dxpy
import subprocess

from Bio import SeqIO


@dxpy.entry_point('main')
def main(reads, chunk_size=20000000):

    # The following line(s) initialize your data object inputs on the platform
    # into dxpy.DXDataObject instances that you can start using immediately.

    reads = dxpy.DXFile(reads)

    # The following line(s) download your file inputs to the local file system
    # using variable names for the filenames.

    print "starting SFF file download..."
    dxpy.download_dxfile(reads.get_id(), "reads.sff.gz")
    print "download completed!"

    print "uncompressing reads..."
    subprocess.check_call("gunzip reads.sff.gz", shell=True)
    print "reads successfully unpacked!"

    # Fill in your application code here.

    read_name = reads.describe()['name'].split(".")[0]
    split_filenames = []

    with open("reads.sff", "rb") as fh:
        split_name = read_name+"_0.sff"
        out_file = open(split_name, "wb")

        writer = SeqIO.SffIO.SffWriter(out_file, index=False)
        writer._number_of_reads = chunk_size

        for i, record in enumerate(SeqIO.parse(fh, "sff"), start=0):

            #print record
            if i==0:
                writer.write_file([record])
                continue

            if i % chunk_size == 0:
                print "Switching file at index: "+str(i)
                writer.handle.seek(0) 
                writer._number_of_reads = chunk_size
                writer.write_header() 

                # write to, close old file and add to list
                out_file.close()
                split_filenames.append(split_name)

                # make new file
                split_name = "".join([read_name,"_",str(i),".sff"])
                out_file=open(split_name, "wb")
                writer = SeqIO.SffIO.SffWriter(out_file, index=False)
                writer.write_file([record])
                continue

            writer.write_record(record)

        writer.handle.seek(0) 
        writer._number_of_reads = i % chunk_size
        writer.write_header() 
        out_file.close()
        split_filenames.append(split_name)

    output = {}
    output["split_reads"] = []

    for split_file in split_filenames:
        print "uploading and outputting file: "+split_file
        output["split_reads"].append(dxpy.upload_local_file(split_file))

    return output

dxpy.run()

#!/usr/bin/env python
# sff_splitter 1.0.0
# Generated by dx-app-wizard.
#
# Basic execution pattern: Your app will run on a single machine from
# beginning to end.
#
# See http://wiki.dnanexus.com/Developer-Portal for documentation and
# tutorials on how to modify this file.
#
# DNAnexus Python Bindings (dxpy) documentation:
#   http://autodoc.dnanexus.com/bindings/python/current/

import os
import dxpy
import subprocess

from Bio import SeqIO


@dxpy.entry_point('main')
def main(reads, chunk_size=20000000):

    # The following line(s) initialize your data object inputs on the platform
    # into dxpy.DXDataObject instances that you can start using immediately.

    reads = dxpy.DXFile(reads)

    # The following line(s) download your file inputs to the local file system
    # using variable names for the filenames.

    print "starting SFF file download..."
    dxpy.download_dxfile(reads.get_id(), "reads.sff.gz")
    print "download completed!"

    print "uncompressing reads..."
    subprocess.check_call("gunzip reads.sff.gz", shell=True)
    print "reads successfully unpacked!"

    # Fill in your application code here.

    read_name = reads.describe()['name'].split(".")[0]
    split_filenames = []

    with open("reads.sff", "rb") as fh:
        split_name = read_name+"_0.sff"
        out_file = open(split_name, "wb")

        writer = SeqIO.SffIO.SffWriter(out_file, index=False)
        writer._number_of_reads = chunk_size

        for i, record in enumerate(SeqIO.parse(fh, "sff"), start=0):

            #print record
            if i==0:
                writer.write_file([record])
                continue

            if i % chunk_size == 0:
                print "Switching file at index: "+str(i)
                writer.handle.seek(0) 
                writer._number_of_reads = chunk_size
                writer.write_header() 

                # write to, close old file and add to list
                out_file.close()
                split_filenames.append(split_name)

                # make new file
                split_name = "".join([read_name,"_",str(i),".sff"])
                out_file=open(split_name, "wb")
                writer = SeqIO.SffIO.SffWriter(out_file, index=False)
                writer.write_file([record])
                continue

            writer.write_record(record)

        writer.handle.seek(0) 
        writer._number_of_reads = i % chunk_size
        writer.write_header() 
        out_file.close()
        split_filenames.append(split_name)

    output = {}
    output["split_reads"] = []

    for split_file in split_filenames:
        print "uploading and outputting file: "+split_file
        output["split_reads"].append(dxpy.upload_local_file(split_file))

    return output

dxpy.run()

#!/usr/bin/env python # sff_splitter 1.0.0 # Generated by dx-app-wizard. # # Basic execution pattern: Your app will run on a single machine from # beginning to end. # # See http://wiki.dnanexus.com/Developer-Portal for documentation and # tutorials on how to modify this file. # # DNAnexus Python Bindings (dxpy) documentation: #   http://autodoc.dnanexus.com/bindings/python/current/ # The following line(s) initialize your data object inputs on the platform # into dxpy.DXDataObject instances that you can start using immediately. # The following line(s) download your file inputs to the local file system # using variable names for the filenames. # Fill in your application code here. #print record # write to, close old file and add to list # make new file