using DataFrames
#using CSV

function main(pairsfile, setsfile)
    #pairsfile = ARGS[1]
    #setsfile = ARGS[2]
    dfpairs = readtable(pairsfile,header=true)
    dfsets = readtable(setsfile,header=true)

    for i = 1:size(dfsets,1)
        algo = dfsets[i,:algorithm_name]
        if startswith(algo,"PW_")
            algo = algo[4:end]
        else
            error("bad algo name?")
        end
        netset = dfsets[i,:networks_group]
        # same algo and network set
        df = dfpairs[(dfpairs[:algorithm_name].==algo) .& Vector{Bool}(map(x -> startswith(x,"$(netset)_"), dfpairs[:networks_group])),:]
        alnfile = dfsets[i,:aln_file]

        println("Writing algorithm_name = $algo, networks_group = $netset to alignment file $alnfile")

        mkpath(dirname(alnfile))
        open(alnfile,"w") do fdout
            for j = 1:size(df,1)
                open(string(df[j,:aln_file]), "r") do fdin
                    for line in eachline(fdin)
                        println(fdout,line)
                    end
                    if j < size(df,1) println(fdout,"#--#") end
                end
            end
        end
    end
end

main(ARGS[1], ARGS[2])
