2017-03-24 15 views
0

私はこのファイルを持っている:のpython:キーワードに基づいてファイルを分割

あなたはそこに2つのシリーズ(GSE11097とGSE11291)があり、そして私は、各シリーズの概要を望むここで見ることができます
GSENumber Species Platform Sample Age Tissue Sex  Count 
GSE11097 Rat  GPL1355 GSM280267 4 Liver Male Count 
GSE11097 Rat  GPL1355 GSM280268 4 Liver Female Count 
GSE11097 Rat  GPL1355 GSM280269 6 Liver Male Count 
GSE11097 Rat  GPL1355 GSM280409 6 Liver Female Count 
GSE11291 Mouse GPL1261 GSM284967 5 Heart Male Count 
GSE11291 Mouse GPL1261 GSM284968 5 Heart Male Count 
GSE11291 Mouse GPL1261 GSM284969 5 Heart Male Count 
GSE11291 Mouse GPL1261 GSM284970 5 Heart Male Count 
GSE11291 Mouse GPL1261 GSM284975 10 Heart Male Count 
GSE11291 Mouse GPL1261 GSM284976 10 Heart Male Count 
GSE11291 Mouse GPL1261 GSM284987 5 Muscle Male Count 
GSE11291 Mouse GPL1261 GSM284988 5 Muscle Female Count 
GSE11291 Mouse GPL1261 GSM284989 30 Muscle Male Count 
GSE11291 Mouse GPL1261 GSM284990 30 Muscle Male Count 
GSE11291 Mouse GPL1261 GSM284991 30 Muscle Male Count 

。ファイルに

  1. 読むと、すべてのリストを作る:

    Series  Species Platform AgeRange Tissue Sex Count 
    GSE11097 Rat  GPL1355  4-6 Liver Mixed Count 
    GSE11291 Mouse GPL1261  5-10 Heart Male  Count 
    GSE11291 Mouse GPL1261  5-30 Muscle Mixed Count 
    

    だから私はこれを行うための一つの方法は次のようになります知っている:出力はそれぞれ「GSE」数のため、このような辞書でなければなりませんGSE番号。

  2. その後、再びファイルを読み込むと、GSE数に基づいて解析します。

  1. 私は二回、ファイル全体を読み込むためにきました(実際には、ここでの例よりもはるかに大きいファイル)

  2. これを:

    import sys 
    
    list_of_series = list(set([line.strip().split()[0] for line in open(sys.argv[1])])) 
    
    list_of_dicts = [] 
    for each_list in list_of_series: 
        temp_dict={"species":"","platform":"","age":[],"tissue":"","Sex":[],"Count":""} 
        for line in open(sys.argv[1]).readlines()[1:]: 
          line = line.strip().split() 
          if line[0] == each_list: 
           temp_dict["species"] = line[1] 
           temp_dict["platform"] = line[2] 
           temp_dict["age"].append(line[4]) 
           temp_dict["tissue"] = line[5] 
           temp_dict["sex"].append(line[6]) 
           temp_dict["count"] = line[7] 
    

    は、私は、これは二つの方法で厄介だと思いますメソッドは、同じ単語を持つ同じ辞書エントリ上に書き換えを保持します。セックスに問題があります。また

は、私が 『それ以外の、辞書に入れて『混合「男性と女性の両方が、置けば』男性』または 『女性』を言いたい。

Iをこのコードを動作させることができますが、私はコードをより洗練された/もっとpythonicするためのクイックヒントについては疑問に思っていますか?

+0

は、なぜあなたは二回、ファイルを読み込む必要がありますか? readlines()を使ってリストに一度だけ読み込んでから、複数回反復する必要がある場合は、リストを繰り返してください。 – Artagel

+0

これは私が思うようなクエリ言語で行うべきことです。例えば。 mysqlにロードしてクエリを作成します。しかし、私は今 –

答えて

0

私はMax Paymarにこれを問い合わせ言語で行う必要があります。 Pythonでは、パンダモジュールが大いに役立つでしょう。

import pandas as pd 

## columns widths of the fixed width format 
fwidths = [12, 8, 8, 12, 4, 8, 8, 5] 

## read fixed width format into pandas data frame 
df = pd.read_fwf("your_file.txt", widths=fwidths, header=1, 
       names=["GSENumber", "Species", "Platform", "Sample", 
         "Age", "Tissue", "Sex", "Count"]) 

## drop "Sample" column as it is not needed in the output 
df = df.drop("Sample", axis=1) 

## group by GSENumber 
grouped = df.groupby(df.GSENumber) 

## aggregate columns for each group 
aggregated = grouped.agg({'Species': lambda x: list(x.unique()), 
         'Platform': lambda x: list(x.unique()), 
         'Age': lambda x: "%d-%d" % (min(x), max(x)), 
         'Tissue': lambda x: list(x.unique()), 
         'Sex': lambda x: "Mixed" if x.nunique() > 1 else list(x.unique()), 
         'Count': lambda x: list(x.unique())}) 

print aggregated 

これはかなりあなたが尋ねた結果を生成し、純粋なPythonでファイルを解析するよりもはるかにきれいです。

0
import sys 

def main(): 
    data = read_data(open(sys.argv[1])) 
    result = process_rows(data) 
    format_and_print(result, sys.argv[2]) 

def read_data(file): 
    data = [line.strip().split() for line in open(sys.argv[1])] 
    data.pop(0) # remove header 
    return data 


def process_rows(data): 
    data_dict = {} 
    for row in data: 
     process_row(row, data_dict) 
    return data_dict 

def process_row(row, data_dict): 
    composite_key = row[0] + row[1] + row[5] #assuming this is how you are grouping the entries 
    if composite_key in data_dict: 
     data_dict[composite_key]['age_range'].add(row[4]) 
     if row[5] != data_dict[composite_key]: 
      data_dict[composite_key]['sex'] = 'Mixed' 

     #do you need to accumulate the counts? data_dict[composite_key]['count']+=row[6] 

    else: 
     data_dict[composite_key] = { 
      'series': row[0], 
      'species': row[1], 
      'platform': row[2], 
      'age_range': set([row[4]]), 
      'tissue': row[5], 
      'sex': row[6], 
      'count': row[7] 
     } 

def format_and_print(data_dict, outfile): 
    pass 
    #you can implement this one :) 


if __name__ == "__main__": 
    main() 
+0

をあなたのための可能な解決策を書いているので、私は仕事で退屈私はあなたがシリーズ、種や組織によってデータをグループ化しているという仮定をしました。したがって、複合キー。 –

関連する問題