scripts/export/rnac2json.py
def rnac_to_json(rfam2rnac_file, fasta_dir, no_seqs=None, out_dir=None):
    """
    This was initially developed for processing the entire Rfam2RNAcentral
    export with the output split to multiple output files with the number
    of sequences per file set by the parameter no_seqs.

    rfam2rnac_file:  Rfam2RNAcentral db dump
    fasta_dir:       The path to the directory containing the fasta files
                     of the current Rfam release
    no_seqs:         The number of sequences to split input file to
    out_dir:         The path to the output directory
    """

    json_obj_list = []
    sequence = None

    # open a log file for tracking the obsolete sequences
    logging.basicConfig(
        filename="empty_seqs.log", filemode='w', level=logging.DEBUG)

    rnac_fp = open(rfam2rnac_file, 'r')
    filename = os.path.basename(rfam2rnac_file).partition('.')[0]

    # drop header
    rnac_fp.readline()

    f_index = 1  # file index

    for entry in rnac_fp:

        # get entry fields
        entry = entry.strip().split('\t')

        # get family fa file path
        fam_fa_path = os.path.join(fasta_dir, entry[RFAM_ACC] + ".fa")

        sequence = ''
        seq_bits = None

        # check if the fasta file exists
        if os.path.exists(fam_fa_path):
            seq_id = entry[SEQACC] + '/' + \
                     entry[SEQ_START] + '-' + entry[SEQ_END]

            cmd = "esl-sfetch %s %s" % (fam_fa_path, seq_id)
            proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)

            seq = proc.communicate()[0]

            # get sequence
            sequence = ''
            seq_bits = seq.split('\n')[1:]
            sequence = sequence.join(seq_bits)

        # if no fasta file found, get the sequence from ENA
        if sequence == '':
            sequence = fetch_seq_from_ena(entry)

        # build dictionary and update sequence list
        if sequence != '' and seq_validator(sequence) is True:
            json_obj_list.append(build_json_dict(entry, sequence))
        else:
            # log obsolete sequences
            logging.debug("%s", "\t".join(entry))

        if len(json_obj_list) == no_seqs:
            fp_out = open(
                os.path.join(out_dir, filename + str(f_index) + ".json"), 'w')

            json.dump(
                json_obj_list, fp_out, indent=2, separators=(',', ':'))

            fp_out.close()

            f_index = f_index + 1
            json_obj_list = []

        cmd = ''

    # if list != empty write remaining seqs to new file
    if len(json_obj_list) > 0:
        fp_out = open(
            os.path.join(out_dir, filename + str(f_index) + ".json"), 'w')
        json.dump(
            json_obj_list, fp_out, indent=2, separators=(',', ':'))

        fp_out.close()

    rnac_fp.close()

    This was initially developed for processing the entire Rfam2RNAcentral
    export with the output split to multiple output files with the number
    of sequences per file set by the parameter no_seqs.

    rfam2rnac_file:  Rfam2RNAcentral db dump
    fasta_dir:       The path to the directory containing the fasta files
                     of the current Rfam release
    no_seqs:         The number of sequences to split input file to
    out_dir:         The path to the output directory
    
This was initially developed for processing the entire Rfam2RNAcentral
export with the output split to multiple output files with the number
of sequences per file set by the parameter no_seqs.

