示例#1
0
def _test_photo_type_get(type, date):
    temporary_folder, folder = helper.create_working_folder()

    photo_name = 'photo.{}'.format(type)
    photo_file = helper.get_file(photo_name)
    origin = '{}/{}'.format(folder, photo_name)

    if not photo_file:
        photo_file = helper.download_file(photo_name, folder)
        if not photo_file or not os.path.isfile(photo_file):
            raise SkipTest('{} file not downlaoded'.format(type))

        # downloading for each test is costly so we save it in the working directory
        file_path_save_as = helper.get_file_path(photo_name)
        if os.path.isfile(photo_file):
            shutil.copyfile(photo_file, file_path_save_as)

    shutil.copyfile(photo_file, origin)

    photo = Photo(origin)
    metadata = photo.get_metadata()

    shutil.rmtree(folder)

    assert metadata['date_taken'] == helper.time_convert(date), '{} date {}'.format(type, metadata['date_taken'])
示例#2
0
def _test_photo_type_get(type, date):
    temporary_folder, folder = helper.create_working_folder()

    photo_name = 'photo.{}'.format(type)
    photo_file = helper.get_file(photo_name)
    origin = '{}/{}'.format(folder, photo_name)

    if not photo_file:
        photo_file = helper.download_file(photo_name, folder)
        if not photo_file or not os.path.isfile(photo_file):
            raise SkipTest('{} file not downlaoded'.format(type))

        # downloading for each test is costly so we save it in the working directory
        file_path_save_as = helper.get_file_path(photo_name)
        if os.path.isfile(photo_file):
            shutil.copyfile(photo_file, file_path_save_as)

    shutil.copyfile(photo_file, origin)

    photo = Photo(origin)
    metadata = photo.get_metadata()

    shutil.rmtree(folder)

    assert metadata['date_taken'] == helper.time_convert(
        date), '{} date {}'.format(type, metadata['date_taken'])
示例#3
0
def get_genbank(UPLOAD_FOLDER, phage_id, payload):
    """Calls function to create the genbank file and returns it.

    Args:
        UPLOAD_FOLDER:
            The folder containing all of the uploaded files.
        phage_id:
            The current user's ID.
        payload:
            The data sent from the front-end.
    
    Returns:
        The GenBank file.
    """
    gb_file = helper.get_file_path("genbank", UPLOAD_FOLDER)
    fasta_file = helper.get_file_path("fasta", UPLOAD_FOLDER)
    gb_file = create_genbank(fasta_file, UPLOAD_FOLDER, phage_id, payload)
    f = open(gb_file, "r")
    return f.read()
示例#4
0
def annotate_cds(phage_id, request, cds_id, UPLOAD_FOLDER):
    """Updates a CDS given the data and the ID.

    Updates the left, right, and function of a given CDS.
    If CDS does not exist returns an error message.
    Updates the CDS status.

    Args:
        request:
            A dictionary containing the new CDS data.
        cds_id:
            The ID of the CDS to be updated.

    Returns:
        A dictionary containing a pass or fail message.
    """
    put_data = request.get_json()
    cds = Annotations.query.filter_by(phage_id=phage_id).filter_by(
        id=cds_id).first()
    if cds:
        if put_data.get('strand'):
            cds.strand = put_data.get('strand')
            cds.left = put_data.get('left')
            cds.right = put_data.get('right')
            cds.function = put_data.get('function')
            cds.notes = put_data.get('notes')
        else:
            cds.notes = put_data.get('notes')
        response_object['message'] = 'CDS updated!'
    else:
        response_object['message'] = 'CDS did not update.'
    coding_potential = {}
    genemark_gdata_file = helper.get_file_path("gdata", UPLOAD_FOLDER)
    gdata_df = pd.read_csv(genemark_gdata_file, sep='\t', skiprows=16)
    gdata_df.columns = ['Base', '1', '2', '3', '4', '5', '6']
    coding_potential['x_data'] = gdata_df["Base"].to_list()
    coding_potential['y_data_1'] = gdata_df["1"].to_list()
    coding_potential['y_data_2'] = gdata_df["2"].to_list()
    coding_potential['y_data_3'] = gdata_df["3"].to_list()
    coding_potential['y_data_4'] = gdata_df["4"].to_list()
    coding_potential['y_data_5'] = gdata_df["5"].to_list()
    coding_potential['y_data_6'] = gdata_df["6"].to_list()

    if (cds.status == "trnaDELETED" or cds.status == "tRNA"):
        cds.status = put_data.get('status')
        cds.frame = put_data.get('frame')
    else:
        frame, status = helper.get_frame_and_status(cds.left, cds.right,
                                                    cds.strand,
                                                    coding_potential)
        cds.status = status
        cds.frame = frame
    db.session.commit()

    return response_object
