scripts/export/metagenomics/fasta2metseq.py
def fasta_to_metseq_dump(fasta_input, filename=None, dest_dir=None):
    """
    Convert UMGS fasta file header accessions to metseq txt dump

    fasta_input: A UMGS fasta file or a directory with multiple fasta files
    filename: The output filename. If None, uses the UMGS filename by default
    dest_dir: The destination directory. If None, uses the input directory by default

    returns: Void
    """

    if dest_dir is None:
        dest_dir = os.path.split(fasta_input)[0]

    if os.path.isdir(fasta_input):

        # make sure we only list the fasta files
        fasta_files = [x for x in os.listdir(fasta_input) if x.endswith(".fa") or x.endswith(".fasta")]

        for fasta_file in fasta_files:
            filename = fasta_file.partition('.')[0]

            fasta_file_loc = os.path.join(fasta_input, fasta_file)
            temp_file_loc = os.path.join(fasta_input, "." + filename + ".accs")
            # create a hidden accession file
            p.call("grep \">\" %s > %s" % (fasta_file_loc, temp_file_loc), shell=True)

            fp_out = open(os.path.join(dest_dir, filename + '.metseq'), 'w')
            fp_in = open(temp_file_loc, 'r')

            for line in fp_in:
                # strip whitespaces and trim off '>' char
                metseq_acc = line.strip()[1:]
                fp_out.write(filename + '\t' + metseq_acc + '\n')

            fp_out.close()
            fp_in.close()

            # remove temp file
            os.remove(temp_file_loc)

    elif os.path.isfile(fasta_input):
        # get filename
        input_components = os.path.split(fasta_input)

        if filename is None:
            filename = input_components[1].partition('.')[0]

        temp_file_loc = os.path.join(input_components[0], "." + filename + ".accs")

        p.call("grep \">\" %s > %s" % (fasta_input, temp_file_loc), shell=True)

        fp_out = open(os.path.join(dest_dir, filename + '.metseq'), 'w')
        fp_in = open(temp_file_loc, 'r')

        for line in fp_in:
            metseq_acc = line.strip()[1:]
            fp_out.write(filename + '\t' + metseq_acc + '\n')

        fp_out.close()
        fp_in.close()

        os.remove(temp_file_loc)

    Convert UMGS fasta file header accessions to metseq txt dump

    fasta_input: A UMGS fasta file or a directory with multiple fasta files
    filename: The output filename. If None, uses the UMGS filename by default
    dest_dir: The destination directory. If None, uses the input directory by default

    returns: Void
    
Convert UMGS fasta file header accessions to metseq txt dump
fasta_input: A UMGS fasta file or a directory with multiple fasta files
filename: The output filename. If None, uses the UMGS filename by default
dest_dir: The destination directory. If None, uses the input directory by default

Void
Convert UMGS fasta file header accessions to metseq txt dump fasta_input : A UMGS fasta file or a directory with multiple fasta files filename : The output filename . If None uses the UMGS filename by default dest_dir : The destination directory . If None uses the input directory by default Void
def fasta_to_metseq_dump(fasta_input, filename=None, dest_dir=None):
    if dest_dir is None:
        dest_dir = os.path.split(fasta_input)[0]
    if os.path.isdir(fasta_input):
        fasta_files = [x for x in os.listdir(fasta_input) if x.endswith(".fa") or x.endswith(".fasta")]
        for fasta_file in fasta_files:
            filename = fasta_file.partition('.')[0]
            fasta_file_loc = os.path.join(fasta_input, fasta_file)
            temp_file_loc = os.path.join(fasta_input, "." + filename + ".accs")
            p.call("grep \">\" %s > %s" % (fasta_file_loc, temp_file_loc), shell=True)
            fp_out = open(os.path.join(dest_dir, filename + '.metseq'), 'w')
            fp_in = open(temp_file_loc, 'r')
            for line in fp_in:
                metseq_acc = line.strip()[1:]
                fp_out.write(filename + '\t' + metseq_acc + '\n')
            fp_out.close()
            fp_in.close()
            os.remove(temp_file_loc)
    elif os.path.isfile(fasta_input):
        input_components = os.path.split(fasta_input)
        if filename is None:
            filename = input_components[1].partition('.')[0]
        temp_file_loc = os.path.join(input_components[0], "." + filename + ".accs")
        p.call("grep \">\" %s > %s" % (fasta_input, temp_file_loc), shell=True)
        fp_out = open(os.path.join(dest_dir, filename + '.metseq'), 'w')
        fp_in = open(temp_file_loc, 'r')
        for line in fp_in:
            metseq_acc = line.strip()[1:]
            fp_out.write(filename + '\t' + metseq_acc + '\n')
        fp_out.close()
        fp_in.close()
        os.remove(temp_file_loc)
def fasta_to_metseq_dump ( fasta_input , filename = None , dest_dir = None ) : if dest_dir is None : dest_dir = os . path . split ( fasta_input ) [ 0 ] if os . path . isdir ( fasta_input ) : fasta_files = [ x for x in os . listdir ( fasta_input ) if x . endswith ( ".fa" ) or x . endswith ( ".fasta" ) ] for fasta_file in fasta_files : filename = fasta_file . partition ( '.' ) [ 0 ] fasta_file_loc = os . path . join ( fasta_input , fasta_file ) temp_file_loc = os . path . join ( fasta_input , "." + filename + ".accs" ) p . call ( "grep \">\" %s > %s" % ( fasta_file_loc , temp_file_loc ) , shell = True ) fp_out = open ( os . path . join ( dest_dir , filename + '.metseq' ) , 'w' ) fp_in = open ( temp_file_loc , 'r' ) for line in fp_in : metseq_acc = line . strip ( ) [ 1 : ] fp_out . write ( filename + '\t' + metseq_acc + '\n' ) fp_out . close ( ) fp_in . close ( ) os . remove ( temp_file_loc ) elif os . path . isfile ( fasta_input ) : input_components = os . path . split ( fasta_input ) if filename is None : filename = input_components [ 1 ] . partition ( '.' ) [ 0 ] temp_file_loc = os . path . join ( input_components [ 0 ] , "." + filename + ".accs" ) p . call ( "grep \">\" %s > %s" % ( fasta_input , temp_file_loc ) , shell = True ) fp_out = open ( os . path . join ( dest_dir , filename + '.metseq' ) , 'w' ) fp_in = open ( temp_file_loc , 'r' ) for line in fp_in : metseq_acc = line . strip ( ) [ 1 : ] fp_out . write ( filename + '\t' + metseq_acc + '\n' ) fp_out . close ( ) fp_in . close ( ) os . remove ( temp_file_loc )
Convert UMGS fasta file header accessions to metseq txt dump
fasta_input: A UMGS fasta file or a directory with multiple fasta files
filename: The output filename.
Convert UMGS fasta file header accessions to metseq txt dump fasta_input : A UMGS fasta file or a directory with multiple fasta files filename : The output filename .
"""
    Convert UMGS fasta file header accessions to metseq txt dump

    fasta_input: A UMGS fasta file or a directory with multiple fasta files
    filename: The output filename. If None, uses the UMGS filename by default
    dest_dir: The destination directory. If None, uses the input directory by default

    returns: Void
    """ # make sure we only list the fasta files # create a hidden accession file # strip whitespaces and trim off '>' char # remove temp file # get filename
fasta_input
filename
dest_dir