Provide a specific argument to a specific function from a series of functions from command line

Hello everybody.

I would like to ask you: how to enter the argument(s) for only one specific function of the many ones I have in my script file from command line?

In more details: I am working on a training file, where I am writing many different functions, just to train myself…
In one of the function (which translates dna into proteins) I would like to provide from the command line the dna sequence as function argument directly or even provide the file text directly (but I haven’t worked with files yet.

here my function:

function translator_from_rna(rna_seq :: AbstractString)
    traslation = []
    table = Dict(
        "AUA"::"I", "AUC"::"I", "AUU"::"I", "AUG"::"M", 
        "ACA"::"U", "ACC"::"T", "ACG"::"T", "ACU"::"T", 
        "AAC"::"N", "AAU"::"N", "AAA"::"K", "AAG"::"K", 
        "AGC"::"S", "AGU"::"S", "AGA"::"R", "AGG"::"R",                  
        "CUA"::"L", "CUC"::"L", "CUG"::"L", "CUU"::"L", 
        "CCA"::"P", "CCC"::"P", "CCG"::"P", "CCU"::"P", 
        "CAC"::"H", "CAU"::"H", "CAA"::"Q", "CAG"::"Q", 
        "CGA"::"R", "CGC"::"R", "CGG"::"R", "CGU"::"R", 
        "GUA"::"V", "GUC"::"V", "GUG"::"V", "GUU"::"V", 
        "GCA"::"A", "GCC"::"A", "GCG"::"A", "GCU"::"A", 
        "GAC"::"D", "GAU"::"D", "GAA"::"E", "GAG"::"E", 
        "GGA"::"G", "GGC"::"G", "GGG"::"G", "GGU"::"G", 
        "UCA"::"S", "UCC"::"S", "UCG"::"S", "UCU"::"S", 
        "UUC"::"F", "UUU"::"F", "UUA"::"L", "UUG"::"L", 
        "UAC"::"Y", "UAU"::"Y", "UAA"::"_", "UAG"::"_", 
        "UGC"::"C", "UGU"::"C", "UGA"::"_", "UGG"::"W" )
    
    start_translation = findfirst("AUG", uppercase(rna_seq))[2]
    triplets = 2
    for (num, sequence) in enumerate(start_translation:length(rna_seq))
        if num % 3 == 0 && num < length(rna_seq)
            triplents = rna_seq[sequence:sequence + triplets]
            println(triplents)
            append!(traslation, table[triplents])
        end
    end
    return join(traslation, "")
end

translator_from_rna("UUUAUGGCCAUGGCGCCCAGAACUGAGAUCAAUAGUACCCGUAUUAACGGGUGAUUCACAUGUAAA")

Thank you!

The variables ARGS has the command line arguments. You can do something like

length(ARGS) == 1 || error("I need one command line argument")

translator_from_rna(ARGS[1])

(Julia 1.4 will have only which simplifies assertions like this)

Thank you. I am having troubles with your solution.
Did I implemented it right?


function translator_from_rna_v2((ARGS[1]) :: AbstractString)
    """This function provided a RNA sequence will generate the protein sequence starting from the first encountered AUG.
    However, in contrast to the previous function this one takes the function argument from the command line"""
    
    #This will throw an error in case the lenght of the argument is not correct
    length(ARGS) == 1 || error("I need one command line argument")
    traslation = []
    table = Dict(
        "AUA"::"I", "AUC"::"I", "AUU"::"I", "AUG"::"M", 
        "ACA"::"U", "ACC"::"T", "ACG"::"T", "ACU"::"T", 
        "AAC"::"N", "AAU"::"N", "AAA"::"K", "AAG"::"K", 
        "AGC"::"S", "AGU"::"S", "AGA"::"R", "AGG"::"R",                  
        "CUA"::"L", "CUC"::"L", "CUG"::"L", "CUU"::"L", 
        "CCA"::"P", "CCC"::"P", "CCG"::"P", "CCU"::"P", 
        "CAC"::"H", "CAU"::"H", "CAA"::"Q", "CAG"::"Q", 
        "CGA"::"R", "CGC"::"R", "CGG"::"R", "CGU"::"R", 
        "GUA"::"V", "GUC"::"V", "GUG"::"V", "GUU"::"V", 
        "GCA"::"A", "GCC"::"A", "GCG"::"A", "GCU"::"A", 
        "GAC"::"D", "GAU"::"D", "GAA"::"E", "GAG"::"E", 
        "GGA"::"G", "GGC"::"G", "GGG"::"G", "GGU"::"G", 
        "UCA"::"S", "UCC"::"S", "UCG"::"S", "UCU"::"S", 
        "UUC"::"F", "UUU"::"F", "UUA"::"L", "UUG"::"L", 
        "UAC"::"Y", "UAU"::"Y", "UAA"::"_", "UAG"::"_", 
        "UGC"::"C", "UGU"::"C", "UGA"::"_", "UGG"::"W" )
    
    start_translation = findfirst("AUG", uppercase(rna_seq))[2]
    triplets = 2
    for (num, sequence) in enumerate(start_translation:length(rna_seq))
        if num % 3 == 0 && num < length(rna_seq)
            triplents = rna_seq[sequence:sequence + triplets]
            println(triplents)
            append!(traslation, table[triplents])
        end
    end
    return join(traslation, "")
end

I also tried to write the function like this:


function translator_from_rna_v2(ARGS :: AbstractString)
    """This function provided a RNA sequence will generate the protein sequence starting from the first encountered AUG.
    However, in contrast to the previous function this one takes the function argument from the command line"""
    
    traslation = []
    table = Dict(
        "AUA"::"I", "AUC"::"I", "AUU"::"I", "AUG"::"M", 
        "ACA"::"U", "ACC"::"T", "ACG"::"T", "ACU"::"T", 
        "AAC"::"N", "AAU"::"N", "AAA"::"K", "AAG"::"K", 
        "AGC"::"S", "AGU"::"S", "AGA"::"R", "AGG"::"R",                  
        "CUA"::"L", "CUC"::"L", "CUG"::"L", "CUU"::"L", 
        "CCA"::"P", "CCC"::"P", "CCG"::"P", "CCU"::"P", 
        "CAC"::"H", "CAU"::"H", "CAA"::"Q", "CAG"::"Q", 
        "CGA"::"R", "CGC"::"R", "CGG"::"R", "CGU"::"R", 
        "GUA"::"V", "GUC"::"V", "GUG"::"V", "GUU"::"V", 
        "GCA"::"A", "GCC"::"A", "GCG"::"A", "GCU"::"A", 
        "GAC"::"D", "GAU"::"D", "GAA"::"E", "GAG"::"E", 
        "GGA"::"G", "GGC"::"G", "GGG"::"G", "GGU"::"G", 
        "UCA"::"S", "UCC"::"S", "UCG"::"S", "UCU"::"S", 
        "UUC"::"F", "UUU"::"F", "UUA"::"L", "UUG"::"L", 
        "UAC"::"Y", "UAU"::"Y", "UAA"::"_", "UAG"::"_", 
        "UGC"::"C", "UGU"::"C", "UGA"::"_", "UGG"::"W" )
    
    start_translation = findfirst("AUG", uppercase(rna_seq))[2]
    triplets = 2
    for (num, sequence) in enumerate(start_translation:length(rna_seq))
        if num % 3 == 0 && num < length(rna_seq)
            triplents = rna_seq[sequence:sequence + triplets]
            println(triplents)
            append!(traslation, table[triplents])
        end
    end
    return join(traslation, "")
end

#This will throw an error in case the lenght of the argument is not correct
length(ARGS) == 1 || error("I need one command line argument")
translator_from_rna_v2(ARGS[1])

This particular part should work, but I don’t know about the rest of the code, or how you are calling it from the command line. Eg

$ cat /tmp/tmp.jl
length(ARGS) == 1 || error("I need one command line argument")
println(ARGS[1])
$ julia /tmp/tmp.jl "something"
something

I also tried to keep the same function argument rna_seq and call the function from the ARGS argument that you suggested, but it does not work.

``ERROR: LoadError: TypeError: in typeassert, expected Type, got String
Stacktrace:```

Do you have some suggestions on how can I solve this?

Not without more information: a self-contained example that I can run, including how you call it from the command line, and the exact error message you get. Please read

1 Like

I think it has to do with your dictionary inside the function.

Dict("A" :: "B") is NOT the correct syntax. You should use Dict("A" => "B") instead. When you are trying to fix a problem it is better in isolation, this way other unrelated errors do not introduce noise.

3 Likes