What can cause flank/rib pain when breathing deep in the middle of the night?

Costochondritis. This could be costochondritis which is inflammation of the joints where the ribs attach to the sternum. Try an anti-inflammatory like motrin. However, it could be something more serious and certainly warrants a visit with your doctor.