These are dark days indeed for Sir Paul McCartney. But after the anguish of seeing his wife's allegations filling newspapers day after day as the "divorce battle of the century" plunges new depths of viciousness, tonight will see him pour out his heart in music, with the assistance of choirboys from Oxford.

Eight years ago, McCartney was invited by Anthony Smith, the then president of Magdalen College, Oxford, to compose a piece to set the seal on a new concert hall for the college.

The deadline came and went. But it developed into something of a musical journey that enabled the former Beatle to recover from the death of wife Linda.

McCartney spent more than eight years working on Ecce Cor Meum (Latin for behold my heart), a timespan equivalent to the entire recording career of The Beatles.

And tonight the classical piece will be performed publicly for the first time in full at London's Royal Albert Hall, with the boys of Magdalen College Choir, Oxford, and King's College, Cambridge, for once joining forces.

McCartney himself is expected to be there for an evening that may well provide solace as one of the most painful episodes of his life unfolds.

Many believe that the classical piece that he produced for Magdalen is his tribute to his first wife. But it is also the product of an unlikely musical partnership between the former pupil of the Liverpool Institute, who never learnt to read music, and an Oxford college.

McCartney's link with Magdalen began when he travelled to Oxford with Linda, a few months before her death from cancer in 1998, to hear the famous choir sing in the college chapel.

Tony Smith's somewhat challenging request to write "a choral piece which could be sung by young people the world over in the same way that Handel's Messiah is" initially took McCartney by surprise. But he soon began to see it as an irresistible musical challenge.

"The president basically wanted to inaugurate a new building and wanted this piece ready for that," McCartney recalls. "But as time went on, I said, 'I'm not sure that I'll be ready for that'. It took longer than I thought.

"I was very excited by the idea. Linda and I went up there and stayed at the college. We went to the chapel. We heard the choir sing - the work that the choir were doing was very interesting, and a lot of it I had never heard before. Harmonically, it was all very interesting. I thought, 'wow'. It showed me the palette of where you could go and that was how it started."

The fact he had never had a lesson in composition or notation was not to deter him.

"Writing a choral piece was a huge learning curve for me because my experience is basically doing harmonies with The Beatles. The idea of formally structuring something like this and making it work was a huge undertaking.

"I was doing it completely raw so logistically sorting out how to go about it was a big task. But my theory was that Magdalen wouldn't have asked me to do it if they didn't think I could. Also I thought about why they had asked me instead of all the other perfectly good choral composers around, and I figured that it was because they must have wanted something different."

McCartney was certainly made to feel among friends at Magdalen, especially on being introduced to Magdalen's director of music, Bill Ives, who was to be supportive throughout the long project.

Any initial unease McCartney might have felt on meeting Magdalen's Informator Choristarum vanished when he immediately recognised Mr Ives. "You were in the King Singers," McCartney told him. "You sang the Frog Chorus on We All Stand Together."

As a lifelong fan of the Fab Four, Mr Ives was always happy to make himself available and still chuckles at the strangeness of hearing the voice of a Beatle on his answerphone.

"I did not need to offer advice as such," said Mr Ives. "He would contact me when he wanted to know about the range of voices in the choir, for example."

McCartney said: "I get so excited about being offered a project that I don't think, 'do I know how to do this?', which most people would think.

"I got off to a good start and was getting quite a bit of speed on. Then about a year or so into it, Linda passed away, which immediately held things up. Consequently, I lost all momentum that I had gained in that first year and had to slowly start putting it back together.

"One of the ways that I did this was to just sort of write my sadness out. There is a lament in the middle called Interlude, which was very specifically grieving over Linda. I remember playing it to someone and they started welling up, which was great because I hadn't told them that it was anything to do with Linda. But something in the chords communicated itself to this person.

"My colleague and I remember actually just sitting at the keyboard, just weeping whilst doing this piece, so it does it to me every time.

"It was very emotional - a very sad time for me.

He was to be hugely touched when Magdalen contacted him to seek his permission to include Linda among those remembered at Magdalen's All Soul's Day service, the date of remembrance in the Christian calendar when the choir performs a requiem. McCartney attended the service, unnoticed but for a handful of people who saw him quietly entering at the back of the chapel shortly after service had begun.

He does not attempt to underplay the technical difficulties he faced when he decided to return to Ecce Cor Meum. "Being a complete innocent in the field, I found out fairly late on that most people start by finding a text and setting it to music."

The title was to come when he was sitting in a church during a visit to New York, where he had been asked to undertake a narration for a friend. "I was looking around and I saw a crucifixion. Underneath it said 'Ecce Cor Meum', and I worked out, by dredging my mind for the Latin that I had learnt at school, that it meant 'behold my heart'.

"I remember back in the 60s when I had written Eleanor Rigby and I was very excited by the idea that this wasn't the band. It was actually string players. I remember thinking, 'what am I gonna do when I am 30?'. I had this image of myself in a tweed jacket, with patches on the elbows and a pencil and some manuscript paper and I thought, 'that's what I will do when The Beatles run out'. In fact I did not do it until much later. But I did it in the end."

Mr Ives recalls hearing the piece for the first time. "When he came to let me hear what he had on tape, he was certainly a little nervous and apprehensive. It was a strange situation, being with the great man who has written so much wonderful music, and being shown his piece in trepidation about what I think of it."

Exactly five years ago an early version of the work for chorus and orchestra was performed in Oxford, when it emerged that the piece simply demanded too much from the Magdalen choir. The tearful solo treble, Michael Hickman, was unable to come on in the second half d=3,3,1McCartney remembers: "I had a preview at the Sheldonian where we just banged up pretty much where I was up to at that point. I realised that I was overworking the boys. A really experienced choral writer would realise that young boy trebles can't be given huge sustained passages because they just haven't got the energy and stamina."

But Magdalen was impressed that McCartney, newly married and embarking on a series of world tours, willingly knuckled down to reorganise and restructure the piece.

The choir turnover means it was an entirely different set of Magdalen boys who gathered at Abbey Road Studios to record Ecce Cor Meum earlier in the year.

Mr Ives was delighted by the trouble McCartney took over the boys. On hearing that it was Conor Diamond's 12th birthday, he led the singing on Happy Birthday. When discord broke out between the Magdalen and King's youngsters, he teased them that the whole idea was to bring Oxford and Cambridge together.

Earlier he had spoken to them about his lifelong love of choir music, explaining that he had once had a voice trial at Liverpool Cathedral. If he had been accepted, The Beatles would never have happened, he told them.

Mr Ives says he finds the piece tuneful, honest and unpretentious. "I think Linda's presence is behind the piece. Her loss is suffused throughout it. But it is not a sacred piece. It really is his philosophy of life and how important love is."

Former Magdalen president Anthony Smith has no regrets about his initial impulse. "It could be the most important idea I've ever had," he says. "It would be nice to think of it as an evolutionary step in English choral tradition."

And it would be good to think in the Royal Albert Hall tonight choir boys from Oxford will repay a favour to a man whose life has become far from heavenly of late.