Skip to content

ONEcode python interface#6

Open
AnantMaheshwari wants to merge 2 commits intothegenemyers:mainfrom
AnantMaheshwari:python_API
Open

ONEcode python interface#6
AnantMaheshwari wants to merge 2 commits intothegenemyers:mainfrom
AnantMaheshwari:python_API

Conversation

@AnantMaheshwari
Copy link

Uses pybind to create a python interface for ONEcode files, wrapping around existing functionality in the C++ interface (ONElib.hpp).

Notebook provides example usage patterns e.g. see https://github.com/AnantMaheshwari/ONEcode/blob/python_API/pyOnelib.ipynb
for examples of interacting with the API.

For example, the below code block would allow a user to read sequences from a test file (TEST/small.1seq) and write a new ONEfile that is the same sequences reverse complemented (TEST/small_rc.1seq)

def reverse_complement(seq):
    """Simple reverse complement for DNA sequences"""
    complement = {'A': 'T', 'T': 'A', 'G': 'C', 'C': 'G',
                  'a': 't', 't': 'a', 'g': 'c', 'c': 'g'}
    return ''.join(complement.get(base, base) for base in reversed(seq))

# Open input file
infile = ONEcode.ONEfile("./TEST/small.seq", "r", schema, "", 1)

# Create output file for reverse complements
outfile = ONEcode.ONEfile("./TEST/small_rc.1seq", "wb", schema, "seq", 1)
outfile.addProvenance("python_tutorial", "1.0.0", "Reverse complement from Python")

# Process sequences
print("Processing sequences...\n")
while infile.readLine():
    lt = infile.lineType()
    
    if lt == 'S':
        seq = infile.getString()
        rc_seq = reverse_complement(seq)
        outfile.writeLine('S', rc_seq)
        
            print(f"Sequence {seq_count}:")
            print(f"  Original: {seq[:40]}..." if len(seq) > 40 else f"  Original: {seq}")
            print(f"  Rev-comp: {rc_seq[:40]}..." if len(rc_seq) > 40 else f"  Rev-comp: {rc_seq}")
    
    elif lt == 'I':
        seq_id = infile.getString()
        outfile.writeLine('I', seq_id + "_RC")

del outfile

print(f"Output written to: ./TEST/small_rc.1seq")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant