scripts/export/rnac2json.py
def fa_some_records_to_json(seq_dir, fasta_dir, out_dir=None):
    """
    This is a slightly different version of the rnac_to_json methods,
    calling UCSCs faSomeRecords executable to retrieve sequences out of
    fasta input files.

    seq_dir:   The path to the directory containing multiple sequence files
               to be converted to json
    fasta_dir: The path to the directory containing the fasta files of the
               current Rfam release
    out_dir:   The path to the output directory
    """

    if out_dir is None:
        out_dir = seq_dir

    seq_files = os.listdir(seq_dir)
    seq_files = filter(lambda x: string.find(x, ".out") != -1, seq_files)

    # open a new log file for obsolete sequences
    logging.basicConfig(filename=os.path.join(out_dir, "obsolete_seqs.log"),
                        filemode='w', level=logging.DEBUG)
    # new family seqs
    logging.basicConfig(filename=os.path.join(out_dir, "newfamily_seqs.log"),
                        filemode='w', level=logging.WARNING)

    json_obj_list = []

    for seq_file in seq_files:

        fp = open(os.path.join(seq_dir, seq_file), 'r')

        for entry in fp:

            entry = entry.strip().split('\t')

            fam_fa_path = os.path.join(fasta_dir, entry[RFAM_ACC] + ".fa")
            sequence = ''

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

                lfile_path = os.path.join(TMP_PATH, entry[SEQACC] + ".list")
                ofile_path = os.path.join(TMP_PATH, entry[SEQACC] + ".out")
                f_temp = open(lfile_path, 'w')
                f_temp.write(seq_id)
                f_temp.close()

                cmd = "esl-sfetch %s %s %s" % (fam_fa_path, lfile_path, ofile_path)

                # call faSomeRecords to retrieve sequence
                subprocess.call(cmd, shell=True)

                # time.sleep(5)

                # open fsr out file and get sequence
                fsr_out = open(ofile_path, 'r')

                # drop header
                fsr_out.readline()

                # build seq string from file
                sequence = ''
                for line in fsr_out:
                    line = line.strip()
                    sequence = sequence + line

                fsr_out.close()

                sequence = sequence.strip()

                # remove tmp files
                os.remove(lfile_path)
                os.remove(ofile_path)

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

            else:
                # update new families log
                logging.warning("%s", '\t'.join(entry))

        fp_out = open(
            os.path.join(out_dir, seq_file.partition('.')[0] + ".json"), 'w')

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

        json_obj_list = []

        fp_out.close()
        fp.close()

    This is a slightly different version of the rnac_to_json methods,
    calling UCSCs faSomeRecords executable to retrieve sequences out of
    fasta input files.

    seq_dir:   The path to the directory containing multiple sequence files
               to be converted to json
    fasta_dir: The path to the directory containing the fasta files of the
               current Rfam release
    out_dir:   The path to the output directory
    
This is a slightly different version of the rnac_to_json methods,
calling UCSCs faSomeRecords executable to retrieve sequences out of
fasta input files.

The path to the directory containing multiple sequence files
to be converted to json
fasta_dir: The path to the directory containing the fasta files of the
current Rfam release
out_dir:   The path to the output directory
This is a slightly different version of the rnac_to_json methods calling UCSCs faSomeRecords executable to retrieve sequences out of fasta input files . The path to the directory containing multiple sequence files to be converted to json fasta_dir : The path to the directory containing the fasta files of the current Rfam release out_dir : The path to the output directory
def fa_some_records_to_json(seq_dir, fasta_dir, out_dir=None):
    if out_dir is None:
        out_dir = seq_dir
    seq_files = os.listdir(seq_dir)
    seq_files = filter(lambda x: string.find(x, ".out") != -1, seq_files)
    logging.basicConfig(filename=os.path.join(out_dir, "obsolete_seqs.log"),
                        filemode='w', level=logging.DEBUG)
    logging.basicConfig(filename=os.path.join(out_dir, "newfamily_seqs.log"),
                        filemode='w', level=logging.WARNING)
    json_obj_list = []
    for seq_file in seq_files:
        fp = open(os.path.join(seq_dir, seq_file), 'r')
        for entry in fp:
            entry = entry.strip().split('\t')
            fam_fa_path = os.path.join(fasta_dir, entry[RFAM_ACC] + ".fa")
            sequence = ''
            if os.path.exists(fam_fa_path):
                seq_id = entry[SEQACC] + '/' + \
                         entry[SEQ_START] + '-' + entry[SEQ_END]
                lfile_path = os.path.join(TMP_PATH, entry[SEQACC] + ".list")
                ofile_path = os.path.join(TMP_PATH, entry[SEQACC] + ".out")
                f_temp = open(lfile_path, 'w')
                f_temp.write(seq_id)
                f_temp.close()
                cmd = "esl-sfetch %s %s %s" % (fam_fa_path, lfile_path, ofile_path)
                subprocess.call(cmd, shell=True)
                fsr_out = open(ofile_path, 'r')
                fsr_out.readline()
                sequence = ''
                for line in fsr_out:
                    line = line.strip()
                    sequence = sequence + line
                fsr_out.close()
                sequence = sequence.strip()
                os.remove(lfile_path)
                os.remove(ofile_path)
                if sequence != '' and seq_validator(sequence) is True:
                    json_obj_list.append(build_json_dict(entry, sequence))
                else:
                    logging.debug("%s", '\t'.join(entry))
            else:
                logging.warning("%s", '\t'.join(entry))
        fp_out = open(
            os.path.join(out_dir, seq_file.partition('.')[0] + ".json"), 'w')
        json.dump(json_obj_list, fp_out, indent=2, separators=(',', ':'))
        json_obj_list = []
        fp_out.close()
        fp.close()
