Writes a pandas dataframe with the respective SAM columns: 'QNAME','FLAG','RNAME','POS','MAPQ','CIGAR','RNEXT','PNEXT','TLEN','SEQ','QUAL' into a sam file

    :param sam: pandas dataframe to be writen
    :param SAMfile: /path/to/file.sam

    :returns: nothing

def writeSAM(sam,SAMfile,header=None):
    """
    Writes a pandas dataframe with the respective SAM columns: 'QNAME','FLAG','RNAME','POS','MAPQ','CIGAR','RNEXT','PNEXT','TLEN','SEQ','QUAL' into a sam file

    :param sam: pandas dataframe to be writen
    :param SAMfile: /path/to/file.sam

    :returns: nothing
    """
    def toNone(x):
        if x=="None":
            x=np.nan
        return x

    sam.reset_index(inplace=True,drop=True)
    QUAL=pd.DataFrame(sam['QUAL'].str.split("\t").tolist())
    cols=QUAL.columns.tolist()

    for c in cols:
        QUAL[c]=QUAL[c].apply(lambda x: toNone(x))

    sam=sam.drop(['QUAL'],axis=1)
    sam=pd.concat([sam,QUAL],axis=1)
    sam=sam.astype(str)
    sam=sam.as_matrix()

    tfile=open(SAMfile, "w+")

    if header != None:
        for l in header:
            tfile.write(l)

    for l in sam:
        l=[ s for s in l if s not in ['nan'] ]
        l="\t".join(l)
        tfile.write(l+"\n")

    tfile.close()