def run_jobs( args ):
    if exists( args.lockfile ):
        stderr.write("%s has already existed\n" % args.lockfile )
        return 0
    else:
        if not args.dry_run:
            filesystem_util.touch( args.lockfile )

        rsn = int( basename( getcwd() ) )
        '''
        if args.prepack:
            silent_fn  = "pos_%s_silent.out" % rsn
        else:
            #silent_fn  = "pos_%s_silent_packed_c3.out" % rsn
            #silent_fn  = "pos_%s_silent.out.packed.out" % rsn
            #silent_fn  = glob("pos_%s_silent*packed*.out" % rsn )[0]
            #silent_fn  = glob("pos_%s_silent_packed_v2_minbb.out" % rsn )[0]
        '''
        #silent_fn  = glob("pos_%s_silent%s.out" % args.silentfn_pattern )[0]
        if args.silentfn_pattern:
            #silent_fn  = glob("pos_%s_silent_%s.out" %( rsn, args.silentfn_pattern ))
            #assert len(silent_fn)==1, silent_fn
            #silent_fn = silent_fn[0]
            silent_fn  = "pos_%s_silent_%s.out" %( rsn, args.silentfn_pattern )
        else:
            silent_fn  = "pos_%s_silent.out" % rsn

        print silent_fn
        assert exists( silent_fn )
        extract_out    = extract_cmd( silent_fn )
        clustering_out = clustering_cmd( extract_out )
        parse_results( extract_out, clustering_out )
def run_jobs( args ):
    if exists("run.lock"):
        stderr.write("job has already existed\n")
        return 0
    else:
        filesystem_util.touch("run.lock")

        rsn = int( basename( getcwd() ) )

        assert isdir( args.input_dir ), args.input_dir
        ref_fragfiles = " ".join( glob("%s/after_rotation_frags.%s.%s.*pdb" %( args.input_dir, args.mers, rsn ) ) )
        fragfiles_list = []
        # saner
        j_rsds = range( 1, args.n_total_positions+1 )[rsn:]

        for pdb in glob("%s/after_rotation_frags.%s.*pdb" %( args.input_dir, args.mers )):
            frag_rsn = int( basename(pdb).split(".")[2] )

            # to prevent silly bug
            assert frag_rsn < args.n_total_positions, "ERROR: please reset the n_total_positions!\n"

            if frag_rsn in j_rsds:
                fragfiles_list.append( pdb )

        fragfiles = " ".join( fragfiles_list )

        #flags += "-mapfile %s\n" % args.mapfile
        flags = "-ref_fragfiles %s\n" % ref_fragfiles
        flags += "-fragfiles %s\n" % fragfiles
        flags += "-out:score pos_%s_score.sc\n" % rsn
        flags += "-clash_dist_threshold %s\n" % args.clash_dist
        flags += "-database /work/wangyr/xtal_rosetta_database\n"
        if args.native:
            flags  += "-native %s\n" % args.native

        fast_flags = "-chemical:exclude_patches LowerDNA  UpperDNA Cterm_amidation SpecialRotamer protein_cutpoint_upper protein_cutpoint_lower VirtualBB ShoveBB VirtualDNAPhosphate VirtualNTerm CTermConnect sc_orbitals pro_hydroxylated_case1 pro_hydroxylated_case2 ser_phosphorylated thr_phosphorylated  tyr_phosphorylated tyr_sulfated lys_dimethylated lys_monomethylated  lys_trimethylated lys_acetylated glu_carboxylated cys_acetylated tyr_diiodinated N_acetylated C_methylamidated MethylatedProteinCterm\n"

        arguments = fast_flags + flags

        if args.noloophash:
            arguments += "-bound\n"
            arguments += "-bound_stringent\n"
        else:
            arguments += "-lh:db_path /work/kenjung/for/possu/3to25mer\n"

        if args.soften_clash_score:
            arguments += "-soften_clash_score\n"


        flags_fn = open("flags", "w")
        flags_fn.write( arguments + "-mute all\n" )
        flags_fn.close()
        #print "writing down args"
        system( "%s @flags" % args.exe )
def run_jobs( args ):
    if exists("running.lock"): 
        stderr.write("job has already existed\n")
        return 0
    else:
        filesystem_util.touch("running.lock")
        if not exists( args.flags_fn ): 
            stderr.write("flags has not already existed\n")
            return 0
        flags = " " + " ".join( [ line.strip() for line in open( args.flags_fn, "r" ).readlines() ] )
        currdir = basename( getcwd() )
        extra_flags = " -pos %s -out:file:scorefile pos_%s_score.sc -out:file:silent pos_%s_silent.out " %( currdir, currdir, currdir )
        system( args.exe + flags + extra_flags + "-mute all" )
