Skip to content

Instantly share code, notes, and snippets.

@yuifu
Created January 18, 2016 04:58
Show Gist options
  • Save yuifu/e0a1560ad20df363e20c to your computer and use it in GitHub Desktop.
Save yuifu/e0a1560ad20df363e20c to your computer and use it in GitHub Desktop.
path_gtf = "merged.asm/merged.gtf"
path_ofile = "merged.asm/merged.gtf.txt"
arr_attr = Array{Any,1}()
d_attr = Dict{Any,Any}()
# Extract all attributes
f = open(path_gtf)
for ln in eachline(f)
for s in split(split(chomp(ln), "\t")[9], "; ")
if ! in(split(s, " ")[1], arr_attr)
push!(arr_attr, split(s, " ")[1])
end
end
end
close(f)
for s in arr_attr
println(s)
println(typeof(s))
end
# Convert Colmun 9 into columns
fw = open(path_ofile, "w")
write(fw, join(["chr"; "V2"; "V3"; "start"; "end"; "V6"; "strand"; "V8"; arr_attr], "\t") * "\n")
f = open(path_gtf)
for ln in eachline(f)
empty!(d_attr)
for s in split(split(chomp(ln), "\t")[9], "; ")
d_attr[split(s, " ")[1]] = replace(split(s, " ")[2], r"[\";]", s"")
end
res = join(split(chomp(ln), "\t")[1:8], "\t")
for s in arr_attr
if in(s, keys(d_attr))
res *= "\t" * d_attr[s]
else
res *= "\t" * "."
end
end
write(fw, res * "\n")
end
close(f)
close(fw)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment