Statistical thermodynamics of self-organization in the adaptive immune system