(in_file, out_file, r, sentence_type='alt', n_jobs=1)
Generates corpus file from sdf
    
    Parameters
    ----------
    in_file : str
        Input sdf
    out_file : str
        Outfile name prefix, suffix is either _r0, _r1, etc. or _alt_r1 (max radius in alt sentence)
    r : int
        Radius of morgan fingerprint
    sentence_type : str
        Options:    'all' - generates all corpus files for all types of sentences, 
                    'alt' - generates a corpus file with only combined alternating sentence, 
                    'individual' - generates corpus files for each radius
    n_jobs : int
        Number of cores to use (only 'alt' sentence type is parallelized)

    Returns
    -------
Generates corpus file from sdf
    
    Parameters
    ----------
    in_file : str
        Input sdf
    out_file : str
        Outfile name prefix, suffix is either _r0, _r1, etc. or _alt_r1 (max radius in alt sentence)
    r : int
        Radius of morgan fingerprint
    sentence_type : str
        Options:    'all' - generates all corpus files for all types of sentences, 
                    'alt' - generates a corpus file with only combined alternating sentence, 
                    'individual' - generates corpus files for each radius
    n_jobs : int
        Number of cores to use (only 'alt' sentence type is parallelized)
Generates corpus file from sdf Parameters ---------- in_file : str Input sdf out_file : str Outfile name prefix suffix is either _r0 _r1 etc . or _alt_r1 ( max radius in alt sentence ) r : int Radius of morgan fingerprint sentence_type : str Options : all - generates all corpus files for all types of sentences alt - generates a corpus file with only combined alternating sentence individual - generates corpus files for each radius n_jobs : int Number of cores to use ( only alt sentence type is parallelized )
def generate_corpus(in_file, out_file, r, sentence_type='alt', n_jobs=1):

    """Generates corpus file from sdf
    
    Parameters
    ----------
    in_file : str
        Input sdf
    out_file : str
        Outfile name prefix, suffix is either _r0, _r1, etc. or _alt_r1 (max radius in alt sentence)
    r : int
        Radius of morgan fingerprint
    sentence_type : str
        Options:    'all' - generates all corpus files for all types of sentences, 
                    'alt' - generates a corpus file with only combined alternating sentence, 
                    'individual' - generates corpus files for each radius
    n_jobs : int
        Number of cores to use (only 'alt' sentence type is parallelized)

    Returns
    -------
    """

    # File type detection
    in_split = in_file.split('.')
    if in_split[-1].lower() not in ['sdf', 'smi', 'ism', 'gz']:
        raise ValueError('File extension not supported (sdf, smi, ism, sdf.gz, smi.gz)')
    gzipped = False
    if in_split[-1].lower() == 'gz':
        gzipped = True
        if in_split[-2].lower() not in ['sdf', 'smi', 'ism']:
            raise ValueError('File extension not supported (sdf, smi, ism, sdf.gz, smi.gz)')

    file_handles = []
    
    # write only files which contain corpus
    if (sentence_type == 'individual') or (sentence_type == 'all'):
        
        f1 = open(out_file+'_r0.corpus', "w")
        f2 = open(out_file+'_r1.corpus', "w")
        file_handles.append(f1)
        file_handles.append(f2)

    if (sentence_type == 'alt') or (sentence_type == 'all'):
        f3 = open(out_file, "w")
        file_handles.append(f3)
    
    if gzipped:
        import gzip
        if in_split[-2].lower() == 'sdf':
            mols_file = gzip.open(in_file, mode='r')
            suppl = Chem.ForwardSDMolSupplier(mols_file)
        else:
            mols_file = gzip.open(in_file, mode='rt')
            suppl = _read_smi(mols_file)
    else:
        if in_split[-1].lower() == 'sdf':
            suppl = Chem.ForwardSDMolSupplier(in_file)
        else:
            mols_file = open(in_file, mode='rt')
            suppl = _read_smi(mols_file)

    if sentence_type == 'alt':  # This can run parallelized
        result = Parallel(n_jobs=n_jobs, verbose=1)(delayed(_parallel_job)(mol, r) for mol in suppl)
        for i, line in enumerate(result):
            f3.write(str(line) + '\n')
        print('% molecules successfully processed.')

    else:
        for mol in suppl:
            if mol is not None:
                smiles = Chem.MolToSmiles(mol)
                mol = Chem.MolFromSmiles(smiles)
                identifier_sentences, alternating_sentence = mol2sentence(mol, r)

                identifier_sentence_r0 = " ".join(identifier_sentences[0])
                identifier_sentence_r1 = " ".join(identifier_sentences[1])
                alternating_sentence_r0r1 = " ".join(alternating_sentence)

                if len(smiles) != 0:
                    if (sentence_type == 'individual') or (sentence_type == 'all'):
                        f1.write(str(identifier_sentence_r0)+'\n')
                        f2.write(str(identifier_sentence_r1)+'\n')

                    if (sentence_type == 'alt') or (sentence_type == 'all'):
                        f3.write(str(alternating_sentence_r0r1)+'\n')

    for fh in file_handles:
        fh.close()