def run_jobs( args ):
    if exists("run.lock"):
        stderr.write("job has already existed\n")
        return 0
    else:
        filesystem_util.touch("run.lock")

        rsn = int( basename( getcwd() ) )

        assert isdir( args.input_dir ), args.input_dir
        ref_fragfiles = " ".join( glob("%s/after_rotation_frags.%s.%s.*pdb" %( args.input_dir, args.mers, rsn ) ) )
        fragfiles_list = []
        for pdb in glob("%s/after_rotation_frags.%s.*pdb" %( args.input_dir, args.mers )):
            this_rsn = int( basename(pdb).split(".")[2] )
            if this_rsn != rsn:
                fragfiles_list.append( pdb )

        fragfiles = " ".join( fragfiles_list )

        flags = " -native %s -mapfile %s -ref_fragfiles %s -fragfiles %s -out:score pos_%s_score.sc -clash_dist_threshold %s -database /work/wangyr/xtal_rosetta_database " %(
                   args.native, args.mapfile, ref_fragfiles, fragfiles, rsn, args.clash_dist )
        fast_flags = " -chemical:exclude_patches LowerDNA  UpperDNA Cterm_amidation SpecialRotamer protein_cutpoint_upper protein_cutpoint_lower VirtualBB ShoveBB VirtualDNAPhosphate VirtualNTerm CTermConnect sc_orbitals pro_hydroxylated_case1 pro_hydroxylated_case2 ser_phosphorylated thr_phosphorylated  tyr_phosphorylated tyr_sulfated lys_dimethylated lys_monomethylated  lys_trimethylated lys_acetylated glu_carboxylated cys_acetylated tyr_diiodinated N_acetylated C_methylamidated MethylatedProteinCterm "

        arguments = fast_flags + flags

        if args.noloophash:
            arguments += " -bound "
        else:
            arguments += " -lh:db_path /work/kenjung/for/possu/3to25mer "

        if args.soften_clash_score:
            arguments += " -soften_clash_score  "


        if args.go_ahead:
            flags_fn = open("flags", "w")
            flags_fn.write( args.exe + arguments + " -mute all " )
            flags_fn.close()
            system( "sh flags" )
        else:
            flags_fn = open("flags", "w")
            flags_fn.write( arguments + " -mute all " )
            flags_fn.close()
            print "writing down args"
def run_jobs(args):
    filesystem_util.touch("extract.lock")

    rsn = int(basename(getcwd()))
    tags = " ".join(
        [
            line.split()[-1].split(".pdb")[0]
            for line in open(args.clustered_results, "r").readlines()
            if float(line.split()[0]) == rsn
        ]
    )
    if not tags:
        stderr.write("ERROR: no tags available for this target\n")
        exit()

    # silent_fn = "pos_%s_silent_packed_c3_80percent_cut.out" % rsn
    # silent_fn = "pos_%s_silent_packed_c3_100percent_cut.out" % rsn
    matched_files = glob("pos_%s_%s*" % (rsn, args.silent_fn_pattern))
    assert len(matched_files) == 1, matched_files
    silent_fn = matched_files[0]
    fast_flags = " -chemical:exclude_patches LowerDNA  UpperDNA Cterm_amidation SpecialRotamer protein_cutpoint_upper protein_cutpoint_lower VirtualBB ShoveBB VirtualDNAPhosphate VirtualNTerm CTermConnect sc_orbitals pro_hydroxylated_case1 pro_hydroxylated_case2 ser_phosphorylated thr_phosphorylated  tyr_phosphorylated tyr_sulfated lys_dimethylated lys_monomethylated  lys_trimethylated lys_acetylated glu_carboxylated cys_acetylated tyr_diiodinated N_acetylated C_methylamidated MethylatedProteinCterm "

    # arguments = fast_flags + "-mute all -out:file:residue_type_set centroid -in:file:silent %s -in:file:silent_struct_type binary -in:file:tags %s" %( silent_fn, tags )
    arguments = fast_flags + "-mute all -in:file:silent %s -in:file:silent_struct_type binary -in:file:tags %s" % (
        silent_fn,
        tags,
    )

    if args.go_ahead:
        system(args.exe + arguments)
        assert len(glob("after_rotation_frags.*.*.*.*.????.pdb")) != 0
        # shutil.move("after_rotation_frags*pdb", args.outfrags_dir )
        system("mv after_rotation_frags*pdb %s" % args.outfrags_dir)

    else:
        print(args.exe + arguments)
    parser.add_argument("-a", "--fastaA", required=True )
    parser.add_argument("-r", "--closabscore_gapsize", default=15, type=int )
    parser.add_argument("-m", "--mer", default=9 )
    parser.add_argument("--lockfile", default="junction_closabscore_cleaner.lock" )
    args = parser.parse_args()


    #assert not exists( args.lockfile ), args.lockfile
    if exists( args.lockfile ): exit()

    scorefile = "pos_%s_score.sc" % basename( getcwd() )

    assert exists( scorefile ), scorefile 
    assert exists( args.fastaA ), args.fastaA

    filesystem_util.touch( args.lockfile )

    rsds_to_clean = is_within_junction( args, scorefile )

    if not rsds_to_clean:
        exit()
    print scorefile, rsds_to_clean


    # if not
    outlines = cleaner( scorefile, rsds_to_clean )

    clean_outfile = scorefile + ".clean"
    out = open( clean_outfile, "w" )
    out.write( outlines )
    out.close()