Skip to content
Wang Yunfei edited this page Feb 9, 2017 · 1 revision

stringFile

stringFile is used to wrap a string as a file ojbect.

Write to stringFile

from ngslib import stringFile
mstr = ''
with stringFile() as ofh:
    print >>ofh, "First line.\nSecond line."
    mstr = ofh.getvalue() # save stringFile content to a sting object.
print mstr

Output:

First line.
Second line.

Read from stringFile

with stringFile(mstr) as ifh:
    for line in ifh:
        print line.rstrip()

Output:

First line.
Second line.

stringFile is acceptable as IO.BioReader input

from ngslib import IO,stringFile
mstr=">act-1\\nACGATCGTGCATTACGTCGATCGTAGCATGCTAGCTGCGTACGTAGCTTGCTA\\n>egl-1\\nCAGTCTAGCATCGTACGATCGTGCTACGTACGTATCGTACGATCGTACGTAGCT"
for fa in IO.BioReader(stringFile(mstr),'fasta'):
    print fa

Output:

>act-1
ACGATCGTGCATTACGTCGATCGTAGCATGCTAGCTGCGTACGTAGCTTGCTA
>egl-1
CAGTCTAGCATCGTACGATCGTGCTACGTACGTATCGTACGATCGTACGTAGCT
Clone this wiki locally