示例#5
0
def handle_fasta(UPLOAD_FOLDER):
    """Ensures that the file is in FastA format.

    Args:
        UPLOAD_FOLDER:
            The directory containing all of the uploaded files.
    """
    fasta_file = helper.get_file_path("fasta", UPLOAD_FOLDER)
    with open(fasta_file, "r") as handle:
        lines = handle.readlines()
        correct_start = False
        not_empty = False
        for line in lines:
            if line.startswith('>') and not correct_start:
                correct_start = True
            elif not bool(re.match('^[ACTG\n]+$', line.upper())):
                return False
            elif len(line) > 3:
                not_empty = True
        return correct_start and not_empty
示例#6
0
def test_get_metadata_from_arw():
    temporary_folder, folder = helper.create_working_folder()

    photo_file = helper.get_file('photo.arw')
    origin = '%s/photo.arw' % folder

    if not photo_file:
        photo_file = helper.download_file('photo.arw', folder)
        if not photo_file or not os.path.isfile(photo_file):
            raise SkipTest('arw file not downlaoded')

        # downloading for each test is costly so we save it in the working directory
        file_path_save_as = helper.get_file_path('photo.arw')
        if os.path.isfile(photo_file):
            shutil.copyfile(photo_file, file_path_save_as)

    shutil.copyfile(photo_file, origin)

    photo = Photo(origin)
    metadata = photo.get_metadata()

    shutil.rmtree(folder)

    assert metadata['date_taken'] == helper.time_convert((2007, 4, 8, 17, 41, 18, 6, 98, 0)), metadata['date_taken']
示例#7
0
def predict(model):

    if (model == None):
        model = helper.load_checkpoint()

    image_path = helper.get_file_path(
        '\nPlease enter the path of the image you want to analyse\n')
    topk = helper.get_int(
        '\nPlease enter how many to the top predictions you want to see (topk)\n'
    )

    device = helper.get_device()
    model = helper.load_device(model)

    image_tensor = helper.process_image(image_path).to(device)
    idx_to_class = helper.get_idx_to_class()
    print('\nPredicting\n')

    with torch.no_grad():
        output = model.forward(image_tensor)

    ps = torch.exp(output)

    topK_ps = torch.topk(ps, topk)

    probs = topK_ps[0].cpu().numpy().squeeze()
    sorted_ps_label_keys = topK_ps[1].cpu().numpy().squeeze()
    get_label = lambda x: idx_to_class[str(x)]

    classes = []

    for i in sorted_ps_label_keys[0:topk]:
        classes.append(get_label(i))

    print('\nFinished predicting\n')
    return probs, classes
示例#8
0
def get_map(phage_id, UPLOAD_FOLDER):
    """Creates and returns a map of the genome.

    Args:
        UPLOAD_FOLDER:
            The folder containing all of the uploaded files.

    Returns:
        A dictionary containing an image of the genome map.

    """
    features = []
    for cds in db.session.query(Annotations).filter_by(
            phage_id=phage_id).order_by(Annotations.left):
        if cds.function != '@DELETED' and cds.status != 'trnaDELETED':
            if cds.strand == '+':
                if cds.status == "tRNA":
                    features.append(
                        GraphicFeature(start=cds.left,
                                       end=cds.right,
                                       strand=+1,
                                       color="#7570b3",
                                       label=cds.id))
                else:
                    features.append(
                        GraphicFeature(start=cds.left,
                                       end=cds.right,
                                       strand=+1,
                                       color="#1b9e77",
                                       label=cds.id))
            else:
                if cds.status == "tRNA":
                    features.append(
                        GraphicFeature(start=cds.left,
                                       end=cds.right,
                                       strand=-1,
                                       color="#7570b3",
                                       label=cds.id))
                else:
                    features.append(
                        GraphicFeature(start=cds.left,
                                       end=cds.right,
                                       strand=-1,
                                       color="#d95f02",
                                       label=cds.id))
    fasta_file = helper.get_file_path("fasta", UPLOAD_FOLDER)
    genome = SeqIO.read(fasta_file, "fasta").seq
    sequence = str(genome)
    record = GraphicRecord(sequence_length=len(sequence), features=features)
    ax, _ = record.plot(figure_width=len(sequence) / 1000)
    ax.figure.savefig(os.path.join(UPLOAD_FOLDER,
                                   'sequence_and_translation.png'),
                      bbox_inches='tight')
    image_byte_string = ""
    with open(os.path.join(UPLOAD_FOLDER, 'sequence_and_translation.png'),
              "rb") as image_file:
        image_byte_string = base64.b64encode(image_file.read())
    response_object = {}
    response_object['status'] = "success"
    response_object['image'] = str(image_byte_string)
    return response_object
示例#9
0
 def __init__(self, bot):
     super().__init__(bot)
     self._filepath = helper.get_file_path()
示例#10
0
def add_cds(request, UPLOAD_FOLDER, phage_id):
    """Adds a new CDS to the database.
    Checks to see if the CDS is an ORF.
    Args:
        request:
            The data sent from the front-end.
        UPLOAD_FOLDER:
            The folder containing all of the uploaded files.
        phage_id:
            The current user's ID.
    
    Returns:
        A dictionary containing a success or fail message.
    """
    new_cds_data = request.get_json()
    force = new_cds_data.get('force')
    coding_potential = {}
    genemark_gdata_file = helper.get_file_path("gdata", UPLOAD_FOLDER)
    gdata_df = pd.read_csv(genemark_gdata_file, sep='\t', skiprows=16)
    gdata_df.columns = ['Base', '1', '2', '3', '4', '5', '6']
    coding_potential['x_data'] = gdata_df["Base"].to_list()
    coding_potential['y_data_1'] = gdata_df["1"].to_list()
    coding_potential['y_data_2'] = gdata_df["2"].to_list()
    coding_potential['y_data_3'] = gdata_df["3"].to_list()
    coding_potential['y_data_4'] = gdata_df["4"].to_list()
    coding_potential['y_data_5'] = gdata_df["5"].to_list()
    coding_potential['y_data_6'] = gdata_df["6"].to_list()
    frame, status = helper.get_frame_and_status(int(new_cds_data.get('left')), int(new_cds_data.get('right')), new_cds_data.get('strand'), coding_potential)
    cds = Annotations(phage_id = phage_id,
                    id = new_cds_data.get('id'),
                    left = new_cds_data.get('left'),
                    right = new_cds_data.get('right'),
                    strand = new_cds_data.get('strand'),
                    function = "None selected",
                    status = status,
                    frame = frame)

    exists = Annotations.query.filter_by(phage_id=phage_id).filter_by(left=new_cds_data.get('left'), right=new_cds_data.get('right'), strand=new_cds_data.get('strand')).first()
    orf = Blast_Results.query.filter_by(phage_id=phage_id).filter_by(left=new_cds_data.get('left'), right=new_cds_data.get('right'), strand=new_cds_data.get('strand')).first()
    if force:
        db.session.add(cds)
        db.session.commit()
        response_object['message'] = "Added succesfully."
        id_index = 0
        for cds in db.session.query(Annotations).filter_by(phage_id=phage_id).order_by(Annotations.left):
            id_index += 1
            cds.id = str(id_index)
        db.session.commit()
        id_index = 0
        phage_name = db.session.query(Users).filter_by(id=phage_id).first().phage_id
        for cds in db.session.query(Annotations).filter_by(phage_id=phage_id).order_by(Annotations.left):
            id_index += 1
            cds.id = phage_name + '_' + str(id_index)
        db.session.commit()
        return response_object
    if exists:
        response_object['message'] = "ID already exists."
    elif not orf:
        response_object['message'] = "Not orf."
    else:
        db.session.add(cds)
        db.session.commit()
        response_object['message'] = "Added succesfully."
        id_index = 0
        for cds in db.session.query(Annotations).filter_by(phage_id=phage_id).order_by(Annotations.left):
            id_index += 1
            cds.id = str(id_index)
        db.session.commit()
        id_index = 0
        phage_name = db.session.query(Users).filter_by(id=phage_id).first().phage_id
        for cds in db.session.query(Annotations).filter_by(phage_id=phage_id).order_by(Annotations.left):
            id_index += 1
            cds.id = phage_name + '_' + str(id_index)
        db.session.commit()
    return response_object
示例#11
0
def get_cds_data(phage_id, UPLOAD_FOLDER, cds_id):
    """Queries and returns all of the data for a CDS given the ID.

    Gets left, right, function, and status for the CDS.
    Gets the right of the previous CDS and the left of the next CDS.
    Gets the blast results for the CDS.
    Gets the genemark coding potential data.
    Gets the next non-updated CDS ID.

    Args:
        UPLOAD_FOLDER:
            The folder containing all of the uploaded files.
        cds_id:
            The ID of the requested CDS.
    Returns:
        A dictionary containing the CDS data including the blast results and coding potential.
        
    """
    num_begins = cds_id.rfind('_') + 1
    index = float(cds_id[num_begins:])
    index = int(index)
    prev_id = cds_id[:num_begins] + str(index - 1)
    next_id = cds_id[:num_begins] + str(index + 1)
    cds = Annotations.query.filter_by(phage_id=phage_id).filter_by(
        id=cds_id).first()
    prev_cds = Annotations.query.filter_by(phage_id=phage_id).filter_by(
        id=prev_id).first()
    next_cds = Annotations.query.filter_by(phage_id=phage_id).filter_by(
        id=next_id).first()
    if prev_cds is not None:
        response_object['prevCDS'] = prev_id
        response_object['prev_right'] = prev_cds.right
    else:
        response_object['prevCDS'] = 'undefined'
        response_object['prev_right'] = 0
    if next_cds is not None:
        response_object['next_left'] = next_cds.left
    else:
        response_object['next_left'] = cds.right
    response_object['cds'] = {
        'id': cds.id,
        'left': cds.left,
        'right': cds.right,
        'strand': cds.strand,
        'function': cds.function,
        'status': cds.status,
        'frame': cds.frame,
        'notes': cds.notes
    }

    left_positions, right_positions = get_blasts(phage_id, cds.left)

    genemark_gdata_file = helper.get_file_path("gdata", UPLOAD_FOLDER)
    gdata_df = pd.read_csv(genemark_gdata_file, sep='\t', skiprows=16)
    gdata_df.columns = ['Base', '1', '2', '3', '4', '5', '6']
    try:
        gdata_df = gdata_df[gdata_df.Base.isin(
            range(min(left_positions) - 100,
                  max(right_positions) + 100))]
    except:
        response_object['message'] = "Not finished parsing"
        return response_object
    response_object['message'] = "Finished"
    response_object['x_data'] = gdata_df["Base"].to_list()
    response_object['y_data_1'] = gdata_df["1"].to_list()
    response_object['y_data_2'] = gdata_df["2"].to_list()
    response_object['y_data_3'] = gdata_df["3"].to_list()
    response_object['y_data_4'] = gdata_df["4"].to_list()
    response_object['y_data_5'] = gdata_df["5"].to_list()
    response_object['y_data_6'] = gdata_df["6"].to_list()

    reached_CDS = False
    response_object['nextCDS'] = 'undefined'
    for cds in db.session.query(Annotations).filter_by(
            phage_id=phage_id).order_by(Annotations.left):
        if reached_CDS and cds.function != "@DELETED" and cds.status != "tRNA":
            response_object['nextCDS'] = cds.id
            break
        elif cds.id == cds_id:
            reached_CDS = True

    reached_CDS = False
    response_object['prevCDS'] = 'undefined'
    for cds in db.session.query(Annotations).filter_by(
            phage_id=phage_id).order_by(Annotations.left.desc()):
        if reached_CDS and cds.function != "@DELETED" and cds.status != "tRNA":
            response_object['prevCDS'] = cds.id
            break
        elif cds.id == cds_id:
            reached_CDS = True
    if Gene_Calls.query.filter_by(phage_id=phage_id).filter_by(
            id='Glimmer').first():
        response_object['glimmer'] = Gene_Calls.query.filter_by(
            phage_id=phage_id).filter_by(id='Glimmer').first().calls.split(',')
    if Gene_Calls.query.filter_by(phage_id=phage_id).filter_by(
            id='GeneMark').first():
        response_object['genemark'] = Gene_Calls.query.filter_by(
            phage_id=phage_id).filter_by(
                id='GeneMark').first().calls.split(',')
    if Gene_Calls.query.filter_by(phage_id=phage_id).filter_by(
            id='Phanotate').first():
        response_object['phanotate'] = Gene_Calls.query.filter_by(
            phage_id=phage_id).filter_by(
                id='Phanotate').first().calls.split(',')
    if Gene_Calls.query.filter_by(phage_id=phage_id).filter_by(
            id='Prodigal').first():
        response_object['prodigal'] = Gene_Calls.query.filter_by(
            phage_id=phage_id).filter_by(
                id='Prodigal').first().calls.split(',')

    return response_object