def generate_corpus ( in_file , out_file , r , sentence_type = 'alt' , n_jobs = 1 ) : # File type detection in_split = in_file . split ( '.' ) if in_split [ - 1 ] . lower ( ) not in [ 'sdf' , 'smi' , 'ism' , 'gz' ] : raise ValueError ( 'File extension not supported (sdf, smi, ism, sdf.gz, smi.gz)' ) gzipped = False if in_split [ - 1 ] . lower ( ) == 'gz' : gzipped = True if in_split [ - 2 ] . lower ( ) not in [ 'sdf' , 'smi' , 'ism' ] : raise ValueError ( 'File extension not supported (sdf, smi, ism, sdf.gz, smi.gz)' ) file_handles = [ ] # write only files which contain corpus if ( sentence_type == 'individual' ) or ( sentence_type == 'all' ) : f1 = open ( out_file + '_r0.corpus' , "w" ) f2 = open ( out_file + '_r1.corpus' , "w" ) file_handles . append ( f1 ) file_handles . append ( f2 ) if ( sentence_type == 'alt' ) or ( sentence_type == 'all' ) : f3 = open ( out_file , "w" ) file_handles . append ( f3 ) if gzipped : import gzip if in_split [ - 2 ] . lower ( ) == 'sdf' : mols_file = gzip . open ( in_file , mode = 'r' ) suppl = Chem . ForwardSDMolSupplier ( mols_file ) else : mols_file = gzip . open ( in_file , mode = 'rt' ) suppl = _read_smi ( mols_file ) else : if in_split [ - 1 ] . lower ( ) == 'sdf' : suppl = Chem . ForwardSDMolSupplier ( in_file ) else : mols_file = open ( in_file , mode = 'rt' ) suppl = _read_smi ( mols_file ) if sentence_type == 'alt' : # This can run parallelized result = Parallel ( n_jobs = n_jobs , verbose = 1 ) ( delayed ( _parallel_job ) ( mol , r ) for mol in suppl ) for i , line in enumerate ( result ) : f3 . write ( str ( line ) + '\n' ) print ( '% molecules successfully processed.' ) else : for mol in suppl : if mol is not None : smiles = Chem . MolToSmiles ( mol ) mol = Chem . MolFromSmiles ( smiles ) identifier_sentences , alternating_sentence = mol2sentence ( mol , r ) identifier_sentence_r0 = " " . join ( identifier_sentences [ 0 ] ) identifier_sentence_r1 = " " . join ( identifier_sentences [ 1 ] ) alternating_sentence_r0r1 = " " . join ( alternating_sentence ) if len ( smiles ) != 0 : if ( sentence_type == 'individual' ) or ( sentence_type == 'all' ) : f1 . write ( str ( identifier_sentence_r0 ) + '\n' ) f2 . write ( str ( identifier_sentence_r1 ) + '\n' ) if ( sentence_type == 'alt' ) or ( sentence_type == 'all' ) : f3 . write ( str ( alternating_sentence_r0r1 ) + '\n' ) for fh in file_handles : fh . close ( )