Rfam2RNAcentral db dump
fasta_dir:       The path to the directory containing the fasta files
of the current Rfam release
no_seqs:         The number of sequences to split input file to
out_dir:         The path to the output directory
This was initially developed for processing the entire Rfam2RNAcentral export with the output split to multiple output files with the number of sequences per file set by the parameter no_seqs . Rfam2RNAcentral db dump fasta_dir : The path to the directory containing the fasta files of the current Rfam release no_seqs : The number of sequences to split input file to out_dir : The path to the output directory
def rnac_to_json(rfam2rnac_file, fasta_dir, no_seqs=None, out_dir=None):
    json_obj_list = []
    sequence = None
    logging.basicConfig(
        filename="empty_seqs.log", filemode='w', level=logging.DEBUG)
    rnac_fp = open(rfam2rnac_file, 'r')
    filename = os.path.basename(rfam2rnac_file).partition('.')[0]
    rnac_fp.readline()
    f_index = 1  
    for entry in rnac_fp:
        entry = entry.strip().split('\t')
        fam_fa_path = os.path.join(fasta_dir, entry[RFAM_ACC] + ".fa")
        sequence = ''
        seq_bits = None
        if os.path.exists(fam_fa_path):
            seq_id = entry[SEQACC] + '/' + \
                     entry[SEQ_START] + '-' + entry[SEQ_END]
            cmd = "esl-sfetch %s %s" % (fam_fa_path, seq_id)
            proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
            seq = proc.communicate()[0]
            sequence = ''
            seq_bits = seq.split('\n')[1:]
            sequence = sequence.join(seq_bits)
        if sequence == '':
            sequence = fetch_seq_from_ena(entry)
        if sequence != '' and seq_validator(sequence) is True:
            json_obj_list.append(build_json_dict(entry, sequence))
        else:
            logging.debug("%s", "\t".join(entry))
        if len(json_obj_list) == no_seqs:
            fp_out = open(
                os.path.join(out_dir, filename + str(f_index) + ".json"), 'w')
            json.dump(
                json_obj_list, fp_out, indent=2, separators=(',', ':'))
            fp_out.close()
            f_index = f_index + 1
            json_obj_list = []
        cmd = ''
    if len(json_obj_list) > 0:
        fp_out = open(
            os.path.join(out_dir, filename + str(f_index) + ".json"), 'w')
        json.dump(
            json_obj_list, fp_out, indent=2, separators=(',', ':'))
        fp_out.close()
    rnac_fp.close()
def rnac_to_json ( rfam2rnac_file , fasta_dir , no_seqs = None , out_dir = None ) : json_obj_list = [ ] sequence = None logging . basicConfig ( filename = "empty_seqs.log" , filemode = 'w' , level = logging . DEBUG ) rnac_fp = open ( rfam2rnac_file , 'r' ) filename = os . path . basename ( rfam2rnac_file ) . partition ( '.' ) [ 0 ] rnac_fp . readline ( ) f_index = 1 for entry in rnac_fp : entry = entry . strip ( ) . split ( '\t' ) fam_fa_path = os . path . join ( fasta_dir , entry [ RFAM_ACC ] + ".fa" ) sequence = '' seq_bits = None if os . path . exists ( fam_fa_path ) : seq_id = entry [ SEQACC ] + '/' + entry [ SEQ_START ] + '-' + entry [ SEQ_END ] cmd = "esl-sfetch %s %s" % ( fam_fa_path , seq_id ) proc = subprocess . Popen ( cmd , shell = True , stdout = subprocess . PIPE ) seq = proc . communicate ( ) [ 0 ] sequence = '' seq_bits = seq . split ( '\n' ) [ 1 : ] sequence = sequence . join ( seq_bits ) if sequence == '' : sequence = fetch_seq_from_ena ( entry ) if sequence != '' and seq_validator ( sequence ) is True : json_obj_list . append ( build_json_dict ( entry , sequence ) ) else : logging . debug ( "%s" , "\t" . join ( entry ) ) if len ( json_obj_list ) == no_seqs : fp_out = open ( os . path . join ( out_dir , filename + str ( f_index ) + ".json" ) , 'w' ) json . dump ( json_obj_list , fp_out , indent = 2 , separators = ( ',' , ':' ) ) fp_out . close ( ) f_index = f_index + 1 json_obj_list = [ ] cmd = '' if len ( json_obj_list ) > 0 : fp_out = open ( os . path . join ( out_dir , filename + str ( f_index ) + ".json" ) , 'w' ) json . dump ( json_obj_list , fp_out , indent = 2 , separators = ( ',' , ':' ) ) fp_out . close ( ) rnac_fp . close ( )
This was initially developed for processing the entire Rfam2RNAcentral
export with the output split to multiple output files with the number
of sequences per file set by the parameter no_seqs.
This was initially developed for processing the entire Rfam2RNAcentral export with the output split to multiple output files with the number of sequences per file set by the parameter no_seqs .
"""
    This was initially developed for processing the entire Rfam2RNAcentral
    export with the output split to multiple output files with the number
    of sequences per file set by the parameter no_seqs.

    rfam2rnac_file:  Rfam2RNAcentral db dump
    fasta_dir:       The path to the directory containing the fasta files
                     of the current Rfam release
    no_seqs:         The number of sequences to split input file to
    out_dir:         The path to the output directory
    """ # open a log file for tracking the obsolete sequences # drop header # file index # get entry fields # get family fa file path # check if the fasta file exists # get sequence # if no fasta file found, get the sequence from ENA # build dictionary and update sequence list # log obsolete sequences # if list != empty write remaining seqs to new file
rfam2rnac_file
fasta_dir
no_seqs
out_dir