def fa_some_records_to_json ( seq_dir , fasta_dir , out_dir = None ) : if out_dir is None : out_dir = seq_dir seq_files = os . listdir ( seq_dir ) seq_files = filter ( lambda x : string . find ( x , ".out" ) != - 1 , seq_files ) logging . basicConfig ( filename = os . path . join ( out_dir , "obsolete_seqs.log" ) , filemode = 'w' , level = logging . DEBUG ) logging . basicConfig ( filename = os . path . join ( out_dir , "newfamily_seqs.log" ) , filemode = 'w' , level = logging . WARNING ) json_obj_list = [ ] for seq_file in seq_files : fp = open ( os . path . join ( seq_dir , seq_file ) , 'r' ) for entry in fp : entry = entry . strip ( ) . split ( '\t' ) fam_fa_path = os . path . join ( fasta_dir , entry [ RFAM_ACC ] + ".fa" ) sequence = '' if os . path . exists ( fam_fa_path ) : seq_id = entry [ SEQACC ] + '/' + entry [ SEQ_START ] + '-' + entry [ SEQ_END ] lfile_path = os . path . join ( TMP_PATH , entry [ SEQACC ] + ".list" ) ofile_path = os . path . join ( TMP_PATH , entry [ SEQACC ] + ".out" ) f_temp = open ( lfile_path , 'w' ) f_temp . write ( seq_id ) f_temp . close ( ) cmd = "esl-sfetch %s %s %s" % ( fam_fa_path , lfile_path , ofile_path ) subprocess . call ( cmd , shell = True ) fsr_out = open ( ofile_path , 'r' ) fsr_out . readline ( ) sequence = '' for line in fsr_out : line = line . strip ( ) sequence = sequence + line fsr_out . close ( ) sequence = sequence . strip ( ) os . remove ( lfile_path ) os . remove ( ofile_path ) if sequence != '' and seq_validator ( sequence ) is True : json_obj_list . append ( build_json_dict ( entry , sequence ) ) else : logging . debug ( "%s" , '\t' . join ( entry ) ) else : logging . warning ( "%s" , '\t' . join ( entry ) ) fp_out = open ( os . path . join ( out_dir , seq_file . partition ( '.' ) [ 0 ] + ".json" ) , 'w' ) json . dump ( json_obj_list , fp_out , indent = 2 , separators = ( ',' , ':' ) ) json_obj_list = [ ] fp_out . close ( ) fp . close ( )
This is a slightly different version of the rnac_to_json methods,
calling UCSCs faSomeRecords executable to retrieve sequences out of
fasta input files.
This is a slightly different version of the rnac_to_json methods calling UCSCs faSomeRecords executable to retrieve sequences out of fasta input files .
"""
    This is a slightly different version of the rnac_to_json methods,
    calling UCSCs faSomeRecords executable to retrieve sequences out of
    fasta input files.

    seq_dir:   The path to the directory containing multiple sequence files
               to be converted to json
    fasta_dir: The path to the directory containing the fasta files of the
               current Rfam release
    out_dir:   The path to the output directory
    """ # open a new log file for obsolete sequences # new family seqs # check if the fasta file exists # call faSomeRecords to retrieve sequence # time.sleep(5) # open fsr out file and get sequence # drop header # build seq string from file # remove tmp files # update json obj list # log obsolete sequence # update new families log
seq_dir
fasta_dir
out_dir