tabelike <- read.table("input.txt", sep = "\t", header = FALSE)
attach(tabelike)

# PDF file
pdf("output.pdf", width = 7, height = 3)

# Bottom part: main plot
par(fig = c(0, 1, 0, 0.85), mar = c(5.2, 4, 0, 2)) 

# PVY-EU-N (rename this if using other non-recombinant strains; 
# also update the label in the legend accordingly)
plot(V1, V4 / (V3 + V4) * 100, type = "p", col = "darkorange1",
     ylab = "percentage", xlab = "PVY position",
     xlim = c(0, max(V1)), ylim = c(0, 100))

# PVY-O
points(V1, V3 / (V3 + V4) * 100, col = "cornflowerblue", pch = 16, cex = 0.5)

# Vertical lines
abline(v = c(502, 2395, 2419, 5834, 5857, 6714, 8607, 9193))

# Legend
legend("bottom", inset = c(0, -0.75), xpd = TRUE,
       legend = c("PVY-EU-N", "PVY-O"),
       col = c("darkorange1", "cornflowerblue"),
       pch = c(1, 16), pt.cex = c(1, 0.5), horiz = TRUE, bty = "n", cex = 1)

# Top genome schematic
par(fig = c(0, 1, 0.8, 1), new = TRUE, mar = c(0, 4, 0, 2))  
plot(NA, xlim = c(0, max(V1)), ylim = c(0, 1), axes = FALSE, xlab = "", ylab = "")

# Genome boxes
gene_positions <- data.frame(
  gene = c("P1", "HC-Pro", "P3", "6K1", "CI", "6K2", "VPg", "NIa", "NIb", "CP"),
  start = c(165, 1017, 2385, 3480, 3636, 5538, 5694, 6258, 6990, 8553),
  end   = c(1017, 2385, 3480, 3636, 5538, 5694, 6258, 6990, 8553, 9354)
)

for (i in 1:nrow(gene_positions)) {
  rect(gene_positions$start[i], 0.4,
       gene_positions$end[i], 0.6,
       col = "grey80", border = "grey30")
  text(x = (gene_positions$start[i] + gene_positions$end[i]) / 2,
       y = 0.75, labels = gene_positions$gene[i], cex = 0.6)
}

# 5′ and 3′ ends
text(0, 0.5, expression(5*minute), pos = 2, cex = 0.8)
text(max(V1), 0.5, expression(3*minute), pos = 4, cex = 0.8)

# Connecting line between 5′ and start of genome
segments(x0 = 0, y0 = 0.5, x1 = gene_positions$start[1], y1 = 0.5, lwd = 1)

# Connecting line between 3′ and end of genome
segments(x0 = max(V1), y0 = 0.5, x1 = gene_positions$end[nrow(gene_positions)], y1 = 0.5, lwd = 1)

# Label
par(fig = c(0, 1, 0, 1), new = TRUE, mar = c(0, 0, 0, 0))
plot(NA, xlim = c(0, 1), ylim = c(0, 1), type = "n", axes = FALSE, xaxs = "i", yaxs = "i", xlab = "", ylab = "")

text(x = 0.01, y = 0.95, labels = "A", cex = 1.2, font = 1, adj = c(0, 1))

dev.off()