Let $X$ be an affine scheme with an open affine subscheme $U\subset X$. Given an automorphism of $U$, we can glue $X$ with itself along $U$ to get a new scheme. Is there a description in terms of commutative algebra of automorphisms such that the resulting scheme is affine/separated? 


If $U=\mathrm{Spec}\:B$, $X=\mathrm{Spec}\:A$, then $B$ is an $A$-algebra of finite presentation so there's a chance to be explicit.


For example, if $X=\mathrm{Spec}\:k[x]$ and $U=\mathrm{Spec}\:k[x, \frac{1}{x}]$ if we take the identity on $U$ the result is non-separated and if we take $x\rightarrow \frac{1}{x}$ the result is separated.


I'm especially interested in what happens for $X$ the spectrum of a discrete valuation ring or a PID.


[Here](https://mathoverflow.net/a/20792/142965) is a commutative algebra description of open immersions between affine schemes (not a very convenient one I'd guess).



When does glueing affine schemes produce affine/separated schemes?
Here are some thoughts in the case of gluing a DVR along an automorphism of its fraction field:


**Setup**: Let $A$ be a DVR with uniformizer $\pi$ and fraction field $K$, and let $\varphi : K \to K$ be a ring automorphism. Let $S$ be the gluing of two copies of $\operatorname{Spec} A$ along the automorphism of $\operatorname{Spec} K$ corresponding to $\varphi$.


**On separatedness**: I claim that $S$ is separated if and only if $\varphi(A) \not\subseteq A$. By [Tag 01KP](https://stacks.math.columbia.edu/tag/01KP) we have that $S$ is separated if and only if the ring map \begin{align} \mu\_{\varphi} : A \otimes\_{\mathbb{Z}} A \to K \end{align} sending $a\_{1} \otimes a\_{2} \mapsto a\_{1} \cdot \varphi(a\_{2})$ is surjective. If $\varphi(A) \not\subseteq A$, then $\mu\_{\varphi}$ is surjective since the image of $\mu\_{\varphi}$ is a subring of $K$ strictly larger than $A$. On the other hand, if $\varphi(A) \subseteq A$ then the image of $\mu\_{\varphi}$ is $A$.


**On affineness**: *EDIT: user "m.mor" proves in [this answer](https://mathoverflow.net/a/337442/) that if $S$ is separated then it is affine.* I don't have a complete answer. We may consider using Serre's criterion (e.g. [Tag 01XF](https://stacks.math.columbia.edu/tag/01XF)). Let $\mathcal{I} \subseteq \mathcal{O}\_{S}$ be an ideal sheaf of $S$. By the Mayer-Vietoris sequence, we have that $\mathrm{H}^{1}(S,\mathcal{I}) = 0$ if and only if for all $e\_{1},e\_{2} \in \mathbb{Z}\_{\ge 0}$ the addition map \begin{align} \alpha\_{e\_{1},e\_{2},\varphi} : \pi^{e\_{1}}A \oplus \pi^{e\_{2}}A \to K \end{align} sending $(a\_{1},a\_{2}) \mapsto a\_{1} + \varphi(a\_{2})$ is surjective. Thus certainly if $\varphi(A) \subseteq A$ then $S$ is not affine. Here I tried some examples (e.g. $A = k[x]\_{(x)}$ and the automorphism of $k(x)$ sends $x \mapsto \frac{1}{x}$, or $A = k[x,y]\_{(x)}$ and the automorphism of $k(x,y)$ switches $x,y$) and the resulting glued schemes were affine (these examples generalize to Lemma 1 below). Another strategy would be to view everything with the $\pi$-adic topology and try to use Lemma 2 below, but I don't know whether $\varphi(A)$ is a dense subgroup of $K$.


**Lemma 1**: Let $A$ be a Dedekind domain with fraction field $K$. Suppose that $A$ has exactly two maximal ideals $\mathfrak{p}\_{1},\mathfrak{p}\_{2}$. Then for any $s\_{1},s\_{2} \in \mathbb{Z}\_{\ge 0}$ the addition map \begin{align} \mathfrak{p}\_{1}^{s\_{1}}A\_{\mathfrak{p}\_{1}} \oplus \mathfrak{p}\_{2}^{s\_{2}}A\_{\mathfrak{p}\_{2}} \to K \end{align} sending $(a\_{1},a\_{2}) \mapsto a\_{1}+a\_{2}$ is surjective.


*Proof*: Since $A$ has finitely many maximal ideals, it is a PID. Let $\pi\_{i}$ be a generator of $\mathfrak{p}\_{i}$; then $K := A[(\pi\_{1}\pi\_{2})^{-1}]$ as $A$-algebras. Moreover $\pi\_{1}^{e\_{1}}-\pi\_{2}^{e\_{2}}$ is a unit for all $e\_{1},e\_{2} \ge 1$. As an $A$-module, $K$ is generated by the elements $1/\pi\_{1}^{e\_{1}}\pi\_{2}^{e\_{2}}$ for all $e\_{1},e\_{2} \ge 0$. We may assume that $e\_{1},e\_{2} \ge 1$. Then \begin{align} \textstyle \frac{1}{\pi\_{1}^{e\_{1}}\pi\_{2}^{e\_{2}}} = \pi\_{1}^{s\_{1}} \cdot (\frac{1}{\pi\_{1}^{s\_{1}+e\_{1}}-\pi\_{2}^{s\_{2}+e\_{2}}}) \frac{1}{\pi\_{2}^{e\_{2}}} + \pi\_{2}^{s\_{2}} \cdot (-\frac{1}{\pi\_{1}^{s\_{1}+e\_{1}}-\pi\_{2}^{s\_{2}+e\_{2}}}) \frac{1}{\pi\_{1}^{e\_{1}}} \end{align} so we have the desired result. 


**Lemma 2**: Let $G$ be a topological group. Let $U$ be an open subgroup of $G$ and let $H$ be a dense subgroup of $G$. Then $G = UH$.


(I heard about this fact [here](https://mathoverflow.net/a/300262/).)


