toEmbl.awk 3.95 KB
Newer Older
alain viari committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#
# iff -> embl
#

function FromLoc(i, _local_, s) {
  s = substr(Exon[i]["modif"], 1, 1) "" Exon[i]["from"]
  gsub("=", "", s)
  return s
}

function ToLoc(i, _local_, s) {
  s = substr(Exon[i]["modif"], 2, 1) "" Exon[i]["to"]
  gsub("=", "", s)
  return s
}

function GeneLocation(_local_, d, s) {
  d = Exon[1]["strand"]
  if (d == "+")
      s = FromLoc(1) ".." ToLoc(Nexon)
  else
      s = FromLoc(Nexon) ".." ToLoc(1)
  if (d == "-") s = "complement(" s ")"
  return s
}

function CdsLocation(_local_, d, i, s) {
  d = Exon[1]["strand"]
  if (d == "+") {
    for (i = 1 ; i <= Nexon ; i++)
      s = s "," FromLoc(i) ".." ToLoc(i)
  }
  else {
    for (i = Nexon ; i >= 1 ; i--)
      s = s "," FromLoc(i) ".." ToLoc(i)
  }
  s = substr(s, 2)
  if (Nexon > 1) s = "join(" s ")"
  if (d == "-") s = "complement(" s ")"
  return s
}

function ExonLocation(i, _local_, s) {
  s = FromLoc(i) ".." ToLoc(i)
  if (Exon[i]["strand"] == "-") s = "complement(" s ")"
  return s
}

function Pad(s, len) {
  while (length(s) < len)
    s = s " "
  return s
}

function Feature(feat, loc, _local_, s) {
  s = Pad("FT   " feat, 21)
  print s "" loc
}

function SQualifier(qual, val, _local_, s) {
  s = Pad("FT", 21)
  print s "/" qual "=" val
}

function QQualifier(qual, val) {
66
  gsub("\"", "''", val)
alain viari committed
67 68 69
  SQualifier(qual, "\"" val "\"")
}

Alain Viari committed
70 71 72 73
function Unk(s) {
  return (s =="" ? "none" : s)
}

alain viari committed
74 75 76 77
#
# rules
#

Alain Viari committed
78 79 80 81 82 83
/^c pass/ {
  PassType = $3
  PassInfo = $NF
  next
}

alain viari committed
84 85 86 87 88 89 90 91 92 93 94 95 96
/^c annot/ {
  GeneName = $4
  Product = $NF
  gsub("_", " ", Product)
  next
}

/^c nclust/ {
  Ngene = $3
  next
}

/^c begin_entry/ {
97
	Nexon = 0
98
	FrameShift=0
99 100
	delete Exon
	next
alain viari committed
101 102 103 104 105 106 107
}

/^e exon/ {
  Nexon++
  Exon[Nexon]["from"]   = $3
  Exon[Nexon]["to"]     = $4
  Exon[Nexon]["strand"] = $6
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	match($0, / insertions +([0-9]+) +/, arr)
	if (arr[1])
		insertions=arr[1]
	else
		insertions=0
		
	match($0, / insertions +([0-9]+) +/, arr)
	if (arr[1])
		deletions=arr[1]
	else
		deletions=0
				
	Exon[Nexon]["indels"] = insertions "+" $deletions
		
	match($0, / modifier +"([^"]*)"/, arr)
	if (arr[1])
		modif=substr(arr[1],1,1)
	else
		modif=""
	if (modif == "=")
		modif=""
		
	Exon[Nexon]["modif"]  = modif

	match($0, / frameshifts +([-0-9]+)/, arr)
	if (arr[1]) {
		FrameShift=FrameShift+1
		Exon[Nexon]["frameshift"] = arr[1]
	}
	else
		Exon[Nexon]["frameshift"] = 0
	
alain viari committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  next
}

/^e similarity/ {
  split($12, a, "@")
  Simil = a[1] ":" a[2]
  next
}

/^e translate/ {
  Translat = $3
  next
}

/^c end_entry/ {

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
	GeneName = Unk(GeneName)
	PassType = Unk(PassType)
	  
	gname = (Ngene == 1 ? GeneName : GeneName "_" ++Igene)
	locus = ""
	  
	Feature("gene", GeneLocation())
	QQualifier("gene", gname)
	QQualifier("locus_tag", locus)
	if (FrameShift)
		QQualifier("pseudogene","unknown")
		  
	Feature("CDS", CdsLocation())
	SQualifier("codon_start", 1)
	SQualifier("transl_table", 11)
	QQualifier("gene", gname)
	QQualifier("locus_tag", locus)
174
	if (FrameShift) {
175
		QQualifier("pseudogene","unknown")
176 177 178 179 180
		if (FrameShift > 1)
			QQualifier("note","nonfunctional due to frameshifts in "FrameShift" exons")
		else
			QQualifier("note","nonfunctional due to a frameshift")
		}
181 182 183
	QQualifier("product", Product)
	QQualifier("inference", "similar to DNA sequence:" Simil)
	QQualifier("inference", "org.annot -- detect pass:" PassType ":" PassInfo)
184 185 186 187
	if (match(Translat,/\*/)>0) {
		QQualifier("pseudogene","unknown")
		QQualifier("note","nonfunctional due to stop codon")
	}
188 189 190 191 192 193 194 195
	if (FrameShift==0)
		QQualifier("translation", Translat)
	  
	if (Nexon > 1) {
		for (i = 1 ; i <= Nexon ; i++) {
	    	Feature("exon", ExonLocation(i))
	    	QQualifier("gene", gname)
	    	QQualifier("locus_tag", locus)
196 197 198 199 200 201 202 203 204
	    	
	    	if (Exon[i]["frameshift"]) {
				QQualifier("pseudogene","unknown")
				if (Exon[i]["frameshift"] > 0)
					QQualifier("note","frameshifted by insertion of " Exon[i]["frameshift"] " bp")
				else
					QQualifier("note","frameshifted by deletion of " -Exon[i]["frameshift"] " bp")
			}
	    			    		
205 206 207
	    	SQualifier("number", Exon[1]["strand"] == "+" ? i : Nexon-i+1)
	    }
	  }
alain viari committed
208 209 210
}