TL[S_String]:=TL@@(Characters[S]/.{"n"->Caps[1],"N"->Caps[2],"u"->Cups[1],"U"->Cups[2],"i"->Strands[1],"I"->Strands[2],d_String:>Strands[ToExpression[d]]})

TL["nuii"]

TL["anu2nub"]

FoldDown[TL[Z___,Strands[n_]]]:=TL[Z,Strands[n-1],Caps[1]]
FoldDown[TL[Z___,Cups[n_]]]:=TL[Z,Strands[1],Cups[n-1]]

FoldDown[TL[Z___,Strands[n_],Caps[m_]]]:=TL[Z,Strands[n-1],Caps[m+1]]

TL[X___,Strands[n_],Strands[m_],Y___]:=TL[X,Strands[n+m],Y]
Strands[0]:=Sequence[]
Caps[0]:=Sequence[]
Cups[0]:=Sequence[]

Reflect[t_TL]:=Reflect/@t

Reflect[Caps[n_]]:=Cups[n]
Reflect[Cups[n_]]:=Caps[n]
Reflect[Strands[n_]]:=Strands[n]

BottomWidth[TL[Z___]]:=Plus@@BottomWidth/@({Z})
TopWidth[TL[Z___]]:=Plus@@BottomWidth/@({Z})

BottomWidth[Caps[n_]]:=2n
BottomWidth[Cups[_]]:=0
BottomWidth[Strands[n_]]:=n
BottomWidth[Cups[n_]]:=2n
BottomWidth[Caps[_]]:=0
BottomWidth[Strands[n_]]:=n

Circumference[t_TL]:=BottomWidth[t]+TopWidth[t]

TLCoefficient[TL[]]:=1
TLCoefficient[TL[Strands[n_]]]:=1

The general rule :

TLCoefficient[t_TL]:=With[{w=BottomWidth[t]},Plus@@ReplaceList[FoldDown[t],TL[Z___,Caps[n_],Y___]:>With[{k=BottomWidth[TL[Z]]+n},(((-1))^(w+k))((qI[k] )/(qI[w]))Hold[TLCoefficient[TL[Z,Caps[n-1],Y]]]]]]

ReleaseHold[TLCoefficient[TL["n1u"]]]

Deal with single waist n-2 diagrams:

TLCoefficient[TL[A___Strands,Caps[1],B___Strands,Cups[1],C___Strands]]:=With[{a=TopWidth[TL[A]],b=TopWidth[TL[B]],c =TopWidth[TL[C]]},(((-1))^(b+1))((qI[a+1]qI[a+b+1])/(qI[a+b+c+1]qI[a+b+c+2]))]

TLCoefficient[TL[A___Strands,Cups[1],B___Strands,Caps[1],C___Strands]]:=With[{a=TopWidth[TL[A]],b=TopWidth[TL[B]],c =TopWidth[TL[C]]},(((-1))^(b+1))((qI[a+1]qI[a+b+1])/(qI[a+b+c+1]qI[a+b+c+2]))]

TLepi[n_,{k_,K___}]/;k>1:=TL[Strands[k-1],TLepi[n-k+1,{k,K}-(k-1)]]

TLepi[n_,{1,K___}]/;First[{K,∞}]>2:=TL[Caps[1],TLepi[n-2,{K}-2]]
TLepi[n_,{1,2,K___}]/;First[{K,∞}]>4:=TL[Caps[2],TLepi[n-4,{K}-4]]

TLepi[n_,{}]:=TL[Strands[n]]

TLmono[n_,{k_,K___}]/;k>1:=TL[Strands[k-1],TLmono[n-k+1,{k,K}-(k-1)]]

TLmono[n_,{1,K___}]/;First[{K,∞}]>2:=TL[Cups[1],TLmono[n-2,{K}-2]]
TLmono[n_,{1,2,K___}]/;First[{K,∞}]>4:=TL[Cups[2],TLmono[n-4,{K}-4]]

TLmono[n_,{}]:=TL[Strands[n]]

TLepi[5,{2,3}]

Some cases just don' t get handled

TLepi[6,{1,2,4}]

Clear[TL]

SetAttributes[TL,{Flat,OneIdentity}]

TL/:TL[Strands[a_],X__]**TL[Strands[a_],Y__]:=TL[Strands[a],TL[X]**TL[Y]]
TL/:TL[Strands[a_],X___]**TL[Strands[b_],Y___]/;Simplify[a<b]:=TL[Strands[a],TL[X]**TL[Strands[b-a],Y]]
TL/:TL[Strands[a_],X___]**TL[Strands[b_],Y___]/;Simplify[a>b]:=TL[Strands[b],TL[Strands[a-b],X]**TL[Y]]
TL/:TL[Caps[a_],X___]**TL[Y___]:=TL[Caps[a],TL[X]**TL[Y]]
TL/:TL[X___]**TL[Cups[a_],Y___]:=TL[Cups[a],TL[X]**TL[Y]]
TL/:TL[X___]**TL[Strands[_]]:=TL[X]
TL/:TL[Strands[_]]**TL[X___]:=TL[X]

TL/:TL[]**TL[]:=TL[]

TLepi[5,{2,3}]

TLmono[7,{1,3}]

TLepi[5,{2,3}]**TLmono[7,{1,3,5}]

BottomWidth[S]=0;
TopWidth[S]=2n;

TopWidth[Rainbow[n_,X_]]:=2n+TopWidth[X]
TopWidth[HalfClick[X_]]:=TopWidth[X]
TopWidth[InverseHalfClick[X_]]:=TopWidth[X]

HalfClick[InverseHalfClick[X_]]:=X
InverseHalfClick[HalfClick[X_]]:=X

HalfClick[HalfClick[X_]]/;LowestWeightVectorQ[X]:=λ[X] X

InverseHalfClick[InverseHalfClick[X_]]/;LowestWeightVectorQ[X]:=((λ[X])^(-1)) X

LowestWeightVectorQ[S]=True;

Clear[CappablePositions]

CappablePositions[X_]/;LowestWeightVectorQ[X]:={}
CappablePositions[HalfClick[X_]]/;LowestWeightVectorQ[X]:={}
CappablePositions[InverseHalfClick[X_]]/;LowestWeightVectorQ[X]:={}

CappablePositions[Rainbow[1,X_]]:={1}~Join~(1+CappablePositions[X])~Join~{TopWidth[S]+1}

CappablePositions[Rainbow[2,X_]]:={1,2}~Join~(2+CappablePositions[X])~Join~({2,3}+TopWidth[S])

CappablePositions[TL[Caps[1],X_]]:={1,2}~Join~(2+CappablePositions[X])
CappablePositions[TL[X_,Caps[1]]]:=CappablePositions[X]~Join~({0,1}+TopWidth[X])

CappablePositions[Rainbow[1,S]]

NonzeroCaps[X_]:=TL[Strands[#-1],Caps[1],Strands[TopWidth[X]-2-(#-1)]]&/@CappablePositions[X]

NonzeroCaps[Rainbow[2,S]]

Clear[TL]

TL/:Rainbow[1,X_]**TL[Caps[1],Strands[n_]]/;n==TopWidth[X]:=InverseHalfClick[X]
TL/:Rainbow[1,X_]**TL[Strands[n_],Caps[1]]/;n==TopWidth[X]:=HalfClick[X]

TL/:Rainbow[2,X_]**TL[Caps[1],Strands[n_]]/;(n==TopWidth[X]+2):=TL[X,Caps[1]]
TL/:Rainbow[2,X_]**TL[Strands[n_],Caps[1]]/;(n==TopWidth[X]+2):=TL[Caps[1],X]
TL/:Rainbow[2,X_]**TL[Strands[1],Caps[1],Strands[n_]]/;(n==TopWidth[X]+1):=Rainbow[1,InverseHalfClick[X]]
TL/:Rainbow[2,X_]**TL[Strands[n_],Caps[1],Strands[1]]/;(n==TopWidth[X]+1):=Rainbow[1,HalfClick[X]]

EpisAtDepth[X_,k_]:=Union[Flatten[(Function[{cap},{#[[1]]**cap,cap**#[[2]]}]/@NonzeroCaps[#[[1]]])&/@EpisAtDepth[X,k-1],1]]

EpisAtDepth[X_,0]:={{X,TL[Strands[TopWidth[X]]]}}

EpisAtDepth[S,0]

EpisAtDepth[S,1]

EpisAtDepth[Rainbow[1,S],1]

Epis[X_]:=Module[{k=0,last,result={}},
While[(last=EpisAtDepth[X,k++])≠{},
result=result~Join~last;
];
result
]

Epis[Rainbow[1,S]]

EpisAtDepth[Rainbow[2,S],1]

EpisAtDepth[Rainbow[2,S],2]

Simplify[2n<1+2n]

λ[S]=-1

JWInnerProduct[Rainbow[1,S],Rainbow[1,S]]

JWInnerproduct[X_,Y_]:=Module[{pX,pY},
pX=CappablePositions[X];
pY=CappablePositions[y];
{pX,pY}
]